UNPKG

@lucidcms/core

Version:

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

1 lines 3.29 kB
{"version":3,"file":"delete-single-permanently.mjs","names":["userServices.checks.checkUserAccess","formatter","userServices.checks.checkNotLastUser"],"sources":["../../../src/services/users/delete-single-permanently.ts"],"sourcesContent":["import formatter from \"../../libs/formatters/index.js\";\nimport { 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 deleteSinglePermanently: 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 getUserRes = await Users.selectSingle({\n\t\tselect: [\"id\", \"is_deleted\"],\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\tvalidation: {\n\t\t\tenabled: true,\n\t\t\tdefaultError: {\n\t\t\t\tmessage: copy(\"server:core.user.not.found.message\"),\n\t\t\t\tstatus: 404,\n\t\t\t},\n\t\t},\n\t});\n\tif (getUserRes.error) return getUserRes;\n\n\tif (!formatter.formatBoolean(getUserRes.data.is_deleted)) {\n\t\tconst notLastUserRes = await userServices.checks.checkNotLastUser(context);\n\t\tif (notLastUserRes.error) return notLastUserRes;\n\t}\n\n\tconst deleteUserRes = await Users.deleteSingle({\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\"],\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 deleteSinglePermanently;\n"],"mappings":"mTAOA,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,EAAa,MAAM,EAAM,aAAa,CAC3C,OAAQ,CAAC,KAAM,YAAY,EAC3B,MAAO,CACN,CACC,IAAK,KACL,SAAU,IACV,MAAO,EAAK,MACb,CACD,EACA,WAAY,CACX,QAAS,GACT,aAAc,CACb,QAAS,EAAK,oCAAoC,EAClD,OAAQ,GACT,CACD,CACD,CAAC,EACD,GAAI,EAAW,MAAO,OAAO,EAE7B,GAAI,CAACC,EAAU,cAAc,EAAW,KAAK,UAAU,EAAG,CACzD,IAAM,EAAiB,MAAMC,EAAqC,CAAO,EACzE,GAAI,EAAe,MAAO,OAAO,CAClC,CAEA,IAAM,EAAgB,MAAM,EAAM,aAAa,CAC9C,MAAO,CACN,CACC,IAAK,KACL,SAAU,IACV,MAAO,EAAK,MACb,CACD,EACA,UAAW,CAAC,IAAI,EAChB,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"}