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.38 kB
{"version":3,"file":"delete-multiple-permanently.mjs","names":["mediaServices.checks.checkMediaAccess"],"sources":["../../../src/services/media/delete-multiple-permanently.ts"],"sourcesContent":["import executeHooks from \"../../libs/hooks/execute-hooks.js\";\nimport { copy } from \"../../libs/i18n/index.js\";\nimport cacheKeys from \"../../libs/kv/cache-keys.js\";\nimport { invalidateHttpCacheTags } from \"../../libs/kv/http-cache.js\";\nimport { MediaRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport { mediaServices } from \"../index.js\";\nimport clearClientMediaSingleCache from \"./helpers/clear-client-media-cache.js\";\nimport permanentlyDeleteMedia from \"./helpers/permanently-delete-media.js\";\n\nconst deleteMultiplePermanently: ServiceFn<\n\t[\n\t\t{\n\t\t\tids: number[];\n\t\t\tuserId: number;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tif (!data.ids || data.ids.length === 0) {\n\t\treturn {\n\t\t\terror: undefined,\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst Media = new MediaRepository(context.db.client, context.config.db);\n\n\tconst accessRes = await mediaServices.checks.checkMediaAccess(context, {\n\t\tids: data.ids,\n\t});\n\tif (accessRes.error) return accessRes;\n\n\tconst existRes = await Media.selectMultiple({\n\t\tselect: [\"id\"],\n\t\twhere: [\n\t\t\t{\n\t\t\t\tkey: \"id\",\n\t\t\t\toperator: \"in\",\n\t\t\t\tvalue: data.ids,\n\t\t\t},\n\t\t],\n\t\tvalidation: { enabled: true },\n\t});\n\tif (existRes.error) return existRes;\n\n\tconst existing = new Set(existRes.data.map((r) => r.id));\n\tconst missing = data.ids.filter((id) => !existing.has(id));\n\tif (missing.length > 0) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.media.not.found.message\"),\n\t\t\t\terrors: {\n\t\t\t\t\tids: {\n\t\t\t\t\t\tmessage: copy(\"server:core.documents.ids.not.found.partial\", {\n\t\t\t\t\t\t\tdata: {\n\t\t\t\t\t\t\t\tids: existRes.data.map((m) => m.id).join(\", \"),\n\t\t\t\t\t\t\t},\n\t\t\t\t\t\t}),\n\t\t\t\t\t},\n\t\t\t\t},\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tfor (const id of data.ids) {\n\t\tconst deleteRes = await permanentlyDeleteMedia(context, {\n\t\t\tid,\n\t\t\tinvalidateCache: false,\n\t\t});\n\t\tif (deleteRes.error) return deleteRes;\n\t}\n\n\tawait Promise.all([\n\t\t...data.ids.map((id) => clearClientMediaSingleCache(context, id)),\n\t\tinvalidateHttpCacheTags(context, [cacheKeys.http.tags.clientMedia]),\n\t]);\n\n\tconst hookRes = await executeHooks(\n\t\tcontext,\n\t\t{\n\t\t\tservice: \"media\",\n\t\t\tevent: \"afterDelete\",\n\t\t\tconfig: context.config,\n\t\t},\n\t\t{\n\t\t\tmeta: {\n\t\t\t\ttenantKey: context.request.tenantKey ?? null,\n\t\t\t},\n\t\t\tdata: {\n\t\t\t\tids: data.ids,\n\t\t\t\tuserId: data.userId,\n\t\t\t\thardDelete: true,\n\t\t\t},\n\t\t},\n\t);\n\tif (hookRes.error) return hookRes;\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default deleteMultiplePermanently;\n"],"mappings":"iaAUA,MAAM,EAQF,MAAO,EAAS,IAAS,CAC5B,GAAI,CAAC,EAAK,KAAO,EAAK,IAAI,SAAW,EACpC,MAAO,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EAGD,IAAM,EAAQ,IAAI,EAAgB,EAAQ,GAAG,OAAQ,EAAQ,OAAO,EAAE,EAEhE,EAAY,MAAMA,EAAsC,EAAS,CACtE,IAAK,EAAK,GACX,CAAC,EACD,GAAI,EAAU,MAAO,OAAO,EAE5B,IAAM,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