@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2.68 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../libs/repositories/document-publish-operations.mjs";import n from"../collections/get-single-instance.mjs";import{collectionTargetSupportsScheduling as r,hasCollectionPermission as i,parseScheduleInput as a,unresolvedPublishOperationExecutionStatuses as o}from"./helpers/index.mjs";import s from"./helpers/create-event.mjs";import c from"./schedule-approved.mjs";const l=async(l,u)=>{let d=new t(l.db.client,l.config.db),f=a({scheduledAt:u.scheduledAt,scheduledTimezone:u.scheduledTimezone});if(f.error)return{error:f.error,data:void 0};let p=f.data,m=await d.selectSingleDetailed({tenantKey:l.request.tenantKey,where:[{key:`lucid_document_publish_operations.id`,operator:`=`,value:u.id}]});if(m.error)return m;if(!m.data||![`pending`,`approved`].includes(m.data.status)||!o.some(e=>e===m.data?.execution_status))return{error:{type:`basic`,message:e(`server:core.publish.operations.not.reschedulable`),status:400},data:void 0};let h=n(l,{key:m.data.collection_key});if(h.error)return h;let g=m.data.requested_by===u.user.id,_=m.data.status===`approved`&&m.data.operation_type===`direct`?`publish`:`review`,v=i({user:u.user,collection:h.data,action:_});if(m.data.status===`pending`&&!g&&!v)return{error:{type:`basic`,name:e(`server:core.collections.permission.error.name`),message:e(`server:core.collections.permission.error.message`,{data:{collection:m.data.collection_key,action:`review`}}),status:403},data:void 0};if(m.data.status===`approved`&&!v)return{error:{type:`basic`,name:e(`server:core.collections.permission.error.name`),message:e(`server:core.collections.permission.error.message`,{data:{collection:m.data.collection_key,action:_}}),status:403},data:void 0};if(p?.scheduledAt&&!r({collection:h.data,target:m.data.target,queueSupportsScheduling:l.queue.support.scheduling}))return{error:{type:`basic`,message:e(`server:core.publish.operations.schedule.not.supported`),status:400},data:void 0};let y=new Date().toISOString(),b=await d.updateSingle({where:[{key:`id`,operator:`=`,value:m.data.id}],data:{scheduled_at:p?.scheduledAt??null,scheduled_timezone:p?.scheduledTimezone??null,...m.data.status===`pending`?{execution_status:`awaiting_approval`}:{},updated_at:y}});if(b.error)return b;if(m.data.status===`approved`)return c(l,{id:m.data.id,userId:u.user.id,eventType:`rescheduled`});let x=await s(l,{operation:m.data,collectionInstance:h.data,event:{type:`rescheduled`,userId:u.user.id,comment:null,metadata:{scheduledAt:p?.scheduledAt??null,scheduledTimezone:p?.scheduledTimezone??null}}});return x.error?x:{error:void 0,data:void 0}};export{l as default};
//# sourceMappingURL=reschedule.mjs.map