@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.81 kB
Source Map (JSON)
{"version":3,"file":"check-single-collection-document-count.mjs","names":[],"sources":["../../../../src/services/documents/checks/check-single-collection-document-count.ts"],"sourcesContent":["import { copy } from \"../../../libs/i18n/index.js\";\nimport { DocumentsRepository } from \"../../../libs/repositories/index.js\";\nimport type { LucidDocumentTableName } from \"../../../types.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\n\n/**\n * Checks if the given single collection has more than one document\n */\nconst checkSingleCollectionDocumentCount: ServiceFn<\n\t[\n\t\t{\n\t\t\tcollectionKey: string;\n\t\t\tcollectionMode: \"single\" | \"multiple\";\n\t\t\tdocumentTable: LucidDocumentTableName;\n\t\t\tdocumentId?: number;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\t//* early return if collection mode is multiple - no restrictions needed\n\tif (data.collectionMode === \"multiple\") {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst Document = new DocumentsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst existingDocumentRes =\n\t\tawait Document.selectMultipleCollectionDocumentIds(\n\t\t\t{\n\t\t\t\tcollectionKey: data.collectionKey,\n\t\t\t\tisDeleted: context.config.db.getDefault(\"boolean\", \"false\"),\n\t\t\t\ttenantKey: context.request.tenantKey,\n\t\t\t},\n\t\t\t{\n\t\t\t\ttableName: data.documentTable,\n\t\t\t},\n\t\t);\n\tif (existingDocumentRes.error) return existingDocumentRes;\n\n\t//* check if there are documents besides the one being updated\n\tconst hasOtherDocuments = existingDocumentRes.data?.some(\n\t\t(doc) => doc.id !== data.documentId,\n\t);\n\tif (hasOtherDocuments) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.collections.single.document.exists\"),\n\t\t\t\tstatus: 400,\n\t\t\t\terrors: {\n\t\t\t\t\tcollectionKey: {\n\t\t\t\t\t\tcode: \"invalid\",\n\t\t\t\t\t\tmessage: copy(\"server:core.collections.single.document.exists\"),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default checkSingleCollectionDocumentCount;\n"],"mappings":"2GAQA,MAAM,EAUF,MAAO,EAAS,IAAS,CAE5B,GAAI,EAAK,iBAAmB,WAC3B,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAQD,IAAM,EACL,MAAM,IANc,EACpB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAIF,CAAC,CAAC,oCACd,CACC,cAAe,EAAK,cACpB,UAAW,EAAQ,OAAO,GAAG,WAAW,UAAW,OAAO,EAC1D,UAAW,EAAQ,QAAQ,SAC5B,EACA,CACC,UAAW,EAAK,aACjB,CACD,EAuBD,OAtBI,EAAoB,MAAc,EAGZ,EAAoB,MAAM,KAClD,GAAQ,EAAI,KAAO,EAAK,UAC1B,EAEQ,CACN,MAAO,CACN,KAAM,QACN,QAAS,EAAK,gDAAgD,EAC9D,OAAQ,IACR,OAAQ,CACP,cAAe,CACd,KAAM,UACN,QAAS,EAAK,gDAAgD,CAC/D,CACD,CACD,EACA,KAAM,IAAA,EACP,EAEM,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}