Skip to content

Commit

Permalink
Merge pull request #7395 from uktrade/fix/remove-format-without-parsing
Browse files Browse the repository at this point in the history
Remove `formatWithoutParsing` function and replace it with `formatDate`
  • Loading branch information
paulgain authored Dec 16, 2024
2 parents 2bd44ca + 319075a commit 79df8cb
Show file tree
Hide file tree
Showing 35 changed files with 290 additions and 220 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ describe('Edit company form transformers', () => {
expect(actual).to.deep.equal({
headingText: undefined,
headingUrl: urls.companies.detail('123'),
subheading: 'Updated on undefined',
subheading: undefined,
metadata: [],
badges: [],
})
Expand Down
2 changes: 1 addition & 1 deletion src/apps/companies/apps/dnb-hierarchy/transformers.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ function transformCompanyToDnbHierarchyList({
return {
headingText: name,
headingUrl: urls.companies.detail(id),
subheading: `Updated on ${formatDateTime(modified_on)}`,
subheading: modified_on && `Updated on ${formatDateTime(modified_on)}`,
metadata,
badges,
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
const { getInteractionTimestamp } = require('../../../../client/utils/date')
const middleware = require('../last-interaction-date')
const buildMiddlewareParameters = require('../../../../../test/unit/helpers/middleware-parameters-builder')
const { getInteractionTimestamp } = require('../utils')
const middleware = require('../last-interaction-date')

const QUERY_PARAM = 'interaction_between'
const START_DATE_PARAM = 'latest_interaction_date_before'
Expand Down
2 changes: 1 addition & 1 deletion src/apps/companies/middleware/last-interaction-date.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
const { QUERY_FIELDS_MAP } = require('../constants')
const { getInteractionTimestamp } = require('../../../client/utils/date')
const { getInteractionTimestamp } = require('./utils')

const QUERY_PARAM = QUERY_FIELDS_MAP.lastInteractionDate
const START_DATE_PARAM = 'latest_interaction_date_before'
Expand Down
20 changes: 20 additions & 0 deletions src/apps/companies/middleware/utils.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
const { subMonths } = require('date-fns')

const {
formatDate,
DATE_FORMAT_INTERACTION_TIMESTAMP,
} = require('../../../client/utils/date-utils')

function getInteractionTimestamp({ offset }) {
const date = new Date()

if (offset > 0) {
subMonths(date, offset)
}

return formatDate(date, DATE_FORMAT_INTERACTION_TIMESTAMP)
}

module.exports = {
getInteractionTimestamp,
}
19 changes: 12 additions & 7 deletions src/apps/interactions/apps/details-form/client/tasks.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,12 @@ import {
transformExportCountriesToGroupStatus,
} from '../../../../transformers'

import { formatWithoutParsing } from '../../../../../client/utils/date'
import {
formatDate,
DATE_FORMAT_DAY,
DATE_FORMAT_MONTH,
DATE_FORMAT_YEAR,
} from '../../../../../client/utils/date-utils'

const { transformValueForAPI } = require('../../../../../client/utils/date')

Expand Down Expand Up @@ -139,9 +144,9 @@ const transformInteractionToValues = (interaction, companyId, investmentId) => {
transformObjectToOption(adviser)
),
date: {
day: formatWithoutParsing(date, 'dd'),
month: formatWithoutParsing(date, 'MM'),
year: formatWithoutParsing(date, 'yyyy'),
day: formatDate(date, DATE_FORMAT_DAY),
month: formatDate(date, DATE_FORMAT_MONTH),
year: formatDate(date, DATE_FORMAT_YEAR),
},
companies: [companyId],
investment_project: investmentId,
Expand Down Expand Up @@ -237,9 +242,9 @@ export async function getInitialFormValues({
companies: [companyId],
investment_project: investmentId,
date: {
day: formatWithoutParsing(date, 'dd'),
month: formatWithoutParsing(date, 'MM'),
year: formatWithoutParsing(date, 'yyyy'),
day: formatDate(date, DATE_FORMAT_DAY),
month: formatDate(date, DATE_FORMAT_MONTH),
year: formatDate(date, DATE_FORMAT_YEAR),
},
contacts:
referral && referral.contact
Expand Down
Original file line number Diff line number Diff line change
@@ -1,18 +1,13 @@
import React from 'react'
import PropTypes from 'prop-types'
import styled from 'styled-components'

import { H3 } from '@govuk-react/heading'
import { FONT_SIZE, FONT_WEIGHTS, SPACING } from '@govuk-react/constants'

import { DATE_DAY_LONG_FORMAT } from '../../../common/constants'
import urls from '../../../lib/urls'
import { DARK_GREY, LINK_COLOUR, RED, TEXT_COLOUR } from '../../utils/colours'

const {
formatWithoutParsing,
getDifferenceInDaysLabel,
} = require('../../utils/date')
import { formatDate, DATE_FORMAT_FULL_DAY } from '../../utils/date-utils'
import { getDifferenceInDaysLabel } from '../../utils/date'
import urls from '../../../lib/urls'

const StyledSubHeading = styled(H3)`
color: ${RED};
Expand Down Expand Up @@ -92,11 +87,7 @@ const OutstandingPropositions = ({ results, count }) => (
{investment_project.project_code}
</StyledProjectCode>
<StyledDueDate data-test="outstanding-proposition-deadline">
Due{' '}
{formatWithoutParsing(
new Date(deadline),
DATE_DAY_LONG_FORMAT
)}
Due {formatDate(deadline, DATE_FORMAT_FULL_DAY)}
</StyledDueDate>
</StyledDetails>
<StyledDueCountdown data-test="outstanding-proposition-countdown">
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ import PropTypes from 'prop-types'
import styled from 'styled-components'
import { FONT_SIZE, FONT_WEIGHTS, SPACING } from '@govuk-react/constants'

import { DATE_DAY_LONG_FORMAT } from '../../../common/constants'
import {
BLACK,
BUTTON_COLOUR,
Expand All @@ -14,11 +13,12 @@ import {
} from '../../../client/utils/colours'

const {
formatWithoutParsing,
getDifferenceInDays,
getDifferenceInDaysLabel,
} = require('../../utils/date')

const { formatDate, DATE_FORMAT_FULL_DAY } = require('../../utils/date-utils')

const StyledPanel = styled('div')`
padding: ${SPACING.SCALE_2};
color: ${BLACK};
Expand Down Expand Up @@ -72,10 +72,7 @@ const InvestmentEstimatedLandDate = ({ estimatedLandDate, ...props }) => {
{getDifferenceInDaysLabel(estimatedLandDate)}
</StyledTitle>
<StyledBody data-test="estimated-land-date-date">
{formatWithoutParsing(
new Date(estimatedLandDate),
DATE_DAY_LONG_FORMAT
)}
{formatDate(estimatedLandDate, DATE_FORMAT_FULL_DAY)}
</StyledBody>
</Panel>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import { connect } from 'react-redux'
import { subMonths } from 'date-fns'

import ActivityCard from './ActivityCard'
import { OVERVIEW_RECENT_ACTIVITY_ID, recentState2props } from './state'
import { formatWithoutParsing, subtractMonths } from '../../../../../utils/date'
import { DATE_LONG_FORMAT_3 } from '../../../../../../common/constants'
import { COMPANIES__OVERVIEW_RECENT_ACTIVITY_LOADED } from '../../../../../actions'
import { formatDate, DATE_FORMAT_ISO } from '../../../../../utils/date-utils'
import { OVERVIEW_RECENT_ACTIVITY_ID, recentState2props } from './state'
import ActivityCard from './ActivityCard'

const RecentActivityCard = ({ company, results }) => (
<ActivityCard
Expand All @@ -16,11 +16,8 @@ const RecentActivityCard = ({ company, results }) => (
stateId={OVERVIEW_RECENT_ACTIVITY_ID}
action={COMPANIES__OVERVIEW_RECENT_ACTIVITY_LOADED}
additionalPayload={{
date_before: formatWithoutParsing(new Date(), DATE_LONG_FORMAT_3),
date_after: formatWithoutParsing(
subtractMonths(new Date(), 6),
DATE_LONG_FORMAT_3
),
date_before: formatDate(new Date(), DATE_FORMAT_ISO),
date_after: formatDate(subMonths(new Date(), 6), DATE_FORMAT_ISO),
}}
/>
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
import React from 'react'
import { connect } from 'react-redux'
import { endOfTomorrow } from 'date-fns'

import ActivityCard from './ActivityCard'
import { OVERVIEW_UPCOMING_ACTIVITY_ID, upcomingState2props } from './state'
import { formatWithoutParsing, tomorrow } from '../../../../../utils/date'
import { DATE_LONG_FORMAT_3 } from '../../../../../../common/constants'
import { COMPANIES__OVERVIEW_UPCOMING_ACTIVITY_LOADED } from '../../../../../actions'
import { formatDate, DATE_FORMAT_ISO } from '../../../../../utils/date-utils'
import { OVERVIEW_UPCOMING_ACTIVITY_ID, upcomingState2props } from './state'
import ActivityCard from './ActivityCard'

const UpcomingActivityCard = ({ company, results }) => (
<ActivityCard
Expand All @@ -16,7 +16,7 @@ const UpcomingActivityCard = ({ company, results }) => (
stateId={OVERVIEW_UPCOMING_ACTIVITY_ID}
action={COMPANIES__OVERVIEW_UPCOMING_ACTIVITY_LOADED}
additionalPayload={{
date_after: formatWithoutParsing(tomorrow(), DATE_LONG_FORMAT_3),
date_after: formatDate(endOfTomorrow(), DATE_FORMAT_ISO),
sortby: 'date:asc',
}}
/>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ const transformCompanyToListItem =

return {
id,
subheading: modified_on
? `Updated on ${formatDate(modified_on, DATE_FORMAT_MEDIUM_WITH_TIME)}`
: undefined,
subheading:
modified_on &&
`Updated on ${formatDate(modified_on, DATE_FORMAT_MEDIUM_WITH_TIME)}`,
headingText: name,
headingUrl: urls.investments.projects.editRecipientCompany(projectId, id),
badges,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
* put this back into the main transformers file.
*/

const { formatMonthYearDate } = require('../../../utils/date')
const { formatDate, DATE_FORMAT_ISO } = require('../../../utils/date-utils')

const MONTH_DATE_FIELD_LIST = [
'estimated_land_date_before',
Expand All @@ -17,7 +17,7 @@ const transformLandDateFilters = (params) => {
// The API only accepts yyyy-MM-dd format, so month-year filters have to be updated
MONTH_DATE_FIELD_LIST.forEach((field) => {
if (field in params) {
params[field] = formatMonthYearDate(params[field])
params[field] = formatDate(params[field], DATE_FORMAT_ISO)
}
})
return params
Expand Down
31 changes: 31 additions & 0 deletions src/client/modules/Tasks/TaskForm/__test__/transformer.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { format, addMonths, addDays } from 'date-fns'

import { getDueDate } from '../transformers'

describe('getDueDate', () => {
it('should return the transformed value for a custom date', () => {
const customDate = { year: 2024, month: 12, day: 31 }
const expectedTransformedValue = '2024-12-31'

const result = getDueDate('custom', customDate)
expect(result).to.equal(expectedTransformedValue)
})

it('should return the correct due date for the next month', () => {
const today = new Date()
const nextMonthDate = addMonths(today, 1)
const formattedDate = format(nextMonthDate, 'yyyy-MM-dd')

const result = getDueDate('month')
expect(result).to.equal(formattedDate)
})

it('should return the correct due date for the next week', () => {
const today = new Date()
const nextWeekDate = addDays(today, 7)
const formattedDate = format(nextWeekDate, 'yyyy-MM-dd')

const result = getDueDate('week')
expect(result).to.equal(formattedDate)
})
})
32 changes: 14 additions & 18 deletions src/client/modules/Tasks/TaskForm/transformers.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
import { addDays, addMonths } from 'date-fns'

import { transformOptionToValue } from '../../../../apps/transformers'
import {
OPTION_YES,
DATE_LONG_FORMAT_3,
OPTION_NO,
} from '../../../../common/constants'
import { OPTION_YES, OPTION_NO } from '../../../../common/constants'
import {
transformArrayIdNameToValueLabel,
transformIdNameToValueLabel,
} from '../../../transformers'
import {
formatWithoutParsing,
transformValueForAPI,
addMonths,
addDays,
convertDateToFieldDateObject,
} from '../../../utils/date'
import { formatDate, DATE_FORMAT_ISO } from '../../../utils/date-utils'

import { OPTIONS } from './constants'

export const transformTaskFormValuesForAPI = (
Expand Down Expand Up @@ -60,17 +57,16 @@ const getUniquePKValue = (formValues) => {
return { investment_project: null, company: null, interaction: null }
}

const getDueDate = (dueDate, customDate) => {
switch (dueDate) {
case 'custom':
return transformValueForAPI(customDate)
case 'month':
return formatWithoutParsing(addMonths(new Date(), 1), DATE_LONG_FORMAT_3)
case 'week':
return formatWithoutParsing(addDays(new Date(), 7), DATE_LONG_FORMAT_3)
default:
null
export const getDueDate = (dueDate, customDate) => {
const today = new Date()

const handlers = {
custom: () => transformValueForAPI(customDate),
month: () => formatDate(addMonths(today, 1), DATE_FORMAT_ISO),
week: () => formatDate(addDays(today, 7), DATE_FORMAT_ISO),
}

return handlers[dueDate]?.()
}

const transformAdvisor = (advisers, currentAdviserId) =>
Expand Down
Loading

0 comments on commit 79df8cb

Please sign in to comment.