Skip to content

Commit a50d52f

Browse files
committed
imp(paymaster): isTimeToRecharge added
1 parent 4784588 commit a50d52f

1 file changed

Lines changed: 29 additions & 23 deletions

File tree

workers/paymaster/src/index.ts

Lines changed: 29 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -60,40 +60,46 @@ export default class PaymasterWorker extends Worker {
6060
/**
6161
* Check if today is a payday for passed timestamp
6262
*
63-
* Pay day is calculated by formula: last charge date + 30 days
64-
*
6563
* @param date - last charge date
6664
* @param paidUntil - paid until date
6765
* @param isDebug - flag for debug purposes
6866
*/
69-
private static isTimeToPay(date: Date, paidUntil: Date, isDebug = false): boolean {
70-
const expectedPayDay = paidUntil ? new Date(paidUntil) : new Date(date);
67+
private static isTimeToPay(date: Date, paidUntil?: Date, isDebug = false): boolean {
68+
const expectedPayDay = paidUntil ? new Date(paidUntil) : this.composeBillingPeriodEndDate(date, isDebug);
69+
const now = new Date();
7170

72-
if (isDebug) {
73-
expectedPayDay.setDate(date.getDate() + 1);
74-
} else if (!paidUntil) {
75-
expectedPayDay.setMonth(date.getMonth() + 1);
76-
}
71+
return now >= expectedPayDay;
72+
}
7773

78-
const now = new Date().getTime();
74+
/**
75+
* Check if today is a recharge day for passed timestamp
76+
*
77+
* @param date - last charge date
78+
* @param isDebug - flag for debug purposes
79+
*/
80+
private static isTimeToRecharge(date: Date, isDebug = false): boolean {
81+
const nexTimeToRecharge = this.composeBillingPeriodEndDate(date, isDebug);
82+
const now = new Date();
7983

80-
return now >= expectedPayDay.getTime();
84+
return now >= nexTimeToRecharge;
8185
}
8286

8387
/**
84-
* Method that returns date of the last charge date + 1 month
85-
*
86-
* It is used to recharge prepaid workspaces
88+
* Returns the date - end of the billing period
8789
*
8890
* @param date - last charge date
89-
* @returns - last charge date + 1 month
91+
* @param isDebug - flag for debug workspaces
9092
*/
91-
private static monthSinceLastChargeDate(date: Date): Date {
92-
const monthSinceLastChargeDate = new Date(date.getTime());
93+
private static composeBillingPeriodEndDate(date: Date, isDebug = false): Date {
94+
const endDate = new Date(date);
9395

94-
monthSinceLastChargeDate.setMonth(monthSinceLastChargeDate.getMonth() + 1);
96+
if (isDebug) {
97+
endDate.setDate(date.getDate() + 1);
98+
} else {
99+
endDate.setMonth(date.getMonth() + 1);
100+
}
95101

96-
return monthSinceLastChargeDate;
102+
return endDate;
97103
}
98104

99105
/**
@@ -231,9 +237,9 @@ export default class PaymasterWorker extends Worker {
231237
const isTimeToPay = PaymasterWorker.isTimeToPay(workspace.lastChargeDate, workspace.paidUntil, workspace.isDebug);
232238

233239
/**
234-
* Date one month after last charge date
240+
* Is it time to recharge workspace limits
235241
*/
236-
const monthSinceLastChargeDate: Date = PaymasterWorker.monthSinceLastChargeDate(workspace.lastChargeDate);
242+
const isTimeToRecharge = PaymasterWorker.isTimeToRecharge(workspace.lastChargeDate, workspace.isDebug);
237243

238244
/**
239245
* How many days have passed since payments the expected day of payments
@@ -258,10 +264,10 @@ export default class PaymasterWorker extends Worker {
258264
*/
259265
if (!isTimeToPay) {
260266
/**
261-
* If month since last charge date passed, but it is still not time to pay - then workspace has prepaid period
267+
* If it is time to recharge workspace limits, but not time to pay
262268
* Start new month - recharge billing period events count and update last charge date
263269
*/
264-
if (date >= monthSinceLastChargeDate) {
270+
if (isTimeToRecharge) {
265271
await this.updateLastChargeDate(workspace, date);
266272
await this.clearBillingPeriodEventsCount(workspace);
267273
}

0 commit comments

Comments
 (0)