-
Notifications
You must be signed in to change notification settings - Fork 30
/
user.service.ts
420 lines (372 loc) · 15.6 KB
/
user.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
import { HttpException, HttpStatus, Injectable } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { deleteMailchimpProfile } from 'src/api/mailchimp/mailchimp-api';
import { CrispService } from 'src/crisp/crisp.service';
import { PartnerAccessEntity } from 'src/entities/partner-access.entity';
import { PartnerEntity } from 'src/entities/partner.entity';
import { UserEntity } from 'src/entities/user.entity';
import { IFirebaseUser } from 'src/firebase/firebase-user.interface';
import { Logger } from 'src/logger/logger';
import { ServiceUserProfilesService } from 'src/service-user-profiles/service-user-profiles.service';
import { SubscriptionUserService } from 'src/subscription-user/subscription-user.service';
import { TherapySessionService } from 'src/therapy-session/therapy-session.service';
import { SIGNUP_TYPE } from 'src/utils/constants';
import { FIREBASE_ERRORS } from 'src/utils/errors';
import { FIREBASE_EVENTS, USER_SERVICE_EVENTS } from 'src/utils/logs';
import { ILike, IsNull, Not, Repository } from 'typeorm';
import { AuthService } from '../auth/auth.service';
import { basePartnerAccess, PartnerAccessService } from '../partner-access/partner-access.service';
import { formatUserObject } from '../utils/serialize';
import { generateRandomString } from '../utils/utils';
import { AdminUpdateUserDto } from './dtos/admin-update-user.dto';
import { CreateUserDto } from './dtos/create-user.dto';
import { GetUserDto } from './dtos/get-user.dto';
import { UpdateUserDto } from './dtos/update-user.dto';
@Injectable()
export class UserService {
private readonly logger = new Logger('UserService');
constructor(
@InjectRepository(UserEntity)
private userRepository: Repository<UserEntity>,
@InjectRepository(PartnerAccessEntity)
private partnerAccessRepository: Repository<PartnerAccessEntity>,
@InjectRepository(PartnerEntity)
private partnerRepository: Repository<PartnerEntity>,
private readonly authService: AuthService,
private readonly subscriptionUserService: SubscriptionUserService,
private readonly therapySessionService: TherapySessionService,
private readonly partnerAccessService: PartnerAccessService,
private readonly serviceUserProfilesService: ServiceUserProfilesService,
private readonly crispService: CrispService,
) {}
public async createUser(createUserDto: CreateUserDto): Promise<GetUserDto> {
const { email, partnerAccessCode, partnerId, password } = createUserDto;
const signUpType = partnerAccessCode
? SIGNUP_TYPE.PARTNER_USER_WITH_CODE
: partnerId
? SIGNUP_TYPE.PARTNER_USER_WITHOUT_CODE
: SIGNUP_TYPE.PUBLIC_USER;
try {
let partnerAccess: PartnerAccessEntity;
let partner: PartnerEntity;
if (signUpType === SIGNUP_TYPE.PARTNER_USER_WITHOUT_CODE) {
await this.partnerAccessService.validatePartnerAutomaticAccessCode(partnerId);
partner = await this.partnerRepository.findOneBy({ id: partnerId });
}
if (signUpType === SIGNUP_TYPE.PARTNER_USER_WITH_CODE) {
partnerAccess = await this.partnerAccessService.getPartnerAccessByCode(partnerAccessCode);
partner = partnerAccess.partner;
}
const firebaseUser = await this.authService.createFirebaseUser(email, password);
const user = await this.userRepository.save({
...createUserDto,
lastActiveAt: new Date(),
firebaseUid: firebaseUser.uid,
});
if (signUpType === SIGNUP_TYPE.PARTNER_USER_WITHOUT_CODE) {
// Create and assign new partner access without code
partnerAccess = await this.partnerAccessService.createPartnerAccess(
basePartnerAccess,
partnerId,
null,
user.id,
);
this.logger.log(`Create user: (no access code) created partner user in db. User: ${email}`);
} else if (signUpType === SIGNUP_TYPE.PARTNER_USER_WITH_CODE) {
// Assign the existing partner access to new user
partnerAccess.userId = user.id;
partnerAccess = await this.partnerAccessRepository.save(partnerAccess);
this.logger.log(
`Create user: (with access code) created partner user in db. User: ${email}`,
);
} else {
this.logger.log(`Create user: created public user in db. User: ${email}`);
}
await this.serviceUserProfilesService.createServiceUserProfiles(user, partner, partnerAccess);
const userDto = formatUserObject({
...user,
...(partnerAccess && { partnerAccess: [{ ...partnerAccess, partner }] }),
});
return userDto;
} catch (error) {
if (!Object.values(FIREBASE_ERRORS).includes(error)) {
this.logger.error(`Create user: Error creating user ${email}: ${error}`);
}
throw error;
}
}
public async getUserByFirebaseId({ uid }: IFirebaseUser): Promise<{
userEntity: UserEntity | undefined;
userDto: GetUserDto | undefined;
}> {
const queryResult = await this.userRepository.findOneBy({
firebaseUid: uid,
});
if (!queryResult) {
throw new HttpException('USER NOT FOUND', HttpStatus.NOT_FOUND);
}
return { userEntity: queryResult, userDto: formatUserObject(queryResult) };
}
public async getUserProfile(id: string): Promise<{
userEntity: UserEntity | undefined;
userDto: GetUserDto | undefined;
}> {
const queryResult = await this.userRepository
.createQueryBuilder('user')
.leftJoinAndSelect('user.partnerAccess', 'partnerAccess')
.leftJoinAndSelect('user.partnerAdmin', 'partnerAdmin')
.leftJoinAndSelect('partnerAccess.therapySession', 'therapySession')
.leftJoinAndSelect('partnerAccess.partner', 'partner')
.leftJoinAndSelect('partnerAccess.partner', 'partnerAccessPartner')
.leftJoinAndSelect('partnerAdmin.partner', 'partnerAdminPartner')
.leftJoinAndSelect('user.courseUser', 'courseUser')
.leftJoinAndSelect('courseUser.course', 'course')
.leftJoinAndSelect('courseUser.sessionUser', 'sessionUser')
.leftJoinAndSelect('sessionUser.session', 'session')
.leftJoinAndSelect('user.subscriptionUser', 'subscriptionUser')
.leftJoinAndSelect('subscriptionUser.subscription', 'subscription')
.where('user.id = :id', { id })
.getOne();
if (!queryResult) {
throw new HttpException('USER NOT FOUND', HttpStatus.NOT_FOUND);
}
return { userEntity: queryResult, userDto: formatUserObject(queryResult) };
}
public async getUserById(id: string): Promise<UserEntity | undefined> {
const queryResult = await this.userRepository
.createQueryBuilder('user')
.where('user.id = :id', { id })
.getOne();
if (!queryResult) {
throw new HttpException('USER NOT FOUND', HttpStatus.NOT_FOUND);
}
return queryResult;
}
public async deleteUser(user: UserEntity): Promise<UserEntity> {
const randomString = generateRandomString(20);
try {
await this.authService.deleteFirebaseUser(user.firebaseUid);
this.logger.log(`Firebase account deleted for user with ID ${user.id}`);
} catch (err) {
// Continue to delete user, even if firebase request fails
this.logger.error(
`deleteUser - firebase error. Unable to delete user ${user.email} due to error ${err}`,
);
}
try {
//TODO Not yet sure if deleting automatically from Crisp is the right thing to do
// as we don't know whether we need to manually check if there is any safeguarding concerns before we delete.
// const crispResponse = await deleteCrispProfile(user.email);
// if they have subscriptions,redact the number
await this.subscriptionUserService.softDeleteSubscriptionsForUser(user.id, user.email);
// if they have therapy sessions redact email and delete client from therapy sessions
await this.therapySessionService.softDeleteTherapySessions(user.id, user.email, randomString);
//Randomise User Data in DB
const updateUser = {
...user,
name: randomString,
email: randomString,
isActive: false,
deletedAt: new Date(),
};
return await this.userRepository.save(updateUser);
} catch (error) {
throw new HttpException(
`Unable to complete deleting user, ${user.email} due to error - ${error}`,
HttpStatus.INTERNAL_SERVER_ERROR,
);
}
}
public async deleteUserById(id: string): Promise<UserEntity> {
const user = await this.getUserById(id);
return await this.deleteUser(user);
}
public async updateUser(updateUserDto: Partial<UpdateUserDto>, userId: string) {
const user = await this.userRepository.findOneBy({ id: userId });
if (!user) {
throw new HttpException('USER NOT FOUND', HttpStatus.NOT_FOUND);
}
if (updateUserDto.email && user.email !== updateUserDto.email) {
// check whether email has been updated already in firebase
const firebaseUser = await this.authService.getFirebaseUser(user.email);
if (firebaseUser.email !== updateUserDto.email) {
await this.authService.updateFirebaseUserEmail(user.firebaseUid, updateUserDto.email);
this.logger.log({ event: FIREBASE_EVENTS.UPDATE_FIREBASE_USER_EMAIL, userId: user.id });
} else {
this.logger.log({
event: FIREBASE_EVENTS.UPDATE_FIREBASE_EMAIL_ALREADY_UPDATED,
userId: user.id,
});
}
}
const newUserData: UserEntity = {
...user,
...updateUserDto,
};
const updatedUser = await this.userRepository.save(newUserData);
this.logger.log({
event: USER_SERVICE_EVENTS.USER_UPDATED,
userId: user.id,
fields: Object.keys(updateUserDto),
});
const isEmailUpdateRequired = updateUserDto.email && user.email !== updateUserDto.email;
if (
Object.keys(updateUserDto).length === 1 &&
!!updateUserDto.lastActiveAt &&
updateUserDto.lastActiveAt.getDate() === user.lastActiveAt.getDate()
) {
// Do nothing, prevent unnecessay updates to service profiles when last active date is same date
} else {
const isCrispBaseUpdateRequired =
isEmailUpdateRequired ||
user.signUpLanguage !== updateUserDto.signUpLanguage ||
user.name !== updateUserDto.name;
this.serviceUserProfilesService.updateServiceUserProfilesUser(
newUserData,
isCrispBaseUpdateRequired,
isEmailUpdateRequired,
user.email,
);
}
return updatedUser;
}
public async adminUpdateUser(updateUserDto: Partial<AdminUpdateUserDto>, userId: string) {
const { isSuperAdmin, ...updateUserDtoWithoutSuperAdmin } = updateUserDto;
await this.updateUser(updateUserDtoWithoutSuperAdmin, userId);
if (typeof isSuperAdmin !== 'undefined') {
const user = await this.userRepository.findOneBy({ id: userId });
if (user.isSuperAdmin !== isSuperAdmin) {
const updatedUser = await this.userRepository.save({
...user,
isSuperAdmin,
});
this.logger.log({
event: USER_SERVICE_EVENTS.USER_UPDATED,
userId: user.id,
fields: ['isSuperAdmin'],
});
return updatedUser;
}
}
}
// Function to hard delete users in batches, required to clean up e.g. cypress test accounts
// Deleted users in batches of 10 per 1 second, due to firebase rate limiting
public async batchDeleteUsers(users) {
const BATCH_SIZE = 10; // Users to delete per second
const INTERVAL = 100; // Interval between batches in milliseconds
const deletedUsers: UserEntity[] = [];
let startIndex = 0;
while (startIndex < users.length) {
const batch = users.slice(startIndex, startIndex + BATCH_SIZE);
await Promise.all(
batch.map(async (user) => {
try {
await this.crispService.deleteCrispProfile(user.email);
} catch (error) {
this.logger.warn(
`deleteCypressTestUsers - unable to delete crisp profile for user ${user.id}`,
error,
);
}
try {
await deleteMailchimpProfile(user.email);
} catch (error) {
this.logger.warn(
`deleteCypressTestUsers - unable to delete mailchimp profile for user ${user.id}`,
error,
);
}
try {
await this.authService.deleteFirebaseUser(user.firebaseUid);
} catch (error) {
this.logger.warn(
`deleteCypressTestUsers - unable to delete firebase profile for user ${user.id}`,
error,
);
}
try {
await this.userRepository.delete(user.id);
deletedUsers.push(user);
} catch (error) {
this.logger.error(
`deleteCypressTestUsers - Unable to delete db record for user ${user.id}`,
error,
);
}
}),
);
startIndex += BATCH_SIZE;
await new Promise((resolve) => setTimeout(resolve, INTERVAL)); // Wait before processing next batch
}
this.logger.log(`deleteCypressTestUsers - successfully deleted ${deletedUsers.length} users`);
return deletedUsers;
}
public async deleteCypressTestUsers(clean = false): Promise<UserEntity[]> {
let deletedUsers: UserEntity[];
try {
const users = await this.userRepository.find({
where: {
email: ILike('%cypresstestemail+%'),
},
});
deletedUsers = await this.batchDeleteUsers(users);
} catch (error) {
// If this fails we don't want to break cypress tests but we want to be alerted
this.logger.error(`deleteCypressTestUsers - Unable to delete all cypress users`, error);
}
try {
// Clean remaining user accounts in firebase and crisp that do not have a user record in the db
// These rogue accounts may be left over from incomplete signups or errors
if (clean) {
// Delete all remaining cypress firebase users (e.g. from failed user creations)
await this.authService.deleteCypressFirebaseUsers();
// Delete all remaining crisp accounts
await this.crispService.deleteCypressCrispProfiles();
}
} catch (error) {
// If this fails we don't want to break cypress tests but we want to be alerted
this.logger.error(`deleteCypressTestUsers - Unable to clean all cypress users`, error);
}
return deletedUsers;
}
public async getUsers(
filters: {
email?: string;
partnerAccess?: { userId: string; featureTherapy: boolean; active: boolean };
partnerAdmin?: { partnerAdminId: string };
},
relations: string[],
fields: Array<string>,
limit: number,
): Promise<UserEntity[] | undefined> {
const users = await this.userRepository.find({
relations,
where: {
...(filters.email && { email: ILike(`%${filters.email}%`) }),
...(filters.partnerAccess && {
partnerAccess: {
...(filters.partnerAccess.userId && { userId: filters.partnerAccess.userId }),
...(typeof filters.partnerAccess.featureTherapy !== 'undefined' && {
featureTherapy: filters.partnerAccess.featureTherapy,
}),
...(typeof filters.partnerAccess.active !== 'undefined' && {
active: filters.partnerAccess.active,
}),
},
}),
...(filters.partnerAdmin && {
partnerAdmin: {
...(filters.partnerAdmin && {
id:
filters.partnerAdmin.partnerAdminId === 'IS NOT NULL'
? Not(IsNull())
: filters.partnerAdmin.partnerAdminId,
}),
},
}),
},
...(limit && { take: limit }),
});
return users;
}
}