{"version":3,"file":"dispatch-scheduled-publish-operations.mjs","names":[],"sources":["../../../src/services/crons/dispatch-scheduled-publish-operations.ts"],"sourcesContent":["import { DocumentPublishOperationsRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport { schedulingDispatchWindowMs } from \"../document-publish-operations/helpers/index.js\";\nimport scheduleApproved from \"../document-publish-operations/schedule-approved.js\";\n\nconst dispatchScheduledPublishOperations: ServiceFn<[], undefined> = async (\n\tcontext,\n) => {\n\tif (!context.queue.support.scheduling) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst Operations = new DocumentPublishOperationsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\tconst dispatchBefore = new Date(\n\t\tDate.now() + schedulingDispatchWindowMs,\n\t).toISOString();\n\n\tconst operationsRes = await Operations.selectMultiple({\n\t\tselect: [\"id\"],\n\t\twhere: [\n\t\t\t{ key: \"status\", operator: \"=\", value: \"approved\" },\n\t\t\t{ key: \"execution_status\", operator: \"=\", value: \"scheduled\" },\n\t\t\t{ key: \"scheduled_job_id\", operator: \"is\", value: null },\n\t\t\t{ key: \"scheduled_at\", operator: \"<=\", value: dispatchBefore },\n\t\t],\n\t});\n\tif (operationsRes.error) return operationsRes;\n\n\tconst scheduleResults = await Promise.all(\n\t\t(operationsRes.data ?? []).map((operation) =>\n\t\t\tscheduleApproved(context, {\n\t\t\t\tid: operation.id,\n\t\t\t\teventType: \"scheduled\",\n\t\t\t}),\n\t\t),\n\t);\n\n\tfor (const scheduleRes of scheduleResults) {\n\t\tif (scheduleRes.error) return scheduleRes;\n\t}\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default dispatchScheduledPublishOperations;\n"],"mappings":"yOAKA,MAAM,EAA+D,KACpE,IACI,CACJ,GAAI,CAAC,EAAQ,MAAM,QAAQ,WAC1B,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAa,IAAI,EACtB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EACM,EAAiB,IAAI,KAC1B,KAAK,IAAI,EAAI,CACd,CAAC,CAAC,YAAY,EAER,EAAgB,MAAM,EAAW,eAAe,CACrD,OAAQ,CAAC,IAAI,EACb,MAAO,CACN,CAAE,IAAK,SAAU,SAAU,IAAK,MAAO,UAAW,EAClD,CAAE,IAAK,mBAAoB,SAAU,IAAK,MAAO,WAAY,EAC7D,CAAE,IAAK,mBAAoB,SAAU,KAAM,MAAO,IAAK,EACvD,CAAE,IAAK,eAAgB,SAAU,KAAM,MAAO,CAAe,CAC9D,CACD,CAAC,EACD,GAAI,EAAc,MAAO,OAAO,EAEhC,IAAM,EAAkB,MAAM,QAAQ,KACpC,EAAc,MAAQ,CAAC,EAAA,CAAG,IAAK,GAC/B,EAAiB,EAAS,CACzB,GAAI,EAAU,GACd,UAAW,WACZ,CAAC,CACF,CACD,EAEA,IAAK,IAAM,KAAe,EACzB,GAAI,EAAY,MAAO,OAAO,EAG/B,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}