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 InvokeWebHookWorkflow from "./InvokeWebHookWorkflow.js"; import { IInvocation } from "./IInvocation.js"; import { AppWorkflowContext } from "../AppWorkflowContext.js"; export default class QueueInvokeWebHookWorkflow extends Workflow { static taskGroup = "daily"; static queue(context: AppWorkflowContext, input: IInvocation) { let id = "invoke-web-hook"; for (const key in input) { if (Object.hasOwn(input, key)) { const element = input[key]; id += `-${key}-${element}`; } } return context.queue(QueueInvokeWebHookWorkflow, input, { id }); } async run() { let start = 0; do { start = await this.getHooks(start); } while(BigInt(start)); } @UniqueActivity async getHooks( start: number, @Inject db?: SocialMailContext ) { const { input } = this; let q = db.webHooks.where(input, (x, p) => x.eventType === p.type); switch(input.type) { case "email-received": if (input.mailboxID) { if (input.emailAddressID) { q = q.where(input, (x, p) => x.mailboxID === p.mailboxID && x.emailAddressID === p.emailAddressID); } else { q = q.where(input, (x, p) => x.mailboxID === p.mailboxID); } } break; case "order-paid": if (input.storeItemID) { q = q.where(input, (x, p) => x.storeItemID === p.storeItemID); } if (input.storeID) { q = q.where(input, (x, p) => x.storeID === p.storeID); } break; } const items = await q.where({ start }, (x, p) => x.webHookID > p.start) .orderBy((x) => x.webHookID) .limit(10) .toArray(); if (!items.length) { return 0; } for (const hook of items) { start = hook.webHookID; await InvokeWebHookWorkflow.push(this.context, hook, this.input); } return start; } }