Skip to content

Commit fa400b3

Browse files
committed
add changes based on code review and logging
1 parent 2cb5ce4 commit fa400b3

File tree

5 files changed

+103
-25
lines changed

5 files changed

+103
-25
lines changed

packages/backend/src/apps/formatter/__tests__/date-time.format.test.ts

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -139,26 +139,41 @@ describe('convert date time', () => {
139139
})
140140
})
141141

142-
// check if dd LLL yyyy format is corrected to en-US all the time
143-
it('maintains dd LLL yyyy format in en-US', () => {
142+
it.each([
143+
{
144+
inputFormat: 'formsgDateField',
145+
inputValue: '01 Sept 2025',
146+
toFormat: 'dd LLL yyyy',
147+
expectedResult: '01 Sep 2025',
148+
},
149+
{
150+
inputFormat: 'formsgDateField',
151+
inputValue: '02 Sep 2025',
152+
toFormat: 'dd LLL yyyy',
153+
expectedResult: '02 Sep 2025',
154+
},
155+
{
156+
inputFormat: 'dd LLL yyyy hh:mm a',
157+
inputValue: '03 Sep 2025 11:50 pm',
158+
toFormat: 'dd LLL yyyy',
159+
expectedResult: '03 Sep 2025',
160+
},
161+
{
162+
inputFormat: 'dd LLL yyyy hh:mm:ss a',
163+
inputValue: '04 Sept 2025 11:45:30 pm',
164+
toFormat: 'dd LLL yyyy hh:mm a',
165+
expectedResult: '04 Sep 2025 11:45 pm',
166+
},
167+
])('converts dd MMM yyyy format from en-SG to en-US', (testParams) => {
168+
const { inputFormat, inputValue, toFormat, expectedResult } = testParams
144169
$.step.parameters = {
145-
dateTimeFormat: 'formsgDateField',
146-
formatDateTimeToFormat: 'dd LLL yyyy',
170+
dateTimeFormat: inputFormat,
171+
formatDateTimeToFormat: toFormat,
147172
}
148-
spec.transformData($, '01 Sep 2024')
149-
expect(mocks.setActionItem).toBeCalledWith({
150-
raw: { result: '01 Sep 2024' },
151-
})
152-
})
173+
spec.transformData($, inputValue)
153174

154-
it('converts dd LLL yyyy format from en-SG to en-US', () => {
155-
$.step.parameters = {
156-
dateTimeFormat: 'formsgDateField',
157-
formatDateTimeToFormat: 'dd LLL yyyy',
158-
}
159-
spec.transformData($, '01 Sept 2024')
160175
expect(mocks.setActionItem).toBeCalledWith({
161-
raw: { result: '01 Sep 2024' },
176+
raw: { result: expectedResult },
162177
})
163178
})
164179
})

packages/backend/src/apps/formatter/actions/date-time/transforms/convert-date-time/index.ts

Lines changed: 18 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { IGlobalVariable } from '@plumber/types'
22

3+
import { DateTime } from 'luxon'
34
import { ZodError } from 'zod'
45

56
import StepError, { GenericSolution } from '@/errors/step'
@@ -35,6 +36,22 @@ function getParams($: IGlobalVariable) /* inferred return type */ {
3536
}
3637
}
3738

39+
// helper function to check if format contains dd LLL yyyy, if so, force en-US to maintain consistency with FormSG
40+
function formatStringToUSLocale(formatString: string, dateTime: DateTime) {
41+
switch (formatString) {
42+
case 'dd LLL yyyy':
43+
case 'dd LLL yyyy hh:mm a':
44+
case 'dd LLL yyyy hh:mm:ss a': {
45+
const dateTimeString = dateTime.toFormat(formatString, {
46+
locale: 'en-SG', // TODO: ???
47+
})
48+
return dateTimeString
49+
}
50+
default:
51+
return dateTime.toFormat(formatString)
52+
}
53+
}
54+
3855
export const spec = {
3956
id: 'formatDateTime',
4057

@@ -53,10 +70,7 @@ export const spec = {
5370
// check if format is dd LLL yyyy, if so, force en-US to maintain consistency with FormSG
5471
$.setActionItem({
5572
raw: {
56-
result:
57-
formatString === 'dd LLL yyyy'
58-
? dateTime.toFormat(formatString, { locale: 'en-US' })
59-
: dateTime.toFormat(formatString),
73+
result: formatStringToUSLocale(formatString, dateTime),
6074
},
6175
})
6276
} catch (error) {

packages/backend/src/services/action.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,8 @@
1-
import { type IActionRunResult, TestRunStepMetadata } from '@plumber/types'
1+
import type {
2+
IActionRunResult,
3+
IJSONValue,
4+
TestRunStepMetadata,
5+
} from '@plumber/types'
26

37
import {
48
FOR_EACH_ITERATION_DELAY,
@@ -23,6 +27,23 @@ import { enqueueActionJob } from '@/queues/action'
2327

2428
import getForEachMetadata from './helpers/get-for-each-metadata'
2529

30+
// TODO on Oct: remove this once the logging is not needed anymore
31+
const SEPT_KEYWORD_REGEX = / Sept /
32+
33+
function containsSeptKeyword(obj: IJSONValue): boolean {
34+
if (typeof obj === 'string') {
35+
return SEPT_KEYWORD_REGEX.test(obj)
36+
}
37+
if (Array.isArray(obj)) {
38+
return obj.some((item) => containsSeptKeyword(item))
39+
}
40+
41+
if (obj !== null && typeof obj === 'object') {
42+
return Object.values(obj).some((value) => containsSeptKeyword(value))
43+
}
44+
return false
45+
}
46+
2647
type ProcessActionOptions = {
2748
flowId: string
2849
executionId: string
@@ -209,6 +230,19 @@ export const processAction = async (options: ProcessActionOptions) => {
209230
metadata,
210231
})
211232

233+
// TODO on Oct: remove this once the logging is not needed anymore
234+
if (!testRun && status === 'failure') {
235+
if (containsSeptKeyword($.step.parameters ?? {})) {
236+
logger.info('Execution contains Sept keyword', {
237+
event: 'execution-contains-sept-keyword',
238+
stepId,
239+
flowId,
240+
executionId,
241+
executionStepId: executionStep.id,
242+
})
243+
}
244+
}
245+
212246
let nextStep = null
213247
switch (runResult.nextStep?.command) {
214248
case 'jump-to-step':

packages/frontend/src/components/NewsDrawer/NewsItem.tsx

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,9 @@ const DATE_FORMAT = 'dd MMM yyyy'
2828

2929
export default function NewsItem(props: NewsItemProps) {
3030
const { date, tag, title, details, multimedia } = props
31-
const formattedDate = DateTime.fromISO(date).toFormat(DATE_FORMAT)
31+
const formattedDate = DateTime.fromISO(date).toFormat(DATE_FORMAT, {
32+
locale: 'en-US',
33+
})
3234
const displayedMultimedia = useMemo(() => {
3335
if (!multimedia) {
3436
return

packages/frontend/src/helpers/dateTime.ts

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,25 @@ export function toPrettyDateString(
1515
return ''
1616
}
1717

18+
// Locale en-US is used to maintain consistency with FormSG
1819
switch (from) {
1920
case 'iso':
20-
return DateTime.fromISO(date as string).toFormat('dd MMM yyyy h:mm a')
21+
return DateTime.fromISO(date as string).toFormat('dd MMM yyyy h:mm a', {
22+
locale: 'en-US',
23+
})
2124
case 'ms':
22-
return DateTime.fromMillis(date as number).toFormat('dd MMM yyyy h:mm a')
25+
return DateTime.fromMillis(date as number).toFormat(
26+
'dd MMM yyyy h:mm a',
27+
{
28+
locale: 'en-US',
29+
},
30+
)
2331
default:
24-
return DateTime.fromMillis(date as number).toFormat('dd MMM yyyy h:mm a')
32+
return DateTime.fromMillis(date as number).toFormat(
33+
'dd MMM yyyy h:mm a',
34+
{
35+
locale: 'en-US',
36+
},
37+
)
2538
}
2639
}

0 commit comments

Comments
 (0)