UNPKG

@wepublish/api

Version:
87 lines 3.66 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.resetUserPassword = exports.updateAdminUser = exports.createAdminUser = exports.deleteUserById = void 0; const tslib_1 = require("tslib"); const client_1 = require("@prisma/client"); const user_1 = require("../../db/user"); const api_1 = require("../../../../user-api/src"); const error_1 = require("../../error"); const validator_1 = require("../../validator"); const permissions_1 = require("../permissions"); const api_2 = require("../../../../permissions-api/src"); const user_mutation_1 = require("./user.mutation"); const api_3 = require("../../../../mail-api/src"); const deleteUserById = (id, authenticate, user) => { const { roles } = authenticate(); (0, permissions_1.authorise)(api_2.CanDeleteUser, roles); return user.delete({ where: { id }, select: api_1.unselectPassword }); }; exports.deleteUserById = deleteUserById; const createAdminUser = (input, authenticate, hashCostFactor, prisma, mailContext) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_2.CanCreateUser, roles); input.email = input.email ? input.email.toLowerCase() : input.email; yield validator_1.Validator.createUser().parse(input); const userExists = yield prisma.user.findUnique({ where: { email: input.email } }); if (userExists) throw new error_1.EmailAlreadyInUseError(); return (0, user_mutation_1.createUser)(input, hashCostFactor, prisma, mailContext); }); exports.createAdminUser = createAdminUser; const updateAdminUser = (id, _a, authenticate, user) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { var { properties, address } = _a, input = tslib_1.__rest(_a, ["properties", "address"]); const { roles } = authenticate(); (0, permissions_1.authorise)(api_2.CanCreateUser, roles); input.email = input.email ? input.email.toLowerCase() : input.email; yield validator_1.Validator.createUser().parse(input); return user.update({ where: { id }, data: Object.assign(Object.assign({}, input), { address: address ? { upsert: { create: address, update: address } } : undefined, properties: { deleteMany: { userId: id }, createMany: { data: properties } } }), select: api_1.unselectPassword }); }); exports.updateAdminUser = updateAdminUser; const resetUserPassword = (id, password, sendMail, hashCostFactor, authenticate, mailContext, userClient) => tslib_1.__awaiter(void 0, void 0, void 0, function* () { const { roles } = authenticate(); (0, permissions_1.authorise)(api_2.CanResetUserPassword, roles); const user = yield userClient.update({ where: { id }, data: { password: yield (0, user_1.hashPassword)(password, hashCostFactor) }, select: api_1.unselectPassword }); if (sendMail && user) { const remoteTemplate = yield mailContext.getUserTemplateName(client_1.UserEvent.PASSWORD_RESET); yield mailContext.sendMail({ externalMailTemplateId: remoteTemplate, recipient: user, optionalData: {}, mailType: api_3.mailLogType.UserFlow }); } return user; }); exports.resetUserPassword = resetUserPassword; //# sourceMappingURL=user.private-mutation.js.map