{"version":3,"file":"schedule-approved.mjs","names":[],"sources":["../../../src/services/document-publish-operations/schedule-approved.ts"],"sourcesContent":["import { copy } from \"../../libs/i18n/index.js\";\nimport {\n\tDocumentPublishOperationsRepository,\n\tQueueJobsRepository,\n} from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport execute from \"./execute.js\";\nimport createEvent from \"./helpers/create-event.js\";\nimport {\n\tisInSchedulingDispatchWindow,\n\tpublishOperationExecuteEvent,\n} from \"./helpers/index.js\";\n\nconst scheduleApproved: ServiceFn<\n\t[\n\t\t{\n\t\t\tid: number;\n\t\t\tuserId?: number | null;\n\t\t\teventType?: \"scheduled\" | \"rescheduled\" | \"retried\";\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tconst Operations = new DocumentPublishOperationsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\tconst QueueJobs = new QueueJobsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst operationRes = await Operations.selectSingle({\n\t\tselect: [\n\t\t\t\"id\",\n\t\t\t\"collection_key\",\n\t\t\t\"document_id\",\n\t\t\t\"target\",\n\t\t\t\"status\",\n\t\t\t\"scheduled_at\",\n\t\t\t\"scheduled_timezone\",\n\t\t\t\"scheduled_job_id\",\n\t\t\t\"requested_by\",\n\t\t\t\"decided_by\",\n\t\t\t\"tenant_key\",\n\t\t],\n\t\twhere: [{ key: \"id\", operator: \"=\", value: data.id }],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t\tdefaultError: {\n\t\t\t\tmessage: copy(\"server:core.publish.operations.not.found\"),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t},\n\t});\n\tif (operationRes.error) return operationRes;\n\n\tif (operationRes.data.status !== \"approved\") {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tif (operationRes.data.scheduled_job_id) {\n\t\tawait QueueJobs.updateSingle({\n\t\t\twhere: [\n\t\t\t\t{\n\t\t\t\t\tkey: \"job_id\",\n\t\t\t\t\toperator: \"=\",\n\t\t\t\t\tvalue: operationRes.data.scheduled_job_id,\n\t\t\t\t},\n\t\t\t],\n\t\t\tdata: {\n\t\t\t\tstatus: \"cancelled\",\n\t\t\t\tupdated_at: new Date().toISOString(),\n\t\t\t},\n\t\t});\n\t}\n\n\tconst actorUserId =\n\t\tdata.userId ??\n\t\toperationRes.data.decided_by ??\n\t\toperationRes.data.requested_by ??\n\t\tnull;\n\tconst scheduledAt = operationRes.data.scheduled_at\n\t\t? new Date(operationRes.data.scheduled_at)\n\t\t: null;\n\tconst now = new Date();\n\n\tif (scheduledAt && scheduledAt.getTime() > now.getTime()) {\n\t\tlet scheduledJobId: string | null = null;\n\n\t\tif (isInSchedulingDispatchWindow({ scheduledAt, now })) {\n\t\t\tif (!context.queue.support.scheduling) {\n\t\t\t\treturn {\n\t\t\t\t\terror: {\n\t\t\t\t\t\ttype: \"basic\",\n\t\t\t\t\t\tmessage: copy(\n\t\t\t\t\t\t\t\"server:core.publish.operations.schedule.not.supported\",\n\t\t\t\t\t\t),\n\t\t\t\t\t\tstatus: 400,\n\t\t\t\t\t},\n\t\t\t\t\tdata: undefined,\n\t\t\t\t};\n\t\t\t}\n\n\t\t\tconst queueRes = await context.queue.add(context, {\n\t\t\t\tevent: publishOperationExecuteEvent,\n\t\t\t\tpayload: {\n\t\t\t\t\toperationId: operationRes.data.id,\n\t\t\t\t\ttenantKey: operationRes.data.tenant_key,\n\t\t\t\t},\n\t\t\t\toptions: {\n\t\t\t\t\tscheduledFor: scheduledAt,\n\t\t\t\t\tcreatedByUserId: actorUserId ?? undefined,\n\t\t\t\t\ttenantKeys: operationRes.data.tenant_key\n\t\t\t\t\t\t? [operationRes.data.tenant_key]\n\t\t\t\t\t\t: undefined,\n\t\t\t\t},\n\t\t\t});\n\t\t\tif (queueRes.error) return queueRes;\n\t\t\tscheduledJobId = queueRes.data.jobId;\n\t\t}\n\n\t\tconst updateRes = await Operations.updateSingle({\n\t\t\twhere: [{ key: \"id\", operator: \"=\", value: operationRes.data.id }],\n\t\t\tdata: {\n\t\t\t\texecution_status: \"scheduled\",\n\t\t\t\tscheduled_job_id: scheduledJobId,\n\t\t\t\texecution_error_message: null,\n\t\t\t\texecution_error_data: null,\n\t\t\t\tfailed_at: null,\n\t\t\t\tupdated_at: new Date().toISOString(),\n\t\t\t},\n\t\t});\n\t\tif (updateRes.error) return updateRes;\n\n\t\tconst eventRes = await createEvent(context, {\n\t\t\toperation: operationRes.data,\n\t\t\tevent: {\n\t\t\t\ttype: data.eventType ?? \"scheduled\",\n\t\t\t\tuserId: actorUserId,\n\t\t\t\tcomment: null,\n\t\t\t\tmetadata: {\n\t\t\t\t\tscheduledAt: scheduledAt.toISOString(),\n\t\t\t\t\tscheduledTimezone: operationRes.data.scheduled_timezone,\n\t\t\t\t\tscheduledJobId,\n\t\t\t\t},\n\t\t\t},\n\t\t});\n\t\tif (eventRes.error) return eventRes;\n\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst clearScheduleRes = await Operations.updateSingle({\n\t\twhere: [{ key: \"id\", operator: \"=\", value: operationRes.data.id }],\n\t\tdata: {\n\t\t\tscheduled_job_id: null,\n\t\t\tupdated_at: new Date().toISOString(),\n\t\t},\n\t});\n\tif (clearScheduleRes.error) return clearScheduleRes;\n\n\treturn execute(context, {\n\t\tid: operationRes.data.id,\n\t\tuserId: actorUserId,\n\t\tmarkFailedOnError: true,\n\t});\n};\n\nexport default scheduleApproved;\n"],"mappings":"yVAaA,MAAM,EASF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAa,IAAI,EACtB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EACM,EAAY,IAAI,EACrB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAe,MAAM,EAAW,aAAa,CAClD,OAAQ,CACP,KACA,iBACA,cACA,SACA,SAC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