Skip to content

Commit 491961f

Browse files
committed
feat(paymaster): add paidUntil recharge logic
1 parent a6499c5 commit 491961f

2 files changed

Lines changed: 33 additions & 1 deletion

File tree

lib/utils/hasValue.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
/**
22
* Returns true if specified value is not undefined, null and empty string
3+
*
34
* @param v - value to check
45
*/
56
export function hasValue<T>(v: T | undefined | null): v is T {

workers/paymaster/src/index.ts

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,22 @@ export default class PaymasterWorker extends Worker {
8080
return now >= expectedPayDay.getTime();
8181
}
8282

83+
/**
84+
* Method that returns date of the last charge date + 1 month
85+
*
86+
* It is used to recharge prepaid workspaces
87+
*
88+
* @param date - last charge date
89+
* @returns - last charge date + 1 month
90+
*/
91+
private static monthSinceLastChargeDate(date: Date): Date {
92+
const monthSinceLastChargeDate = new Date(date.getTime());
93+
94+
monthSinceLastChargeDate.setMonth(monthSinceLastChargeDate.getMonth() + 1);
95+
96+
return monthSinceLastChargeDate;
97+
}
98+
8399
/**
84100
* Returns difference between now and payday in days
85101
*
@@ -214,6 +230,11 @@ export default class PaymasterWorker extends Worker {
214230
// @ts-expect-error debug
215231
const isTimeToPay = PaymasterWorker.isTimeToPay(workspace.lastChargeDate, workspace.paidUntil, workspace.isDebug);
216232

233+
/**
234+
* Date one month after last charge date
235+
*/
236+
const monthSinceLastChargeDate: Date = PaymasterWorker.monthSinceLastChargeDate(workspace.lastChargeDate);
237+
217238
/**
218239
* How many days have passed since payments the expected day of payments
219240
*/
@@ -237,7 +258,17 @@ export default class PaymasterWorker extends Worker {
237258
*/
238259
if (!isTimeToPay) {
239260
/**
240-
* If workspace was manually unblocked (reset of billingPeriodEventsCount and lastChargeDate) in db
261+
* If month since last charge date passed, but it is still not time to pay - then workspace has prepaid period
262+
* Start new month - recharge billing period events count and update last charge date
263+
*/
264+
if (date >= monthSinceLastChargeDate) {
265+
await this.updateLastChargeDate(workspace, date);
266+
await this.clearBillingPeriodEventsCount(workspace);
267+
}
268+
269+
/**
270+
* If workspace is blocked, but it is not time to pay
271+
* This case could be reached by prepaid workspaces and manually recharged ones
241272
*/
242273
if (workspace.isBlocked) {
243274
await this.unblockWorkspace(workspace);

0 commit comments

Comments
 (0)