-
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 3 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,25 @@ | ||
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 current = new Date(start); | ||
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. 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. Problem fixed 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. I raised one more comment. 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. I haven't reviewed the function logic closely but just glancing at the expected next billing dates in the test, I wanted to clarify expected behavior... the next billing date should always be the first of the next month (internal ref) 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. I don't have the access to the link, but does that mean we don't even need the
? 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 we can simplify to that, apologies for not catching it sooner! 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. No worries. Better we caught this now. 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. @pasyukevich Can you take care of this 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. Updated |
||
|
||
const monthsDiff = differenceInMonths(current, start); | ||
const nextBillingDate = addDays(addMonths(start, monthsDiff + 1), 1); | ||
|
||
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,24 @@ | ||
import {addDays, addMonths, format} from 'date-fns'; | ||
import getNextBillingDate from '@src/pages/settings/Subscription/CardSection/utils'; | ||
|
||
describe('getNextBillingDate', () => { | ||
it('should return the next billing date one month after the initial date', () => { | ||
const endDate = '2023-01-15'; | ||
const nextBillingDate = getNextBillingDate(endDate); | ||
expect(nextBillingDate).toBe('February 16, 2023'); | ||
}); | ||
|
||
it('should handle end-of-month edge cases correctly', () => { | ||
const initialDate = '2023-01-31'; | ||
const nextBillingDate = getNextBillingDate(initialDate); | ||
const expectedNextBillingDate = format(addDays(addMonths(new Date('2023-01-31'), 1), 1), 'MMMM d, yyyy'); | ||
expect(nextBillingDate).toBe(expectedNextBillingDate); | ||
}); | ||
|
||
it('should handle invalid dates correctly', () => { | ||
const initialDate = 'invalid date'; | ||
const nextBillingDate = getNextBillingDate(initialDate); | ||
const expectedNextBillingDate = format(addDays(addMonths(new Date(), 1), 1), 'MMMM d, yyyy'); | ||
expect(nextBillingDate).toBe(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