@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 3.08 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 r from"./helpers/create-event.mjs";import i from"../document-workflows/can-publish-target.mjs";import a from"../documents-versions/promote-version.mjs";const o=[`executed`,`cancelled`],s=async(s,c)=>{let l=c.tenantKey===void 0?s.request.tenantKey:c.tenantKey,u=new t(s.db.client,s.config.db),d=await u.selectSingleDetailed({tenantKey:l,where:[{key:`lucid_document_publish_operations.id`,operator:`=`,value:c.id}]});if(d.error)return d;if(!d.data)return{error:{type:`basic`,message:e(`server:core.publish.operations.not.found`),status:404},data:void 0};let f=d.data;if(f.status!==`approved`||o.some(e=>e===f.execution_status)||f.execution_status===`executing`)return{error:void 0,data:void 0};let p=c.userId??f.decided_by??f.requested_by??null;if(p===null)return{error:{type:`basic`,message:e(`server:core.publish.operations.execution.user.missing`),status:400},data:void 0};let m=new Date().toISOString(),h=await u.updateSingle({where:[{key:`id`,operator:`=`,value:f.id}],data:{execution_status:`executing`,execution_error_message:null,execution_error_data:null,failed_at:null,updated_at:m}});if(h.error)return h;let g=await r(s,{operation:f,event:{type:`executing`,userId:p,comment:null,metadata:{}}});if(g.error)return g;let _=n(s,{key:f.collection_key});if(_.error)return _;if(!_.data.getData.environments.some(e=>e.key===f.target))return{error:{type:`basic`,message:e(`server:core.publish.requests.target.not.enabled`),status:400},data:void 0};let v=await i(s,{collectionKey:f.collection_key,documentId:f.document_id,target:f.target});if(v.error)return c.markFailedOnError===!0&&await u.updateSingle({where:[{key:`id`,operator:`=`,value:f.id}],data:{execution_status:`failed`,failed_at:new Date().toISOString(),execution_error_message:s.translate(v.error.message),execution_error_data:{source:`execution`},scheduled_job_id:null,updated_at:new Date().toISOString()}}),v;let y=await a(s,{fromVersionId:f.snapshot_version_id,toVersionType:f.target,collectionKey:f.collection_key,documentId:f.document_id,userId:p,createRevision:!1,tenantKey:f.tenant_key});if(y.error)return c.markFailedOnError===!0&&await u.updateSingle({where:[{key:`id`,operator:`=`,value:f.id}],data:{execution_status:`failed`,failed_at:new Date().toISOString(),execution_error_message:s.translate(y.error.message),execution_error_data:{source:`execution`},scheduled_job_id:null,updated_at:new Date().toISOString()}}),y;let b=new Date().toISOString(),x=await u.updateSingle({where:[{key:`id`,operator:`=`,value:f.id}],data:{execution_status:`executed`,executed_at:b,execution_error_message:null,execution_error_data:null,failed_at:null,scheduled_job_id:null,updated_at:b}});if(x.error)return x;let S=await r(s,{operation:f,collectionInstance:_.data,event:{type:`executed`,userId:p,comment:null,metadata:{target:f.target}}});return S.error?S:{error:void 0,data:void 0}};export{s as default};
//# sourceMappingURL=execute.mjs.map