@@ -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