-
Notifications
You must be signed in to change notification settings - Fork 1
/
user.service.ts
159 lines (128 loc) · 4.35 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
import { Injectable } from '@nestjs/common';
import { SnipcodeError, errors } from '@snipcode/utils';
import bcrypt from 'bcryptjs';
import { generateFromEmail } from 'unique-username-generator';
import { CreateUserInput } from './inputs/create-user-input';
import { UpdateUserInput } from './inputs/update-user-input';
import { User } from './user.entity';
import { CreateUserRootFolderInput } from '../folders/inputs/create-user-root-folder-input';
import { PrismaService } from '../prisma.service';
import { Role } from '../roles/role.entity';
@Injectable()
export class UserService {
constructor(private readonly prisma: PrismaService) {}
async create(createUserInput: CreateUserInput): Promise<User> {
const user = await this.findByEmail(createUserInput.email);
if (user) {
throw new SnipcodeError(errors.EMAIL_ALREADY_TAKEN, 'EMAIL_ALREADY_TAKEN');
}
const username = await this.generateUsername(createUserInput.email, createUserInput.username);
createUserInput.setUsername(username);
const input = createUserInput.toUser();
return this.prisma.user.create({
data: {
email: input.email,
id: input.id,
isEnabled: input.isEnabled,
name: input.name,
oauthProvider: input.oauthProvider,
password: input.password,
pictureUrl: input.pictureUrl,
roleId: input.roleId,
timezone: input.timezone,
username: input.username,
},
});
}
async update(currentUser: User, updateUserInput: UpdateUserInput): Promise<User> {
const input = updateUserInput.toUser(currentUser);
return this.prisma.user.update({
data: {
isEnabled: input.isEnabled,
name: input.name,
oauthProvider: input.oauthProvider,
pictureUrl: input.pictureUrl,
roleId: input.roleId,
timezone: input.timezone,
updatedAt: new Date(),
},
where: { id: currentUser.id },
});
}
async loadAdminUser(role: Role, adminPassword: string): Promise<void> {
const userAdminInput = new CreateUserInput({
email: '[email protected]',
name: 'Eric Teco',
oauthProvider: 'email',
password: adminPassword,
pictureUrl: null,
roleId: role.id,
timezone: 'Europe/Paris',
username: 'teco',
});
userAdminInput.isEnabled = true;
const userAdmin = await this.findByEmail(userAdminInput.email);
if (userAdmin) {
return;
}
const userInput = userAdminInput.toUser();
const user = await this.prisma.user.create({
data: {
email: userInput.email,
id: userInput.id,
isEnabled: userInput.isEnabled,
name: userInput.name,
oauthProvider: userInput.oauthProvider,
password: userInput.password,
pictureUrl: userInput.pictureUrl,
roleId: userInput.roleId,
timezone: userInput.timezone,
username: userInput.username,
},
});
const createUserRootFolderInput = new CreateUserRootFolderInput(user.id);
const folderInput = createUserRootFolderInput.toFolder();
await this.prisma.folder.create({
data: {
id: folderInput.id,
name: folderInput.name,
parentId: folderInput.parentId,
userId: folderInput.userId,
},
});
}
async findByEmail(email: string): Promise<User | null> {
return this.prisma.user.findUnique({ where: { email } });
}
async findById(id: string): Promise<User | null> {
return this.prisma.user.findUnique({ where: { id } });
}
async login(email: string, password: string): Promise<User> {
const user = await this.findByEmail(email);
if (!user) {
throw new SnipcodeError(errors.LOGIN_FAILED, 'LOGIN_FAILED');
}
const isPasswordValid = user.password ? bcrypt.compareSync(password, user.password) : false;
if (!isPasswordValid) {
throw new SnipcodeError(errors.LOGIN_FAILED, 'LOGIN_FAILED');
}
if (!user.isEnabled) {
throw new SnipcodeError(errors.ACCOUNT_DISABLED, 'ACCOUNT_DISABLED');
}
return user;
}
private async generateUsername(email: string, username: string | null): Promise<string> {
if (!username) {
return generateFromEmail(email, 3);
}
const user = await this.prisma.user.findFirst({
where: {
username,
},
});
if (!user) {
return username;
}
return generateFromEmail(email, 3);
}
}