UNPKG

4.13 kBJavaScriptView Raw
1"use strict";
2Object.defineProperty(exports, "__esModule", { value: true });
3const tslib_1 = require("tslib");
4const __1 = require("..");
5exports.USER_SERVICE = 'UserService';
6exports.normaliseEmail = (email) => email.toLowerCase();
7class AbstractUserService {
8 constructor(loginIdentifierRepository) {
9 this.loginIdentifierRepository = loginIdentifierRepository;
10 this.baseLogger = __1.createLogger('abstract-user-service');
11 }
12 async getByEmail(context, email) {
13 const loginIdentifier = await this.loginIdentifierRepository.get(context, exports.normaliseEmail(email));
14 return loginIdentifier && this.get(context, loginIdentifier.userId);
15 }
16 async createOrUpdate(context, updates, beforeUpdate = () => { }) {
17 const existingUser = await this.getByEmail(context, updates.email);
18 if (existingUser) {
19 beforeUpdate(existingUser);
20 }
21 return existingUser ? this.updateRetrievedUser(context, existingUser, updates) : this.create(context, updates);
22 }
23 async create(context, user) {
24 const normalisedEmail = exports.normaliseEmail(user.email);
25 await this.validateEmailAddressAvailable(context, normalisedEmail);
26 const createdUser = await this.createUser(context, Object.assign({}, user, { email: normalisedEmail, roles: user.roles || [] }));
27 await this.createLoginIdentifier(context, normalisedEmail, createdUser.id);
28 this.baseLogger.info(`Created new user ${user.email}`);
29 return createdUser;
30 }
31 async update(context, id, updates) {
32 const user = await this.get(context, id);
33 if (!user) {
34 throw new Error(`No user exists with id: ${id}`);
35 }
36 return await this.updateRetrievedUser(context, user, updates);
37 }
38 async updateRetrievedUser(context, user, updates) {
39 if (updates.roles && updates.roles.includes('super')) {
40 throw new Error('Cannot assign super role to users');
41 }
42 const normalisedEmail = updates.email && exports.normaliseEmail(updates.email);
43 if (normalisedEmail && normalisedEmail !== user.email) {
44 this.baseLogger.info(`Email changed from [${user.email}] to [${normalisedEmail}]. Changing email for user id [${user.id}]`);
45 await this.validateEmailAddressAvailable(context, normalisedEmail);
46 await Promise.all([
47 this.loginIdentifierRepository.delete(context, user.email),
48 this.createLoginIdentifier(context, normalisedEmail, user.id),
49 ]);
50 }
51 const userUpdates = (normalisedEmail && Object.assign({}, updates, { email: normalisedEmail })) || updates;
52 return this.updateUser(context, user, userUpdates);
53 }
54 async createLoginIdentifier(context, email, userId) {
55 return this.loginIdentifierRepository.save(context, {
56 id: email,
57 createdAt: new Date(),
58 userId,
59 });
60 }
61 async validateEmailAddressAvailable(context, email) {
62 const existing = await this.loginIdentifierRepository.get(context, email);
63 if (existing) {
64 throw new Error(`Email address already exists: ${email}`);
65 }
66 }
67}
68tslib_1.__decorate([
69 __1.Transactional(),
70 tslib_1.__metadata("design:type", Function),
71 tslib_1.__metadata("design:paramtypes", [Object, Object, Function]),
72 tslib_1.__metadata("design:returntype", Promise)
73], AbstractUserService.prototype, "createOrUpdate", null);
74tslib_1.__decorate([
75 __1.Transactional(),
76 tslib_1.__metadata("design:type", Function),
77 tslib_1.__metadata("design:paramtypes", [Object, Object]),
78 tslib_1.__metadata("design:returntype", Promise)
79], AbstractUserService.prototype, "create", null);
80tslib_1.__decorate([
81 __1.Transactional(),
82 tslib_1.__metadata("design:type", Function),
83 tslib_1.__metadata("design:paramtypes", [Object, String, Object]),
84 tslib_1.__metadata("design:returntype", Promise)
85], AbstractUserService.prototype, "update", null);
86exports.AbstractUserService = AbstractUserService;
87//# sourceMappingURL=user.service.js.map
\No newline at end of file