Skip to content

Commit e77d2a8

Browse files
committed
fix: use toPlumberFormat
1 parent fa400b3 commit e77d2a8

File tree

14 files changed

+74
-55
lines changed

14 files changed

+74
-55
lines changed

packages/backend/src/apps/delay/__tests__/delay-until.test.ts

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@/types/luxon-extensions'
2+
13
import { type IGlobalVariable } from '@plumber/types'
24

35
import { DateTime } from 'luxon'
@@ -180,8 +182,8 @@ describe('Delay until action', () => {
180182
expect(result).toBeFalsy()
181183
expect(mocks.setActionItem).toBeCalledWith({
182184
raw: {
183-
delayUntil: DateTime.now().toFormat('dd MMM yyyy'),
184-
delayUntilTime: DateTime.now().toFormat('HH:mm'),
185+
delayUntil: DateTime.now().toPlumberFormat('dd MMM yyyy'),
186+
delayUntilTime: DateTime.now().toPlumberFormat('HH:mm'),
185187
},
186188
})
187189
})

packages/backend/src/apps/delay/actions/delay-until/index.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const action: IRawAction = {
3333
key: 'delayUntil',
3434
type: 'string' as const,
3535
required: true,
36-
description: 'Delay until the date. E.g. 25 Aug 2023',
36+
description: 'Delay until the date. E.g. 05 Aug 2026',
3737
variables: true,
3838
},
3939
{
@@ -62,7 +62,7 @@ const action: IRawAction = {
6262
)
6363

6464
// check if delayUntilString is of dd MMM yyyy format, if so, force en-US to maintain consistency with FormSG
65-
let delayUntilStringUSFormatted = delayUntilString
65+
let delayUntilFormatted = delayUntilString
6666
// Try parsing with en-SG first (for "Sept"), then en-US (for "Sep"), this is for the test case to work because it is not aware of the en-US locale
6767
let dateTime = DateTime.fromFormat(delayUntilString, 'dd MMM yyyy', {
6868
locale: 'en-SG',
@@ -73,13 +73,11 @@ const action: IRawAction = {
7373
})
7474
}
7575
if (dateTime.isValid) {
76-
delayUntilStringUSFormatted = dateTime.toFormat('dd MMM yyyy', {
77-
locale: 'en-US',
78-
})
76+
delayUntilFormatted = dateTime.toPlumberFormat('dd MMM yyyy')
7977
}
8078

8179
let dataItem = {
82-
delayUntil: delayUntilStringUSFormatted,
80+
delayUntil: delayUntilFormatted,
8381
delayUntilTime: delayUntilTimeString,
8482
}
8583

@@ -102,8 +100,8 @@ const action: IRawAction = {
102100
if (isRetry) {
103101
const dateTimeNow = DateTime.now()
104102
dataItem = {
105-
delayUntil: dateTimeNow.toFormat('dd MMM yyyy', { locale: 'en-US' }), // Force en-US to maintain consistency with FormSG
106-
delayUntilTime: dateTimeNow.toFormat('HH:mm'),
103+
delayUntil: dateTimeNow.toPlumberFormat('dd MMM yyyy'),
104+
delayUntilTime: dateTimeNow.toPlumberFormat('HH:mm'),
107105
}
108106
} else {
109107
throw new StepError(

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@/types/luxon-extensions'
2+
13
import { IGlobalVariable } from '@plumber/types'
24

35
import { Settings as LuxonSettings } from 'luxon'

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@/types/luxon-extensions'
2+
13
import { DateTime, Settings as LuxonSettings } from 'luxon'
24
import { describe, expect, it } from 'vitest'
35

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

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@/types/luxon-extensions'
2+
13
import { IGlobalVariable } from '@plumber/types'
24

35
import { Settings as LuxonSettings } from 'luxon'

packages/backend/src/apps/formatter/actions/date-time/common/date-time-format.ts

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,8 @@ const formatConverters = Object.assign({
5858
// en-US parsing failed, fall back to en-SG.
5959
return DateTime.fromFormat(input, 'dd MMM yyyy')
6060
},
61-
stringify: (dateTime: DateTime): string => dateTime.toFormat('dd MMM yyyy'),
61+
stringify: (dateTime: DateTime): string =>
62+
dateTime.toPlumberFormat('dd MMM yyyy'),
6263
},
6364
...Object.fromEntries(
6465
commonDateFormats
@@ -67,9 +68,19 @@ const formatConverters = Object.assign({
6768
format,
6869
{
6970
description: format,
70-
parse: (input: string): DateTime =>
71-
DateTime.fromFormat(input, format),
72-
stringify: (dateTime: DateTime): string => dateTime.toFormat(format),
71+
parse: (input: string): DateTime => {
72+
const result = DateTime.fromFormat(input, format, {
73+
locale: 'en-US',
74+
})
75+
if (result.isValid) {
76+
return result
77+
}
78+
return DateTime.fromFormat(input, format, {
79+
locale: 'en-SG',
80+
})
81+
},
82+
stringify: (dateTime: DateTime): string =>
83+
dateTime.toPlumberFormat(format),
7384
},
7485
]),
7586
),
@@ -123,6 +134,12 @@ export function parseDateTime(
123134
): DateTime {
124135
const result = formatConverters[dateTimeFormat].parse(valueToTransform)
125136

137+
if (dateTimeFormat === 'dd LLL yyyy' && !result.isValid) {
138+
result = formatConverters[dateTimeFormat].parse(valueToTransform, {
139+
locale: 'en-US',
140+
})
141+
}
142+
126143
if (!result.isValid) {
127144
throw new Error(
128145
`${valueToTransform}' is not a valid ${formatConverters[dateTimeFormat].description}`,

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

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

3-
import { DateTime } from 'luxon'
43
import { ZodError } from 'zod'
54

65
import StepError, { GenericSolution } from '@/errors/step'
@@ -36,22 +35,6 @@ function getParams($: IGlobalVariable) /* inferred return type */ {
3635
}
3736
}
3837

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-
5538
export const spec = {
5639
id: 'formatDateTime',
5740

@@ -67,13 +50,13 @@ export const spec = {
6750
const { dateTimeFormat, formatString } = getParams($)
6851
const dateTime = parseDateTime(dateTimeFormat, valueToTransform)
6952

70-
// check if format is dd LLL yyyy, if so, force en-US to maintain consistency with FormSG
7153
$.setActionItem({
7254
raw: {
73-
result: formatStringToUSLocale(formatString, dateTime),
55+
result: dateTime.toPlumberFormat(formatString),
7456
},
7557
})
7658
} catch (error) {
59+
console.log('error', error)
7760
if (error instanceof StepError) {
7861
throw error
7962
}

packages/backend/src/apps/paysg/actions/create-payment/schema.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -172,7 +172,7 @@ export const requestSchema = z
172172
description: data.description,
173173
amount_in_cents: data.paymentAmountCents,
174174
metadata: data.metadata ?? {}, // PaySG requires at least an empty metadata object.
175-
due_date: data.dueDate?.toFormat('dd-MMM-yyyy')?.toUpperCase(),
175+
due_date: data.dueDate?.toPlumberFormat('dd-MMM-yyyy')?.toUpperCase(),
176176
return_url: data.returnUrl,
177177
}))
178178

packages/backend/src/apps/scheduler/__tests__/common/get-date-time-object.test.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
import '@/types/luxon-extensions'
2+
13
import { DateTime } from 'luxon'
24
import { describe, expect, it } from 'vitest'
35

packages/backend/src/apps/scheduler/common/get-date-time-object.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,11 +8,9 @@ export default function getDateTimeObjectRepresentation(dateTime: DateTime) {
88
return {
99
...defaults,
1010
ISO_date_time: dateTime.toISO(),
11-
pretty_date: dateTime.toFormat('dd MMM yyyy', {
12-
locale: 'en-US', // Force en-US to maintain consistency with FormSG
13-
}),
11+
pretty_date: dateTime.toPlumberFormat('dd MMM yyyy'),
1412
pretty_time: dateTime.toLocaleString(DateTime.TIME_WITH_SECONDS),
15-
pretty_day_of_week: dateTime.toFormat('cccc'),
13+
pretty_day_of_week: dateTime.toPlumberFormat('cccc'),
1614
day_of_week: dateTime.weekday,
1715
} as IJSONObject
1816
}

0 commit comments

Comments
 (0)