UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

2 lines 2.4 kB
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../libs/repositories/document-publish-operations.mjs";import n from"../../libs/repositories/queue-jobs.mjs";import{isInSchedulingDispatchWindow as r,publishOperationExecuteEvent as i}from"./helpers/index.mjs";import a from"./helpers/create-event.mjs";import o from"./execute.mjs";const s=async(s,c)=>{let l=new t(s.db.client,s.config.db),u=new n(s.db.client,s.config.db),d=await l.selectSingle({select:[`id`,`collection_key`,`document_id`,`target`,`status`,`scheduled_at`,`scheduled_timezone`,`scheduled_job_id`,`requested_by`,`decided_by`,`tenant_key`],where:[{key:`id`,operator:`=`,value:c.id}],validation:{enabled:!0,defaultError:{message:e(`server:core.publish.operations.not.found`),status:404}}});if(d.error)return d;if(d.data.status!==`approved`)return{error:void 0,data:void 0};d.data.scheduled_job_id&&await u.updateSingle({where:[{key:`job_id`,operator:`=`,value:d.data.scheduled_job_id}],data:{status:`cancelled`,updated_at:new Date().toISOString()}});let f=c.userId??d.data.decided_by??d.data.requested_by??null,p=d.data.scheduled_at?new Date(d.data.scheduled_at):null,m=new Date;if(p&&p.getTime()>m.getTime()){let t=null;if(r({scheduledAt:p,now:m})){if(!s.queue.support.scheduling)return{error:{type:`basic`,message:e(`server:core.publish.operations.schedule.not.supported`),status:400},data:void 0};let n=await s.queue.add(s,{event:i,payload:{operationId:d.data.id,tenantKey:d.data.tenant_key},options:{scheduledFor:p,createdByUserId:f??void 0,tenantKeys:d.data.tenant_key?[d.data.tenant_key]:void 0}});if(n.error)return n;t=n.data.jobId}let n=await l.updateSingle({where:[{key:`id`,operator:`=`,value:d.data.id}],data:{execution_status:`scheduled`,scheduled_job_id:t,execution_error_message:null,execution_error_data:null,failed_at:null,updated_at:new Date().toISOString()}});if(n.error)return n;let o=await a(s,{operation:d.data,event:{type:c.eventType??`scheduled`,userId:f,comment:null,metadata:{scheduledAt:p.toISOString(),scheduledTimezone:d.data.scheduled_timezone,scheduledJobId:t}}});return o.error?o:{error:void 0,data:void 0}}let h=await l.updateSingle({where:[{key:`id`,operator:`=`,value:d.data.id}],data:{scheduled_job_id:null,updated_at:new Date().toISOString()}});return h.error?h:o(s,{id:d.data.id,userId:f,markFailedOnError:!0})};export{s as default}; //# sourceMappingURL=schedule-approved.mjs.map