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.67 kB
import{copy as e}from"../../libs/i18n/copy.mjs";import t from"../../libs/repositories/documents.mjs";import{getTableNames as n}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";import r from"../../libs/hooks/execute-hooks.mjs";import i from"../document-publish-operations/cancel-for-documents.mjs";import a from"./checks/check-collection.mjs";import o from"./checks/check-document-access.mjs";import s from"./helpers/invalidate-client-cache.mjs";import c from"./nullify-document-references.mjs";import l from"../preview-sessions/delete-for-documents.mjs";const u=async(u,d)=>{let f=await a(u,{key:d.collectionKey});if(f.error)return f;if(f.data.getData.locked)return{error:{type:`basic`,name:e(`server:core.error.locked.collection.name`),message:e(`server:core.error.locked.collection.message.delete`),status:400},data:void 0};let p=new t(u.db.client,u.config.db),m=await n(u,d.collectionKey);if(m.error)return m;let h=await o(u,{collectionKey:d.collectionKey,id:d.id});if(h.error)return h;let g=await p.selectSingle({select:[`id`],where:[{key:`id`,operator:`=`,value:d.id},{key:`collection_key`,operator:`=`,value:d.collectionKey},{key:`is_deleted`,operator:`=`,value:u.config.db.getDefault(`boolean`,`false`)}],validation:{enabled:!0,defaultError:{type:`basic`,message:e(`server:core.documents.not.found.message`),status:404}}},{tableName:m.data.document});if(g.error)return g;let _=await r(u,{service:`documents`,event:`beforeDelete`,config:u.config,collectionInstance:f.data},{meta:{collection:f.data,collectionKey:d.collectionKey,userId:d.userId,collectionTableNames:m.data,tenantKey:u.request.tenantKey??null,hardDelete:!1},data:{ids:[d.id]}});if(_.error)return _;let[v,y,b]=await Promise.all([p.updateSingle({where:[{key:`id`,operator:`=`,value:d.id}],data:{is_deleted:!0,is_deleted_at:new Date().toISOString(),deleted_by:d.userId},returning:[`id`],validation:{enabled:!0}},{tableName:m.data.document}),c(u,{collectionKey:f.data.key,documentId:d.id}),l(u,{collectionKey:d.collectionKey,documentIds:[d.id]})]);if(v.error)return v;if(y.error)return y;if(b.error)return b;let x=await i(u,{collectionKey:d.collectionKey,documentIds:[d.id],comment:u.translate(`server:core.documents.deleted.publish.request.comment`)});if(x.error)return x;let S=await r(u,{service:`documents`,event:`afterDelete`,config:u.config,collectionInstance:f.data},{meta:{collection:f.data,collectionKey:d.collectionKey,userId:d.userId,collectionTableNames:m.data,tenantKey:u.request.tenantKey??null,hardDelete:!1},data:{ids:[d.id]}});return S.error?S:(await s(u,d.collectionKey),{error:void 0,data:void 0})};export{u as default}; //# sourceMappingURL=delete-single.mjs.map