@@ -60,24 +60,47 @@ 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 ( ) ;
70+
71+ return now >= expectedPayDay ;
72+ }
73+
74+ /**
75+ * Check if today is a recharge day for passed timestamp
76+ * It equals to the isTimeToPay in all cases except prePaid workspaces
77+ *
78+ * @param date - last charge date
79+ * @param isDebug - flag for debug purposes
80+ */
81+ private static isTimeToRecharge ( date : Date , isDebug = false ) : boolean {
82+ const nexTimeToRecharge = this . composeBillingPeriodEndDate ( date , isDebug ) ;
83+ const now = new Date ( ) ;
84+
85+ return now >= nexTimeToRecharge ;
86+ }
87+
88+ /**
89+ * Returns the date - end of the billing period
90+ *
91+ * @param date - last charge date
92+ * @param isDebug - flag for debug workspaces
93+ */
94+ private static composeBillingPeriodEndDate ( date : Date , isDebug = false ) : Date {
95+ const endDate = new Date ( date ) ;
7196
7297 if ( isDebug ) {
73- expectedPayDay . setDate ( date . getDate ( ) + 1 ) ;
74- } else if ( ! paidUntil ) {
75- expectedPayDay . setMonth ( date . getMonth ( ) + 1 ) ;
98+ endDate . setDate ( date . getDate ( ) + 1 ) ;
99+ } else {
100+ endDate . setMonth ( date . getMonth ( ) + 1 ) ;
76101 }
77102
78- const now = new Date ( ) . getTime ( ) ;
79-
80- return now >= expectedPayDay . getTime ( ) ;
103+ return endDate ;
81104 }
82105
83106 /**
@@ -214,6 +237,12 @@ export default class PaymasterWorker extends Worker {
214237 // @ts -expect-error debug
215238 const isTimeToPay = PaymasterWorker . isTimeToPay ( workspace . lastChargeDate , workspace . paidUntil , workspace . isDebug ) ;
216239
240+ /**
241+ * Is it time to recharge workspace limits
242+ */
243+ // @ts -expect-error debug
244+ const isTimeToRecharge = PaymasterWorker . isTimeToRecharge ( workspace . lastChargeDate , workspace . isDebug ) ;
245+
217246 /**
218247 * How many days have passed since payments the expected day of payments
219248 */
@@ -237,7 +266,17 @@ export default class PaymasterWorker extends Worker {
237266 */
238267 if ( ! isTimeToPay ) {
239268 /**
240- * If workspace was manually unblocked (reset of billingPeriodEventsCount and lastChargeDate) in db
269+ * If it is time to recharge workspace limits, but not time to pay
270+ * Start new month - recharge billing period events count and update last charge date
271+ */
272+ if ( isTimeToRecharge ) {
273+ await this . updateLastChargeDate ( workspace , date ) ;
274+ await this . clearBillingPeriodEventsCount ( workspace ) ;
275+ }
276+
277+ /**
278+ * If workspace is blocked, but it is not time to pay
279+ * This case could be reached by prepaid workspaces and manually recharged ones
241280 */
242281 if ( workspace . isBlocked ) {
243282 await this . unblockWorkspace ( workspace ) ;
0 commit comments