-
Notifications
You must be signed in to change notification settings - Fork 3.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
integrate payment card section with API #43473
Changes from 4 commits
1601259
049a75c
b6fe126
abe1923
08e6547
2d624ca
59b2bf5
e1b6425
134e433
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change | ||||
---|---|---|---|---|---|---|
@@ -0,0 +1,30 @@ | ||||||
import {addDays, addMonths, differenceInMonths, format, isValid} from 'date-fns'; | ||||||
import CONST from '@src/CONST'; | ||||||
|
||||||
/** | ||||||
* Get the next billing date. | ||||||
* | ||||||
* @param initialDate - The initial billing date in 'yyyy-MM-dd' format. | ||||||
* @returns - The next billing date in 'yyyy-MM-dd' format. | ||||||
*/ | ||||||
function getNextBillingDate(initialDate: string): string { | ||||||
let start = new Date(`${initialDate}T00:00:00`); | ||||||
|
||||||
if (!isValid(start)) { | ||||||
start = new Date(); | ||||||
} | ||||||
|
||||||
const today = new Date(); | ||||||
|
||||||
const monthsDiff = differenceInMonths(today, start); | ||||||
|
||||||
let nextBillingDate = addDays(addMonths(start, monthsDiff), 1); | ||||||
|
||||||
if (nextBillingDate.toUTCString() < today.toUTCString()) { | ||||||
nextBillingDate = addMonths(nextBillingDate, 1); | ||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Is this fix to always ensure that the nextBillingDate is in the future? In this case should this be
Suggested change
? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes, but this update is not doing same as now In this case, we will have today's as a billing date |
||||||
} | ||||||
|
||||||
return format(nextBillingDate, CONST.DATE.MONTH_DAY_YEAR_FORMAT); | ||||||
} | ||||||
|
||||||
export default getNextBillingDate; |
Original file line number | Diff line number | Diff line change | ||||||
---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,40 @@ | ||||||||
import getNextBillingDate from '@src/pages/settings/Subscription/CardSection/utils'; | ||||||||
|
||||||||
describe('getNextBillingDate', () => { | ||||||||
beforeAll(() => { | ||||||||
jest.useFakeTimers(); | ||||||||
jest.setSystemTime(new Date(2024, 6, 5)); | ||||||||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. NAB, maybe we can add a comment that month is zero-indexed? It tripped me up for a sec 😄 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Done! CC: @blimpich There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Where is the comment? I think what Amy meant was something like this
Suggested change
|
||||||||
}); | ||||||||
|
||||||||
afterAll(() => { | ||||||||
jest.useRealTimers(); | ||||||||
}); | ||||||||
|
||||||||
it('should return the next billing date when initial date is valid', () => { | ||||||||
const initialDate = '2024-06-01'; | ||||||||
const expectedNextBillingDate = 'July 2, 2024'; | ||||||||
|
||||||||
expect(getNextBillingDate(initialDate)).toEqual(expectedNextBillingDate); | ||||||||
}); | ||||||||
|
||||||||
it('should handle end-of-month edge cases correctly', () => { | ||||||||
const initialDate = '2024-01-31'; | ||||||||
const nextBillingDate = getNextBillingDate(initialDate); | ||||||||
const expectedNextBillingDate = 'July 1, 2024'; | ||||||||
expect(nextBillingDate).toBe(expectedNextBillingDate); | ||||||||
}); | ||||||||
|
||||||||
it('should handle date when it at the current month', () => { | ||||||||
const initialDate = '2024-06-06'; | ||||||||
const nextBillingDate = getNextBillingDate(initialDate); | ||||||||
const expectedNextBillingDate = 'June 7, 2024'; | ||||||||
expect(nextBillingDate).toBe(expectedNextBillingDate); | ||||||||
}); | ||||||||
|
||||||||
it('should return the next billing date when initial date is invalid', () => { | ||||||||
const initialDate = 'invalid-date'; | ||||||||
const expectedNextBillingDate = 'July 6, 2024'; | ||||||||
|
||||||||
expect(getNextBillingDate(initialDate)).toEqual(expectedNextBillingDate); | ||||||||
}); | ||||||||
}); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
what do you think of this to get rid of
while
loop.cc - @amyevans
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah if we can eliminate a loop that would be great!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
updated