@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
2 lines • 2 kB
JavaScript
import{copy as e}from"../../libs/i18n/copy.mjs";import{generateKeyBetween as t,isFractionalOrderKey as n}from"../../utils/helpers/fractional-order.mjs";import r from"../../libs/repositories/documents.mjs";import{getTableNames as i}from"../../libs/collection/schema/runtime/runtime-schema-selectors.mjs";import a from"./checks/check-collection.mjs";import o from"./checks/check-document-access.mjs";import s from"./helpers/invalidate-client-cache.mjs";const c=async(c,l)=>{let u=new r(c.db.client,c.config.db),d=await a(c,{key:l.collectionKey});if(d.error)return d;if(d.data.getData.orderable!==!0)return{error:{type:`basic`,name:e(`server:core.documents.order.not.orderable.name`),message:e(`server:core.documents.order.not.orderable.message`),status:400},data:void 0};if(l.previousDocumentId===l.documentId||l.nextDocumentId===l.documentId||l.previousDocumentId!==null&&l.previousDocumentId===l.nextDocumentId)return{error:{type:`basic`,name:e(`server:core.documents.order.invalid.name`),message:e(`server:core.documents.order.invalid.message`),status:400},data:void 0};let f=await i(c,l.collectionKey);if(f.error)return f;let p=await o(c,{collectionKey:l.collectionKey,ids:[l.documentId,...l.previousDocumentId===null?[]:[l.previousDocumentId],...l.nextDocumentId===null?[]:[l.nextDocumentId]]});if(p.error)return p;let m=[...l.previousDocumentId===null?[]:[l.previousDocumentId],...l.nextDocumentId===null?[]:[l.nextDocumentId]],h=await u.selectOrderKeysByIds({ids:m},{tableName:f.data.document});if(h.error)return h;let g=e=>{if(e===null)return null;let t=h.data?.find(t=>t.id===e)?.order;return n(t)?t:null},_=g(l.previousDocumentId),v=g(l.nextDocumentId),y=t(_,_!==null&&v!==null&&_>=v?null:v),b=await u.updateSingle({where:[{key:`id`,operator:`=`,value:l.documentId}],data:{order:y},returning:[`id`],validation:{enabled:!0}},{tableName:f.data.document});return b.error?b:(await s(c,l.collectionKey),{error:void 0,data:void 0})};export{c as default};
//# sourceMappingURL=update-order.mjs.map