forked from Code-4-Community/scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathusers.service.ts
More file actions
159 lines (135 loc) · 4.21 KB
/
users.service.ts
File metadata and controls
159 lines (135 loc) · 4.21 KB
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
import {
BadRequestException,
Injectable,
InternalServerErrorException,
NotFoundException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import { In, Repository } from 'typeorm';
import { User } from './users.entity';
import { Role } from './types';
import { validateId } from '../utils/validation.utils';
import { UpdateUserInfoDto } from './dtos/update-user-info.dto';
import { AuthService } from '../auth/auth.service';
import { userSchemaDto } from './dtos/userSchema.dto';
import { emailTemplates } from '../emails/emailTemplates';
import { EmailsService } from '../emails/email.service';
@Injectable()
export class UsersService {
constructor(
@InjectRepository(User)
private repo: Repository<User>,
private authService: AuthService,
private emailsService: EmailsService,
) {}
async create(createUserDto: userSchemaDto): Promise<User> {
const { email, firstName, lastName, phone, role } = createUserDto;
const emailsEnabled = process.env.SEND_AUTOMATED_EMAILS === 'true';
// Just save to DB if emails are disabled (no Cognito creation)
if (!emailsEnabled) {
const user = this.repo.create({
role,
firstName,
lastName,
email,
phone,
});
return this.repo.save(user);
}
// Pantry and food manufacturer users must already exist in the DB
// (created during application) before a Cognito account is made
if (role === Role.PANTRY || role === Role.FOODMANUFACTURER) {
const existingUser = await this.repo.findOneBy({ email });
if (!existingUser) {
throw new NotFoundException(`User with email ${email} not found`);
}
existingUser.userCognitoSub = await this.authService.adminCreateUser({
firstName,
lastName,
email,
});
return this.repo.save(existingUser);
}
// Create Cognito user and save to DB
const userCognitoSub = await this.authService.adminCreateUser({
firstName,
lastName,
email,
});
const user = this.repo.create({
role,
firstName,
lastName,
email,
phone,
userCognitoSub,
});
await this.repo.save(user);
// Send welcome email to new volunteers (only after successful creation)
if (role === Role.VOLUNTEER) {
try {
const message = emailTemplates.volunteerAccountCreated();
await this.emailsService.sendEmails(
[email],
message.subject,
message.bodyHTML,
);
} catch {
throw new InternalServerErrorException(
'Failed to send account created notification email to volunteer',
);
}
}
return user;
}
async findOne(id: number): Promise<User> {
validateId(id, 'User');
const user = await this.repo.findOneBy({ id });
if (!user) {
throw new NotFoundException(`User ${id} not found`);
}
return user;
}
async update(id: number, dto: UpdateUserInfoDto): Promise<User> {
validateId(id, 'User');
const { firstName, lastName, phone } = dto;
if (
firstName === undefined &&
lastName === undefined &&
phone === undefined
) {
throw new BadRequestException(
'At least one field must be provided to update',
);
}
const user = await this.findOne(id);
if (!user) {
throw new NotFoundException(`User ${id} not found`);
}
if (firstName !== undefined) user.firstName = firstName;
if (lastName !== undefined) user.lastName = lastName;
if (phone !== undefined) user.phone = phone;
return this.repo.save(user);
}
async remove(id: number) {
validateId(id, 'User');
const user = await this.findOne(id);
if (!user) {
throw new NotFoundException(`User ${id} not found`);
}
return this.repo.remove(user);
}
async findUsersByRoles(roles: Role[]): Promise<User[]> {
return this.repo.find({
where: { role: In(roles) },
relations: ['pantries'],
});
}
async findUserByCognitoId(cognitoId: string): Promise<User> {
const user = await this.repo.findOneBy({ userCognitoSub: cognitoId });
if (!user) {
throw new NotFoundException(`User with cognitoId ${cognitoId} not found`);
}
return user;
}
}