@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 5.44 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../utils/helpers/get-base-url.mjs";import n from"../../libs/repositories/document-bricks.mjs";import r from"../../libs/repositories/document-versions.mjs";import i from"../../libs/repositories/documents.mjs";import{getBricksTableSchema as a,getTableNames as o}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";import s from"../../libs/collection/get-collection-migration-status.mjs";import c from"../collections/get-single-instance.mjs";import l from"../../libs/hooks/execute-hooks.mjs";import u from"../documents/checks/check-document-access.mjs";import d from"../documents/helpers/invalidate-client-cache.mjs";import f from"../../libs/collection/migration/get-current-collection-migration-id.mjs";import p from"../documents-bricks/helpers/aggregate-brick-tables.mjs";import m from"../documents-bricks/insert-brick-tables.mjs";import h from"../../libs/formatters/document-bricks.mjs";const g=async(g,_)=>{let v=new r(g.db.client,g.config.db),y=new i(g.db.client,g.config.db),b=new n(g.db.client,g.config.db),x=c(g,{key:_.collectionKey});if(x.error)return x;if(_.requirePublishOperationForEnvironmentTarget===!0&&x.data.getData.environments.some(e=>e.key===_.toVersionType))return{error:{type:`basic`,name:e(`server:core.collections.permission.error.name`),message:e(`server:core.publish.operations.required.for.environment.target`),status:403},data:void 0};let S=await s(g,{collection:x.data});if(S.error)return S;if(S.data.requiresMigration)return{error:{type:`basic`,name:e(`server:core.error.schema.migration.required.name`),message:e(`server:core.error.schema.migration.required.message`),status:400},data:void 0};let C=await a(g,_.collectionKey);if(C.error)return C;let w=await o(g,_.collectionKey);if(w.error)return w;let T=await u(g,{collectionKey:_.collectionKey,id:_.documentId});if(T.error)return T;let[E,D]=await Promise.all([v.selectSingle({select:[`id`,`type`,`document_id`,`content_id`],where:[{key:`id`,operator:`=`,value:_.fromVersionId}],validation:{enabled:!0,defaultError:{message:e(`server:core.documents.version.not.found.message`),status:404}}},{tableName:w.data.version}),b.selectMultipleByVersionId({versionId:_.fromVersionId,documentId:_.documentId,bricksSchema:C.data},{tableName:w.data.version})]);if(E.error)return E;if(D.error)return D;if(D.data===void 0)return{error:{status:404,message:e(`server:core.documents.version.not.found.message`)},data:void 0};if(E.data.document_id!==_.documentId)return{error:{type:`basic`,message:e(`server:core.documents.version.document.mismatch`),status:404},data:void 0};if(E.data.type===_.toVersionType)return{error:{type:`basic`,status:400,message:e(`server:core.documents.versions.promote.same.version`)},data:void 0};if(E.data.type===`revision`&&_.skipRevisionCheck!==!0)return{error:{type:`basic`,status:400,message:e(`server:core.documents.revisions.promote.denied`)},data:void 0};if(x.data.getData.locked===!0)return{error:{type:`basic`,name:e(`server:core.error.locked.collection.name`),message:e(`server:core.error.locked.collection.message`),status:400},data:void 0};let O=await f(g,_.collectionKey);if(O.error)return O;let k=x.data.getData.revisions&&_.createRevision!==!1,[,A,j]=await Promise.all([k?v.updateSingle({where:[{key:`document_id`,operator:`=`,value:_.documentId},{key:`type`,operator:`=`,value:_.toVersionType}],data:{type:`revision`,collection_migration_id:O.data,promoted_from:_.fromVersionId,created_by:_.userId}},{tableName:w.data.version}):v.deleteSingle({where:[{key:`document_id`,operator:`=`,value:_.documentId},{key:`type`,operator:`=`,value:_.toVersionType}]},{tableName:w.data.version}),y.upsertSingle({data:{id:_.documentId,collection_key:_.collectionKey,collection_migration_id:O.data,created_by:_.userId,updated_by:_.userId,is_deleted:!1,updated_at:new Date().toISOString()},returning:[`id`],validation:{enabled:!0,defaultError:{status:400,message:e(`server:core.documents.create.failed`)}}},{tableName:w.data.document}),v.createSingle({data:{document_id:_.documentId,collection_key:_.collectionKey,collection_migration_id:O.data,type:_.toVersionType,promoted_from:_.fromVersionId,content_id:E.data.content_id,created_by:_.userId,updated_by:_.userId},returning:[`id`],validation:{enabled:!0,defaultError:{status:400,message:e(`server:core.documents.create.failed`)}}},{tableName:w.data.version})]);if(A.error)return A;if(j.error)return j;let M=t(g),N=await m(g,{tables:p({collection:x.data,documentId:_.documentId,versionId:j.data.id,localization:g.config.localization,bricks:h.formatMultiple({bricksQuery:D.data,bricksSchema:C.data,refData:{data:{}},collection:x.data,config:g.config,host:M}),fields:h.formatDocumentFields({bricksQuery:D.data,bricksSchema:C.data,refData:{data:{}},collection:x.data,config:g.config,host:M}),tableNameByteLimit:g.config.db.config.tableNameByteLimit}).sort((e,t)=>e.priority-t.priority),collection:x.data});if(N.error)return N;let P=await l(g,{service:`documents`,event:`versionPromote`,config:g.config,collectionInstance:x.data},{meta:{collection:x.data,collectionKey:_.collectionKey,userId:_.userId,collectionTableNames:w.data,tenantKey:_.tenantKey===void 0?g.request.tenantKey??null:_.tenantKey},data:{documentId:_.documentId,versionId:j.data.id,versionType:_.toVersionType}});return P.error?P:(await d(g,_.collectionKey),{error:void 0,data:void 0})};export{g as default};
//# sourceMappingURL=promote-version.mjs.map