UNPKG

@lucidcms/core

Version:

The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.

1 lines 2.73 kB
{"version":3,"file":"create-initial.mjs","names":["getCollectionInstance"],"sources":["../../../src/services/document-workflows/create-initial.ts"],"sourcesContent":["import { DocumentWorkflowsRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport getCollectionInstance from \"../collections/get-single-instance.js\";\nimport { getWorkflowConfig } from \"./helpers/index.js\";\n\nconst createInitial: ServiceFn<\n\t[\n\t\t{\n\t\t\tcollectionKey: string;\n\t\t\tdocumentId: number;\n\t\t\tuserId: number;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tconst collectionRes = getCollectionInstance(context, {\n\t\tkey: data.collectionKey,\n\t});\n\tif (collectionRes.error) return collectionRes;\n\n\tconst workflow = getWorkflowConfig(collectionRes.data);\n\tif (!workflow) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst Workflows = new DocumentWorkflowsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst existingRes = await Workflows.selectSingle({\n\t\tselect: [\"id\"],\n\t\twhere: [\n\t\t\t{ key: \"collection_key\", operator: \"=\", value: data.collectionKey },\n\t\t\t{ key: \"document_id\", operator: \"=\", value: data.documentId },\n\t\t],\n\t});\n\tif (existingRes.error) return existingRes;\n\tif (existingRes.data) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst workflowRes = await Workflows.createSingle({\n\t\tdata: {\n\t\t\tcollection_key: data.collectionKey,\n\t\t\tdocument_id: data.documentId,\n\t\t\tstage_key: workflow.initial,\n\t\t\tcreated_by: data.userId,\n\t\t\tupdated_by: data.userId,\n\t\t},\n\t\treturning: [\"id\"],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (workflowRes.error) return workflowRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default createInitial;\n"],"mappings":"4KAKA,MAAM,EASF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAgBA,EAAsB,EAAS,CACpD,IAAK,EAAK,aACX,CAAC,EACD,GAAI,EAAc,MAAO,OAAO,EAEhC,IAAM,EAAW,EAAkB,EAAc,IAAI,EACrD,GAAI,CAAC,EACJ,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAY,IAAI,EACrB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAChB,EAEM,EAAc,MAAM,EAAU,aAAa,CAChD,OAAQ,CAAC,IAAI,EACb,MAAO,CACN,CAAE,IAAK,iBAAkB,SAAU,IAAK,MAAO,EAAK,aAAc,EAClE,CAAE,IAAK,cAAe,SAAU,IAAK,MAAO,EAAK,UAAW,CAC7D,CACD,CAAC,EACD,GAAI,EAAY,MAAO,OAAO,EAC9B,GAAI,EAAY,KACf,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAc,MAAM,EAAU,aAAa,CAChD,KAAM,CACL,eAAgB,EAAK,cACrB,YAAa,EAAK,WAClB,UAAW,EAAS,QACpB,WAAY,EAAK,OACjB,WAAY,EAAK,MAClB,EACA,UAAW,CAAC,IAAI,EAChB,WAAY,CACX,QAAS,EACV,CACD,CAAC,EAGD,OAFI,EAAY,MAAc,EAEvB,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,CACD"}