import Inject from "@entity-access/entity-access/dist/di/di.js"; import Workflow, { UniqueActivity } from "@entity-access/entity-access/dist/workflows/Workflow.js"; import SocialMailContext from "../../model/SocialMailContext.js"; import TimeSpan from "@entity-access/entity-access/dist/types/TimeSpan.js"; import StorePaymentGatewayService from "../../services/store/StorePaymentGatewayService.js"; import DateTime from "@entity-access/entity-access/dist/types/DateTime.js"; export default class FetchCheckoutResultWorkflow extends Workflow { static taskGroup = "batch"; async run() { this.preserveTime = TimeSpan.fromMinutes(5); let start = 0; for(;;) { start = await this.fetchResult(start); if (!BigInt(start)) { break; } } } @UniqueActivity async fetchResult( start: number, @Inject db?: SocialMailContext, @Inject spg?: StorePaymentGatewayService ) { const yesterday = DateTime.now.addDays(-7); for (let index = 0; index < 100; index++) { const items = await db.storePaymentAttempts.where({ start }, (x, p) => x.attemptID > p.start && x.status === null) .orderBy((x) => x.attemptID) .limit(10) .toArray(); if (!items.length) { return 0; } for (const item of items) { start = item.attemptID; const pmt = await spg.checkoutResult(item); if (pmt.status) { continue; } const started = DateTime.from(item.dateCreated); if (started.msSinceEpoch < yesterday.msSinceEpoch) { await db.storePaymentAttempts.statements.update({ url: null, content: null, status: "timeout" }, { attemptID: start }); } } db.changeSet.clear(); } return start; } }