UNPKG

@lucidcms/core

Version:

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

1 lines 4.33 kB
{"version":3,"file":"create-event.mjs","names":["collectionServices.getSingleInstance"],"sources":["../../../../src/services/document-publish-operations/helpers/create-event.ts"],"sourcesContent":["import type CollectionBuilder from \"../../../libs/collection/builders/collection-builder/index.js\";\nimport type {\n\tDocumentPublishOperationEventType,\n\tLucidDocumentPublishOperationEvents,\n\tLucidDocumentPublishOperations,\n\tSelect,\n} from \"../../../libs/db/types.js\";\nimport executeHooks from \"../../../libs/hooks/execute-hooks.js\";\nimport { DocumentPublishOperationEventsRepository } from \"../../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../../utils/services/types.js\";\nimport { collectionServices } from \"../../index.js\";\n\ntype PublishOperationEventData = {\n\toperation: Pick<\n\t\tSelect<LucidDocumentPublishOperations>,\n\t\t\"id\" | \"collection_key\" | \"document_id\" | \"target\"\n\t>;\n\tcollectionInstance?: CollectionBuilder;\n\tevent: {\n\t\ttype: DocumentPublishOperationEventType;\n\t\tuserId?: number | null;\n\t\tcomment?: string | null;\n\t\tmetadata?: Record<string, unknown>;\n\t};\n};\n\nconst createEvent: ServiceFn<\n\t[PublishOperationEventData],\n\tPick<\n\t\tSelect<LucidDocumentPublishOperationEvents>,\n\t\t| \"id\"\n\t\t| \"operation_id\"\n\t\t| \"event_type\"\n\t\t| \"user_id\"\n\t\t| \"comment\"\n\t\t| \"metadata\"\n\t\t| \"created_at\"\n\t>\n> = async (context, data) => {\n\tconst Events = new DocumentPublishOperationEventsRepository(\n\t\tcontext.db.client,\n\t\tcontext.config.db,\n\t);\n\n\tconst eventRes = await Events.createSingle({\n\t\tdata: {\n\t\t\toperation_id: data.operation.id,\n\t\t\tevent_type: data.event.type,\n\t\t\tuser_id: data.event.userId ?? null,\n\t\t\tcomment: data.event.comment ?? null,\n\t\t\tmetadata: data.event.metadata ?? {},\n\t\t},\n\t\treturning: [\n\t\t\t\"id\",\n\t\t\t\"operation_id\",\n\t\t\t\"event_type\",\n\t\t\t\"user_id\",\n\t\t\t\"comment\",\n\t\t\t\"metadata\",\n\t\t\t\"created_at\",\n\t\t],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t},\n\t});\n\tif (eventRes.error) return eventRes;\n\n\tconst collectionRes =\n\t\tdata.collectionInstance !== undefined\n\t\t\t? { error: undefined, data: data.collectionInstance }\n\t\t\t: collectionServices.getSingleInstance(context, {\n\t\t\t\t\tkey: data.operation.collection_key,\n\t\t\t\t});\n\tif (collectionRes.error) return collectionRes;\n\n\tconst hookRes = await executeHooks(\n\t\tcontext,\n\t\t{\n\t\t\tservice: \"publishOperations\",\n\t\t\tevent: \"afterEvent\",\n\t\t\tconfig: context.config,\n\t\t\tcollectionInstance: collectionRes.data,\n\t\t},\n\t\t{\n\t\t\tmeta: {\n\t\t\t\tcollection: collectionRes.data,\n\t\t\t\tcollectionKey: data.operation.collection_key,\n\t\t\t},\n\t\t\tdata: {\n\t\t\t\toperationId: data.operation.id,\n\t\t\t\tcollectionKey: data.operation.collection_key,\n\t\t\t\tdocumentId: data.operation.document_id,\n\t\t\t\ttarget: data.operation.target,\n\t\t\t\tevent: {\n\t\t\t\t\tid: eventRes.data.id,\n\t\t\t\t\ttype: eventRes.data.event_type,\n\t\t\t\t\tuserId: eventRes.data.user_id,\n\t\t\t\t\tcomment: eventRes.data.comment,\n\t\t\t\t\tmetadata: eventRes.data.metadata,\n\t\t\t\t\tcreatedAt: eventRes.data.created_at,\n\t\t\t\t},\n\t\t\t},\n\t\t},\n\t);\n\tif (hookRes.error) return hookRes;\n\n\treturn eventRes;\n};\n\nexport default createEvent;\n"],"mappings":"8LA0BA,MAAM,EAYF,MAAO,EAAS,IAAS,CAM5B,IAAM,EAAW,MAAM,IALJ,EAClB,EAAQ,GAAG,OACX,EAAQ,OAAO,EAGY,CAAC,CAAC,aAAa,CAC1C,KAAM,CACL,aAAc,EAAK,UAAU,GAC7B,WAAY,EAAK,MAAM,KACvB,QAAS,EAAK,MAAM,QAAU,KAC9B,QAAS,EAAK,MAAM,SAAW,KAC/B,SAAU,EAAK,MAAM,UAAY,CAAC,CACnC,EACA,UAAW,CACV,KACA,eACA,aACA,UACA,UACA,WACA,YACD,EACA,WAAY,CACX,QAAS,EACV,CACD,CAAC,EACD,GAAI,EAAS,MAAO,OAAO,EAE3B,IAAM,EACL,EAAK,qBAAuB,IAAA,GAEzBA,EAAqC,EAAS,CAC9C,IAAK,EAAK,UAAU,cACrB,CAAC,EAHA,CAAE,MAAO,IAAA,GAAW,KAAM,EAAK,kBAAmB,EAItD,GAAI,EAAc,MAAO,OAAO,EAEhC,IAAM,EAAU,MAAM,EACrB,EACA,CACC,QAAS,oBACT,MAAO,aACP,OAAQ,EAAQ,OAChB,mBAAoB,EAAc,IACnC,EACA,CACC,KAAM,CACL,WAAY,EAAc,KAC1B,cAAe,EAAK,UAAU,cAC/B,EACA,KAAM,CACL,YAAa,EAAK,UAAU,GAC5B,cAAe,EAAK,UAAU,eAC9B,WAAY,EAAK,UAAU,YAC3B,OAAQ,EAAK,UAAU,OACvB,MAAO,CACN,GAAI,EAAS,KAAK,GAClB,KAAM,EAAS,KAAK,WACpB,OAAQ,EAAS,KAAK,QACtB,QAAS,EAAS,KAAK,QACvB,SAAU,EAAS,KAAK,SACxB,UAAW,EAAS,KAAK,UAC1B,CACD,CACD,CACD,EAGA,OAFI,EAAQ,MAAc,EAEnB,CACR"}