@lucidcms/core
Version:
The core of the Lucid CMS. It's responsible for spinning up the API and serving the CMS.
1 lines • 2.66 kB
Source Map (JSON)
{"version":3,"file":"delete-single.mjs","names":["userServices.checks.checkUserAccess","userServices.checks.checkNotLastUser"],"sources":["../../../src/services/users/delete-single.ts"],"sourcesContent":["import { copy } from \"../../libs/i18n/index.js\";\nimport { UsersRepository } from \"../../libs/repositories/index.js\";\nimport type { ServiceFn } from \"../../utils/services/types.js\";\nimport { invalidateAuthCache } from \"../auth/helpers/auth-cache.js\";\nimport { userServices } from \"../index.js\";\n\nconst deleteSingle: ServiceFn<\n\t[\n\t\t{\n\t\t\tuserId: number;\n\t\t\tcurrentUserId: number;\n\t\t},\n\t],\n\tundefined\n> = async (context, data) => {\n\tconst Users = new UsersRepository(context.db.client, context.config.db);\n\n\tif (data.currentUserId === data.userId) {\n\t\treturn {\n\t\t\terror: {\n\t\t\t\ttype: \"basic\",\n\t\t\t\tmessage: copy(\"server:core.users.self.delete.denied\"),\n\t\t\t\tstatus: 400,\n\t\t\t},\n\t\t\tdata: undefined,\n\t\t};\n\t}\n\n\tconst accessRes = await userServices.checks.checkUserAccess(context, {\n\t\tid: data.userId,\n\t});\n\tif (accessRes.error) return accessRes;\n\n\tconst notLastUserRes = await userServices.checks.checkNotLastUser(context);\n\tif (notLastUserRes.error) return notLastUserRes;\n\n\tconst deleteUserRes = await Users.updateSingle({\n\t\tdata: {\n\t\t\tis_deleted: true,\n\t\t\tis_deleted_at: new Date().toISOString(),\n\t\t\tdeleted_by: data.currentUserId,\n\t\t},\n\t\twhere: [\n\t\t\t{\n\t\t\t\tkey: \"id\",\n\t\t\t\toperator: \"=\",\n\t\t\t\tvalue: data.userId,\n\t\t\t},\n\t\t],\n\t\treturning: [\"id\", \"first_name\", \"last_name\", \"email\"],\n\t\tvalidation: {\n\t\t\tenabled: true,\n\t\t\tdefaultError: {\n\t\t\t\tstatus: 500,\n\t\t\t},\n\t\t},\n\t});\n\tif (deleteUserRes.error) return deleteUserRes;\n\n\tawait invalidateAuthCache(context);\n\n\treturn {\n\t\terror: undefined,\n\t\tdata: undefined,\n\t};\n};\n\nexport default deleteSingle;\n"],"mappings":"oQAMA,MAAM,EAQF,MAAO,EAAS,IAAS,CAC5B,IAAM,EAAQ,IAAI,EAAgB,EAAQ,GAAG,OAAQ,EAAQ,OAAO,EAAE,EAEtE,GAAI,EAAK,gBAAkB,EAAK,OAC/B,MAAO,CACN,MAAO,CACN,KAAM,QACN,QAAS,EAAK,sCAAsC,EACpD,OAAQ,GACT,EACA,KAAM,IAAA,EACP,EAGD,IAAM,EAAY,MAAMA,EAAoC,EAAS,CACpE,GAAI,EAAK,MACV,CAAC,EACD,GAAI,EAAU,MAAO,OAAO,EAE5B,IAAM,EAAiB,MAAMC,EAAqC,CAAO,EACzE,GAAI,EAAe,MAAO,OAAO,EAEjC,IAAM,EAAgB,MAAM,EAAM,aAAa,CAC9C,KAAM,CACL,WAAY,GACZ,cAAe,IAAI,KAAK,CAAA,CAAE,YAAY,EACtC,WAAY,EAAK,aAClB,EACA,MAAO,CACN,CACC,IAAK,KACL,SAAU,IACV,MAAO,EAAK,MACb,CACD,EACA,UAAW,CAAC,KAAM,aAAc,YAAa,OAAO,EACpD,WAAY,CACX,QAAS,GACT,aAAc,CACb,OAAQ,GACT,CACD,CACD,CAAC,EAKD,OAJI,EAAc,MAAc,GAEhC,MAAM,EAAoB,CAAO,EAE1B,CACN,MAAO,IAAA,GACP,KAAM,IAAA,EACP,EACD"}