Skip to content

Commit

Permalink
feat(TopUpCard): add startedAt field
Browse files Browse the repository at this point in the history
  • Loading branch information
keellyp committed May 31, 2024
1 parent a810978 commit 6ad36a8
Show file tree
Hide file tree
Showing 8 changed files with 269 additions and 120 deletions.
1 change: 1 addition & 0 deletions ditto/base.json
Original file line number Diff line number Diff line change
Expand Up @@ -760,6 +760,7 @@
"text_6657cdd8cea6bf010e1ce128": "Fixed top-up",
"text_66584178ee91f801012606a6": "Target ongoing balance should be higher than threshold",
"text_66584178ee91f801012606ac": "Threshold should be lower than target ongoing balance",
"text_66599bfb69fba1010535c5c2": "Start on",
"text_64aeb7b998c4322918c84204": "Payment methods",
"text_64aeb7b998c4322918c84208": "Card",
"text_64aeb7b998c4322918c8420c": "SEPA Direct Debit",
Expand Down
14 changes: 11 additions & 3 deletions src/components/wallets/WalletCodeSnippet.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ curl --location --request ${isEdition ? 'PUT' : 'POST'} "${apiUrl}/api/v1/wallet
"lago_id": "${recurringTransactionRules[0].lagoId}",`
: ''
}
"method": "${wallet.recurringTransactionRules?.[0].method || '__MUST_BE_DEFINED__'}"${
"method": "${wallet.recurringTransactionRules?.[0].method || '__MUST_BE_DEFINED__'}",${
wallet.recurringTransactionRules?.[0].method === RecurringTransactionMethodEnum.Fixed
? `
"paid_credits": "${
Expand All @@ -83,7 +83,7 @@ curl --location --request ${isEdition ? 'PUT' : 'POST'} "${apiUrl}/api/v1/wallet
? `
"target_ongoing_balance": "${
wallet.recurringTransactionRules?.[0].targetOngoingBalance || '0'
}"`
}",`
: ''
}
"trigger": "${wallet.recurringTransactionRules?.[0].trigger || '__MUST_BE_DEFINED__'}"${
Expand All @@ -92,15 +92,23 @@ curl --location --request ${isEdition ? 'PUT' : 'POST'} "${apiUrl}/api/v1/wallet
? `,
"interval": "${
wallet.recurringTransactionRules?.[0].interval || '__MUST_BE_DEFINED__'
}"`
}",`
: ''
}${
wallet.recurringTransactionRules?.[0].trigger ===
RecurringTransactionTriggerEnum.Threshold
? `,
"threshold_credits": "${wallet.recurringTransactionRules?.[0].thresholdCredits || '0'}"`
: ''
}${
wallet.recurringTransactionRules?.[0].trigger ===
RecurringTransactionTriggerEnum.Interval &&
wallet.recurringTransactionRules?.[0].startedAt
? `
"started_at": "${wallet.recurringTransactionRules?.[0].startedAt}"`
: ''
}
}
]`
: ''
}
Expand Down
83 changes: 53 additions & 30 deletions src/components/wallets/__tests__/utils.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,6 @@ import {
} from '~/generated/graphql'
import { TWalletDataForm } from '~/pages/WalletForm/types'

const formatOptions: Intl.DateTimeFormatOptions = {
weekday: undefined,
year: 'numeric',
month: 'long',
day: 'numeric',
}

describe('Wallet Utils', () => {
describe('toNumber', () => {
it('should return 0 when the value is undefined', () => {
Expand Down Expand Up @@ -60,64 +53,94 @@ describe('Wallet Utils', () => {

it('should return the date reference for the customer timezone in French', () => {
expect(
getDateRef(TimezoneEnum.TzEuropeParis, 'fr').set({ year: 2024, month: 5, day: 28 })
.weekdayLong,
getDateRef(TimezoneEnum.TzEuropeParis, 'fr').set({
year: 2024,
month: 5,
day: 28,
}).weekdayLong,
).toBe('mardi')
})
})

describe('getNextRecurringDate', () => {
it('should return the next weekly date', () => {
const date = new Date()
beforeEach(() => {
const expectedNow = DateTime.local(2024, 5, 5)

Settings.now = () => expectedNow.toMillis()
})

it('Weekly - should return May 12nd when current date is May 5th', () => {
expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Weekly,
}),
).toBe(new Date(date.setDate(date.getDate() + 7)).toLocaleDateString('en-US', formatOptions))
).toBe('May 12, 2024')
})

it('should return the next monthly date', () => {
const date = new Date()
it('Weekly - should return June 4th when current date is May 28th', () => {
const expectedNow = DateTime.local(2024, 5, 28)

Settings.now = () => expectedNow.toMillis()

expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Weekly,
}),
).toBe('June 4, 2024')
})

it('Monthly - should return June 5th when current date is May 5th', () => {
expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Monthly,
}),
).toBe(
new Date(date.setMonth(date.getMonth() + 1)).toLocaleDateString('en-US', formatOptions),
)
).toBe('June 5, 2024')
})

it('should return the next quarterly date', () => {
const date = new Date()
it('Monthly - should return Feb 29th when current date is January 31st', () => {
const expectedNow = DateTime.local(2024, 1, 31)

Settings.now = () => expectedNow.toMillis()
expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Monthly,
}),
).toBe('February 29, 2024')
})

it('Quarterly - should return Aug 5th when current date is May 5th', () => {
expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Quarterly,
}),
).toBe(
new Date(date.setMonth(date.getMonth() + 3)).toLocaleDateString('en-US', formatOptions),
)
).toBe('August 5, 2024')
})

it('Yearly - should return May 5, 2025 when current date is May 5, 2024', () => {
expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Yearly,
}),
).toBe('May 5, 2025')
})

it('should return the next yearly date', () => {
const date = new Date()
it('Yearly - should return Feb 28, 2025 when current date is Feb 29, 2024', () => {
const expectedNow = DateTime.local(2024, 2, 29)

Settings.now = () => expectedNow.toMillis()

expect(
getNextRecurringDate({
timezone: TimezoneEnum.TzEuropeParis,
interval: RecurringTransactionIntervalEnum.Yearly,
}),
).toBe(
new Date(date.setFullYear(date.getFullYear() + 1)).toLocaleDateString(
'en-US',
formatOptions,
),
)
).toBe('February 28, 2025')
})
})

Expand Down
39 changes: 27 additions & 12 deletions src/components/wallets/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,30 +48,37 @@ export const getDateRef = (
export const getNextRecurringDate = ({
timezone,
interval,
date,
}: {
timezone: TGetWordingForWalletAlert['customerTimezone']
interval?: RecurringTransactionIntervalEnum | null
}) => {
let date = null
date?: DateTime
}): string => {
let nextRecurringDate = null
const dateRef = getDateRef(timezone).set({
day: date?.day,
month: date?.month,
year: date?.year,
})

switch (interval) {
case RecurringTransactionIntervalEnum.Weekly:
date = getDateRef(timezone).plus({ days: 7 }).toLocaleString(DateTime.DATE_FULL)
nextRecurringDate = dateRef.plus({ days: 7 })
break
case RecurringTransactionIntervalEnum.Monthly:
date = getDateRef(timezone).plus({ months: 1 }).toLocaleString(DateTime.DATE_FULL)
nextRecurringDate = dateRef.plus({ months: 1 })
break
case RecurringTransactionIntervalEnum.Quarterly:
date = getDateRef(timezone).plus({ months: 3 }).toLocaleString(DateTime.DATE_FULL)
nextRecurringDate = dateRef.plus({ months: 3 })
break
case RecurringTransactionIntervalEnum.Yearly:
date = getDateRef(timezone).plus({ years: 1 }).toLocaleString(DateTime.DATE_FULL)
nextRecurringDate = dateRef.plus({ years: 1 })
break
default:
break
}

return date ?? ''
return nextRecurringDate?.toLocaleString(DateTime.DATE_FULL) ?? ''
}

const setStartOfSentence = ({
Expand All @@ -94,12 +101,13 @@ const setStartOfSentence = ({
),
})
} else {
const totalCreditCount =
toNumber(walletValues.recurringTransactionRules?.[0].paidCredits) +
toNumber(walletValues.recurringTransactionRules?.[0].grantedCredits)
const rrule = walletValues.recurringTransactionRules?.[0]

const totalCreditCount = toNumber(rrule?.paidCredits) + toNumber(rrule?.grantedCredits)
const nextRecurringTopUpDate = getNextRecurringDate({
timezone: customerTimezone,
interval: walletValues.recurringTransactionRules?.[0].interval,
interval: rrule?.interval,
date: rrule?.startedAt ? DateTime.fromISO(rrule?.startedAt) : undefined,
})

if (recurringRulesValues?.method === RecurringTransactionMethodEnum.Fixed) {
Expand Down Expand Up @@ -132,7 +140,14 @@ const setEndOfSentence = ({
let text = ''

if (recurringRulesValues?.trigger === RecurringTransactionTriggerEnum.Interval) {
const dateRef = getDateRef(customerTimezone)
const rrule = walletValues.recurringTransactionRules?.[0]
const startedAt = rrule?.startedAt ? DateTime.fromISO(rrule?.startedAt) : undefined

const dateRef = getDateRef(customerTimezone).set({
day: startedAt?.day,
month: startedAt?.month,
year: startedAt?.year,
})
const isDayPotentiallyNotReachableOnEntirePeriod = dateRef.day > MINIMUM_DAYS_IN_MONTH

switch (recurringRulesValues?.interval) {
Expand Down
Loading

0 comments on commit 6ad36a8

Please sign in to comment.