@hedhog/admin 0.46.39 → 0.46.41

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (111) hide show
  1. package/README.md +960 -960
  2. package/dist/auth/auth.service.d.ts.map +1 -1
  3. package/dist/auth/auth.service.js +6 -4
  4. package/dist/auth/auth.service.js.map +1 -1
  5. package/dist/auth/consts/body.js +24 -24
  6. package/dist/emails/templates.d.ts.map +1 -1
  7. package/dist/emails/templates.js +48 -48
  8. package/dist/emails/templates.js.map +1 -1
  9. package/frontend/menu/components/create-panel.tsx.ejs +55 -55
  10. package/frontend/menu/components/update-panel.tsx.ejs +67 -67
  11. package/frontend/menu/locales/en/admin.menu.json +11 -11
  12. package/frontend/menu/locales/pt/admin.menu.json +11 -11
  13. package/frontend/menu/react-query/handlers.ts.ejs +28 -28
  14. package/frontend/menu/react-query/requests.ts.ejs +56 -56
  15. package/frontend/menu-locale/locales/en/admin.menu-locale.json +11 -11
  16. package/frontend/menu-locale/locales/pt/admin.menu-locale.json +11 -11
  17. package/frontend/menu-screen/locales/en/admin.menu-screen.json +11 -11
  18. package/frontend/menu-screen/locales/pt/admin.menu-screen.json +11 -11
  19. package/frontend/multifactor/components/create-panel.tsx.ejs +55 -55
  20. package/frontend/multifactor/components/update-panel.tsx.ejs +70 -70
  21. package/frontend/multifactor/locales/en/admin.multifactor.json +11 -11
  22. package/frontend/multifactor/locales/pt/admin.multifactor.json +11 -11
  23. package/frontend/multifactor/react-query/handlers.ts.ejs +28 -28
  24. package/frontend/multifactor/react-query/requests.ts.ejs +59 -59
  25. package/frontend/multifactor-locale/locales/en/admin.multifactor-locale.json +11 -11
  26. package/frontend/multifactor-locale/locales/pt/admin.multifactor-locale.json +11 -11
  27. package/frontend/screen/components/create-panel.tsx.ejs +55 -55
  28. package/frontend/screen/components/update-panel.tsx.ejs +67 -67
  29. package/frontend/screen/locales/en/admin.screen.json +11 -11
  30. package/frontend/screen/locales/pt/admin.screen.json +11 -11
  31. package/frontend/screen/react-query/handlers.ts.ejs +28 -28
  32. package/frontend/screen/react-query/requests.ts.ejs +56 -56
  33. package/frontend/screen-locale/locales/en/admin.screen-locale.json +11 -11
  34. package/frontend/screen-locale/locales/pt/admin.screen-locale.json +11 -11
  35. package/frontend/translation/components/create-panel.tsx.ejs +52 -52
  36. package/frontend/translation/components/update-panel.tsx.ejs +67 -67
  37. package/frontend/translation/locales/en/admin.translation.json +11 -11
  38. package/frontend/translation/locales/pt/admin.translation.json +11 -11
  39. package/frontend/translation/react-query/handlers.ts.ejs +28 -28
  40. package/frontend/translation/react-query/requests.ts.ejs +58 -58
  41. package/frontend/translation-namespace/components/create-panel.tsx.ejs +53 -53
  42. package/frontend/translation-namespace/components/update-panel.tsx.ejs +70 -70
  43. package/frontend/translation-namespace/locales/en/admin.translation-namespace.json +11 -11
  44. package/frontend/translation-namespace/locales/pt/admin.translation-namespace.json +11 -11
  45. package/frontend/translation-namespace/react-query/handlers.ts.ejs +28 -28
  46. package/frontend/translation-namespace/react-query/requests.ts.ejs +60 -60
  47. package/frontend/user/components/create-panel.tsx.ejs +52 -52
  48. package/frontend/user/components/update-panel.tsx.ejs +64 -64
  49. package/frontend/user/locales/en/admin.user.json +11 -11
  50. package/frontend/user/locales/pt/admin.user.json +11 -11
  51. package/frontend/user/react-query/handlers.ts.ejs +28 -28
  52. package/frontend/user/react-query/requests.ts.ejs +55 -55
  53. package/hedhog.yaml +783 -783
  54. package/package.json +45 -45
  55. package/src/admin.module.ts +39 -39
  56. package/src/auth/auth.controller.ts +88 -88
  57. package/src/auth/auth.module.ts +41 -41
  58. package/src/auth/auth.service.spec.ts +196 -196
  59. package/src/auth/auth.service.ts +355 -349
  60. package/src/auth/consts/body.ts +27 -27
  61. package/src/auth/dto/change.dto.ts +19 -19
  62. package/src/auth/dto/email.dto.ts +15 -15
  63. package/src/auth/dto/forget.dto.ts +6 -6
  64. package/src/auth/dto/login.dto.ts +21 -21
  65. package/src/auth/dto/otp.dto.ts +11 -11
  66. package/src/auth/dto/reset.dto.ts +14 -14
  67. package/src/auth/enums/multifactor-type.enum.ts +4 -4
  68. package/src/auth/guards/auth.guard.ts +54 -54
  69. package/src/auth/types/user.type.ts +8 -8
  70. package/src/dto/delete.dto.ts +8 -8
  71. package/src/dto/update-ids.dto.ts +9 -9
  72. package/src/emails/index.ts +2 -2
  73. package/src/emails/lib.ts +40 -40
  74. package/src/emails/templates.ts +62 -60
  75. package/src/index.ts +20 -20
  76. package/src/menu/dto/create.dto.ts +25 -25
  77. package/src/menu/dto/order.dto.ts +8 -8
  78. package/src/menu/dto/update.dto.ts +19 -19
  79. package/src/menu/menu.controller.ts +105 -105
  80. package/src/menu/menu.module.ts +18 -18
  81. package/src/menu/menu.service.spec.ts +247 -247
  82. package/src/menu/menu.service.ts +263 -263
  83. package/src/role/dto/create.dto.ts +7 -7
  84. package/src/role/dto/update.dto.ts +4 -4
  85. package/src/role/guards/role.guard.ts +121 -121
  86. package/src/role/role.controller.ts +126 -126
  87. package/src/role/role.module.ts +28 -28
  88. package/src/role/role.service.spec.ts +417 -417
  89. package/src/role/role.service.ts +289 -289
  90. package/src/route/dto/create.dto.ts +13 -13
  91. package/src/route/dto/update.dto.ts +15 -15
  92. package/src/route/route.controller.ts +91 -91
  93. package/src/route/route.module.ts +18 -18
  94. package/src/route/route.service.spec.ts +300 -300
  95. package/src/route/route.service.ts +164 -164
  96. package/src/screen/dto/create.dto.ts +11 -11
  97. package/src/screen/dto/update.dto.ts +19 -19
  98. package/src/screen/screen.controller.ts +93 -93
  99. package/src/screen/screen.module.ts +18 -18
  100. package/src/screen/screen.service.spec.ts +298 -298
  101. package/src/screen/screen.service.ts +179 -179
  102. package/src/types/http-method.ts +8 -8
  103. package/src/user/constants/user.constants.ts +1 -1
  104. package/src/user/dto/create.dto.ts +24 -24
  105. package/src/user/dto/update.dto.ts +41 -41
  106. package/src/user/user.controller.ts +75 -75
  107. package/src/user/user.module.ts +18 -18
  108. package/src/user/user.service.spec.ts +294 -294
  109. package/src/user/user.service.ts +129 -129
  110. package/tsconfig.lib.json +9 -9
  111. package/tsconfig.production.json +20 -20
@@ -1,349 +1,355 @@
1
- import { MailService } from '@hedhog/mail';
2
- import { PrismaService } from '@hedhog/prisma';
3
- import {
4
- BadRequestException,
5
- ConflictException,
6
- forwardRef,
7
- Inject,
8
- Injectable,
9
- NotFoundException,
10
- } from '@nestjs/common';
11
- import { ConfigService } from '@nestjs/config';
12
- import { JwtService } from '@nestjs/jwt';
13
- import { compare, genSalt, hash } from 'bcrypt';
14
- import { getBody } from './consts/body';
15
- import { ChangeDTO } from './dto/change.dto';
16
- import { EmailDTO } from './dto/email.dto';
17
- import { ForgetDTO } from './dto/forget.dto';
18
- import { LoginDTO } from './dto/login.dto';
19
- import { OtpDTO } from './dto/otp.dto';
20
- import { ResetDTO } from './dto/reset.dto';
21
- import { MultifactorType } from './enums/multifactor-type.enum';
22
- import { getChangeEmailEmail, getChangePasswordEmail, getForgetPasswordEmail, getResetPasswordEmail, getUserLoginEmail } from '../emails';
23
-
24
- @Injectable()
25
- export class AuthService {
26
- constructor(
27
- private readonly configService: ConfigService,
28
- @Inject(forwardRef(() => PrismaService))
29
- private readonly prisma: PrismaService,
30
- @Inject(forwardRef(() => JwtService))
31
- private readonly jwt: JwtService,
32
- @Inject(forwardRef(() => MailService))
33
- private readonly mail: MailService,
34
- ) { }
35
-
36
- async verifyToken(token: string) {
37
- return this.jwt.verifyAsync(token, {
38
- secret: String(process.env.JWT_SECRET),
39
- });
40
- }
41
-
42
- generateRandomString(length: number): string {
43
- const characters =
44
- 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
45
- let result = '';
46
- for (let i = 0; i < length; i++) {
47
- const randomIndex = Math.floor(Math.random() * characters.length);
48
- result += characters.charAt(randomIndex);
49
- }
50
- return result;
51
- }
52
-
53
- generateRandomNumber(): number {
54
- const min = 100000;
55
- const max = 999999;
56
- return Math.floor(Math.random() * (max - min + 1)) + min;
57
- }
58
-
59
- async loginWithEmailAndPassword(email: string, password: string) {
60
- const user = await this.prisma.user.findFirst({
61
- where: {
62
- email,
63
- },
64
- });
65
-
66
- if (!user) {
67
- throw new BadRequestException('Acesso negado');
68
- }
69
-
70
- const isPasswordValid = await compare(password, user.password);
71
- if (!isPasswordValid) {
72
- throw new BadRequestException('Acesso negado');
73
- }
74
-
75
- if (!user.multifactor_id) {
76
- return this.getToken(user);
77
- } else {
78
- if (user.multifactor_id === MultifactorType.EMAIL) {
79
- const code = this.generateRandomNumber();
80
-
81
- await this.prisma.user.update({
82
- where: {
83
- id: user.id,
84
- },
85
- data: {
86
- code: String(code),
87
- },
88
- });
89
-
90
- await this.mail.send({
91
- to: user.email,
92
- subject: 'Código de Login',
93
- body: `Seu código de login é ${code}`,
94
- });
95
- }
96
-
97
- return {
98
- name: user.name,
99
- email: user.email,
100
- token: this.jwt.sign({
101
- id: user.id,
102
- mfa: user.multifactor_id,
103
- }),
104
- mfa: true,
105
- };
106
- }
107
- }
108
-
109
- async getToken(user) {
110
- delete user.password;
111
-
112
- const payload = { user };
113
-
114
- return {
115
- token: this.jwt.sign(payload),
116
- };
117
- }
118
-
119
- async forget({ email }: ForgetDTO) {
120
- const appUrl =
121
- process.env.APP_URL ?? this.configService.get<string>('APP_URL');
122
-
123
- const user = await this.prisma.user.findFirst({
124
- where: {
125
- email,
126
- },
127
- select: {
128
- id: true,
129
- },
130
- });
131
-
132
- if (user) {
133
- const payload = {
134
- ...user,
135
- };
136
-
137
- const code = this.jwt.sign(payload);
138
-
139
- await this.prisma.user.update({
140
- where: {
141
- id: user.id,
142
- },
143
- data: {
144
- code,
145
- },
146
- });
147
-
148
- await this.mail.send({
149
- to: email,
150
- subject: `Recuperação de Senha`,
151
- body: getForgetPasswordEmail(`${appUrl}/login?mode=reset-password&code=${code}`),
152
- });
153
- }
154
-
155
- return {
156
- message:
157
- 'Se este e-mail estiver cadastrado, você receberá instruções para redefinir sua senha.',
158
- };
159
- }
160
-
161
- async changePassword({
162
- email,
163
- currentPassword,
164
- newPassword,
165
- confirmNewPassword,
166
- }: ChangeDTO) {
167
- if (newPassword !== confirmNewPassword) {
168
- throw new BadRequestException('Senhas não conferem');
169
- }
170
-
171
- const user = await this.prisma.user.findFirst({
172
- where: { email },
173
- });
174
-
175
- if (!(await compare(currentPassword, user.password))) {
176
- throw new NotFoundException('Não foi possível alterar a senha.');
177
- }
178
-
179
- const salt = await genSalt();
180
- const password = await hash(newPassword, salt);
181
-
182
- const newUser = await this.prisma.user.update({
183
- where: {
184
- id: user.id,
185
- },
186
- data: {
187
- password,
188
- },
189
- });
190
-
191
- await this.mail.send({
192
- to: email,
193
- subject: `Senha alterada`,
194
- body: getChangePasswordEmail(),
195
- });
196
-
197
- return this.getToken(newUser);
198
- }
199
-
200
- async changeEmail({ currentEmail, password, newEmail }: EmailDTO) {
201
- const user = await this.prisma.user.findFirst({
202
- where: { email: currentEmail },
203
- });
204
-
205
- if (!user) {
206
- throw new BadRequestException('Não foi possível atualizar o e-mail.');
207
- }
208
-
209
- if (!(await compare(password, user.password))) {
210
- throw new BadRequestException('Não foi possível atualizar o e-mail.');
211
- }
212
-
213
- const existingUser = await this.prisma.user.findFirst({
214
- where: { email: newEmail },
215
- });
216
-
217
- if (existingUser) {
218
- throw new ConflictException('Não foi possível atualizar o e-mail.');
219
- }
220
-
221
- const newUser = await this.prisma.user.updateMany({
222
- where: { email: currentEmail },
223
- data: { email: newEmail },
224
- });
225
-
226
- const personUser = await this.prisma.person_user.findFirst({
227
- where: { user_id: user.id },
228
- select: { person_id: true },
229
- });
230
-
231
- if (!personUser) {
232
- throw new NotFoundException('Erro ao atualizar os dados do usuário.');
233
- }
234
-
235
- const { id: emailContactTypeId } =
236
- await this.prisma.person_contact_type.findFirst({
237
- where: { slug: 'EMAIL' },
238
- });
239
-
240
- await this.prisma.person_contact.updateMany({
241
- where: {
242
- person_id: personUser.person_id,
243
- type_id: emailContactTypeId,
244
- },
245
- data: { value: newEmail },
246
- });
247
-
248
- await this.mail.send({
249
- to: newEmail,
250
- subject: `Email alterado`,
251
- body: getChangeEmailEmail(),
252
- });
253
-
254
- return this.getToken(newUser);
255
- }
256
-
257
- async resetPassword({ code, newPassword, confirmNewPassword }: ResetDTO) {
258
- if (newPassword !== confirmNewPassword) {
259
- throw new BadRequestException('Senhas não conferem');
260
- }
261
-
262
- try {
263
- const decodedCode = this.jwt.decode(code);
264
-
265
- console.log({ decodedCode });
266
-
267
- const { id } = decodedCode;
268
-
269
- const user = await this.prisma.user.findFirst({
270
- where: {
271
- id,
272
- code,
273
- },
274
- });
275
-
276
- if (user) {
277
- const salt = await genSalt();
278
- const password = await hash(confirmNewPassword, salt);
279
-
280
- await this.prisma.user.update({
281
- where: {
282
- id: user.id,
283
- },
284
- data: {
285
- password,
286
- code: null,
287
- },
288
- });
289
-
290
- await this.mail.send({
291
- to: user.email,
292
- subject: `Senha recuperada`,
293
- body: getResetPasswordEmail(),
294
- });
295
-
296
- return this.getToken(user);
297
- }
298
-
299
- return false;
300
- } catch (error: any) {
301
- throw new BadRequestException(
302
- `Invalid code. ${error?.message ?? String(error)}`,
303
- );
304
- }
305
- }
306
-
307
- async otp({ token, code }: OtpDTO) {
308
- const data = this.jwt.decode(token);
309
-
310
- const user = await this.prisma.user.findFirst({
311
- where: {
312
- id: data['id'],
313
- code: String(code),
314
- },
315
- });
316
-
317
- if (!user) {
318
- throw new NotFoundException('Código inválido');
319
- }
320
-
321
- await this.prisma.user.update({
322
- where: {
323
- id: user.id,
324
- },
325
- data: {
326
- code: null,
327
- },
328
- });
329
-
330
- return this.getToken(user);
331
- }
332
-
333
- async login({ email, password }: LoginDTO) {
334
-
335
- await this.mail.send({
336
- to: email,
337
- subject: `Novo login no CoinBitClub`,
338
- body: getUserLoginEmail(),
339
- });
340
-
341
- return this.loginWithEmailAndPassword(email, password);
342
- }
343
-
344
- async verify(id: number) {
345
- return this.prisma.user.findUnique({
346
- where: { id },
347
- });
348
- }
349
- }
1
+ import { MailService } from '@hedhog/mail';
2
+ import { PrismaService } from '@hedhog/prisma';
3
+ import {
4
+ BadRequestException,
5
+ ConflictException,
6
+ forwardRef,
7
+ Inject,
8
+ Injectable,
9
+ NotFoundException,
10
+ } from '@nestjs/common';
11
+ import { ConfigService } from '@nestjs/config';
12
+ import { JwtService } from '@nestjs/jwt';
13
+ import { compare, genSalt, hash } from 'bcrypt';
14
+ import {
15
+ getChangeEmailEmail,
16
+ getChangePasswordEmail,
17
+ getForgetPasswordEmail,
18
+ getResetPasswordEmail,
19
+ } from '../emails';
20
+ import { ChangeDTO } from './dto/change.dto';
21
+ import { EmailDTO } from './dto/email.dto';
22
+ import { ForgetDTO } from './dto/forget.dto';
23
+ import { LoginDTO } from './dto/login.dto';
24
+ import { OtpDTO } from './dto/otp.dto';
25
+ import { ResetDTO } from './dto/reset.dto';
26
+ import { MultifactorType } from './enums/multifactor-type.enum';
27
+
28
+ @Injectable()
29
+ export class AuthService {
30
+ constructor(
31
+ private readonly configService: ConfigService,
32
+ @Inject(forwardRef(() => PrismaService))
33
+ private readonly prisma: PrismaService,
34
+ @Inject(forwardRef(() => JwtService))
35
+ private readonly jwt: JwtService,
36
+ @Inject(forwardRef(() => MailService))
37
+ private readonly mail: MailService,
38
+ ) {}
39
+
40
+ async verifyToken(token: string) {
41
+ return this.jwt.verifyAsync(token, {
42
+ secret: String(process.env.JWT_SECRET),
43
+ });
44
+ }
45
+
46
+ generateRandomString(length: number): string {
47
+ const characters =
48
+ 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
49
+ let result = '';
50
+ for (let i = 0; i < length; i++) {
51
+ const randomIndex = Math.floor(Math.random() * characters.length);
52
+ result += characters.charAt(randomIndex);
53
+ }
54
+ return result;
55
+ }
56
+
57
+ generateRandomNumber(): number {
58
+ const min = 100000;
59
+ const max = 999999;
60
+ return Math.floor(Math.random() * (max - min + 1)) + min;
61
+ }
62
+
63
+ async loginWithEmailAndPassword(email: string, password: string) {
64
+ const user = await this.prisma.user.findFirst({
65
+ where: {
66
+ email,
67
+ },
68
+ });
69
+
70
+ if (!user) {
71
+ throw new BadRequestException('Acesso negado');
72
+ }
73
+
74
+ const isPasswordValid = await compare(password, user.password);
75
+ if (!isPasswordValid) {
76
+ throw new BadRequestException('Acesso negado');
77
+ }
78
+
79
+ if (!user.multifactor_id) {
80
+ return this.getToken(user);
81
+ } else {
82
+ if (user.multifactor_id === MultifactorType.EMAIL) {
83
+ const code = this.generateRandomNumber();
84
+
85
+ await this.prisma.user.update({
86
+ where: {
87
+ id: user.id,
88
+ },
89
+ data: {
90
+ code: String(code),
91
+ },
92
+ });
93
+
94
+ await this.mail.send({
95
+ to: user.email,
96
+ subject: 'Código de Login',
97
+ body: `Seu código de login é ${code}`,
98
+ });
99
+ }
100
+
101
+ return {
102
+ name: user.name,
103
+ email: user.email,
104
+ token: this.jwt.sign({
105
+ id: user.id,
106
+ mfa: user.multifactor_id,
107
+ }),
108
+ mfa: true,
109
+ };
110
+ }
111
+ }
112
+
113
+ async getToken(user) {
114
+ delete user.password;
115
+
116
+ const payload = { user };
117
+
118
+ return {
119
+ token: this.jwt.sign(payload),
120
+ };
121
+ }
122
+
123
+ async forget({ email }: ForgetDTO) {
124
+ const appUrl =
125
+ process.env.APP_URL ?? this.configService.get<string>('APP_URL');
126
+
127
+ const user = await this.prisma.user.findFirst({
128
+ where: {
129
+ email,
130
+ },
131
+ select: {
132
+ id: true,
133
+ },
134
+ });
135
+
136
+ if (user) {
137
+ const payload = {
138
+ ...user,
139
+ };
140
+
141
+ const code = this.jwt.sign(payload);
142
+
143
+ await this.prisma.user.update({
144
+ where: {
145
+ id: user.id,
146
+ },
147
+ data: {
148
+ code,
149
+ },
150
+ });
151
+
152
+ await this.mail.send({
153
+ to: email,
154
+ subject: `Recuperação de Senha`,
155
+ body: getForgetPasswordEmail(
156
+ `${appUrl}/login?mode=reset-password&code=${code}`,
157
+ ),
158
+ });
159
+ }
160
+
161
+ return {
162
+ message:
163
+ 'Se este e-mail estiver cadastrado, você receberá instruções para redefinir sua senha.',
164
+ };
165
+ }
166
+
167
+ async changePassword({
168
+ email,
169
+ currentPassword,
170
+ newPassword,
171
+ confirmNewPassword,
172
+ }: ChangeDTO) {
173
+ if (newPassword !== confirmNewPassword) {
174
+ throw new BadRequestException('Senhas não conferem');
175
+ }
176
+
177
+ const user = await this.prisma.user.findFirst({
178
+ where: { email },
179
+ });
180
+
181
+ if (!(await compare(currentPassword, user.password))) {
182
+ throw new NotFoundException('Não foi possível alterar a senha.');
183
+ }
184
+
185
+ const salt = await genSalt();
186
+ const password = await hash(newPassword, salt);
187
+
188
+ const newUser = await this.prisma.user.update({
189
+ where: {
190
+ id: user.id,
191
+ },
192
+ data: {
193
+ password,
194
+ },
195
+ });
196
+
197
+ await this.mail.send({
198
+ to: email,
199
+ subject: `Senha alterada`,
200
+ body: getChangePasswordEmail(),
201
+ });
202
+
203
+ return this.getToken(newUser);
204
+ }
205
+
206
+ async changeEmail({ currentEmail, password, newEmail }: EmailDTO) {
207
+ const user = await this.prisma.user.findFirst({
208
+ where: { email: currentEmail },
209
+ });
210
+
211
+ if (!user) {
212
+ throw new BadRequestException('Não foi possível atualizar o e-mail.');
213
+ }
214
+
215
+ if (!(await compare(password, user.password))) {
216
+ throw new BadRequestException('Não foi possível atualizar o e-mail.');
217
+ }
218
+
219
+ const existingUser = await this.prisma.user.findFirst({
220
+ where: { email: newEmail },
221
+ });
222
+
223
+ if (existingUser) {
224
+ throw new ConflictException('Não foi possível atualizar o e-mail.');
225
+ }
226
+
227
+ const newUser = await this.prisma.user.updateMany({
228
+ where: { email: currentEmail },
229
+ data: { email: newEmail },
230
+ });
231
+
232
+ const personUser = await this.prisma.person_user.findFirst({
233
+ where: { user_id: user.id },
234
+ select: { person_id: true },
235
+ });
236
+
237
+ if (!personUser) {
238
+ throw new NotFoundException('Erro ao atualizar os dados do usuário.');
239
+ }
240
+
241
+ const { id: emailContactTypeId } =
242
+ await this.prisma.person_contact_type.findFirst({
243
+ where: { slug: 'EMAIL' },
244
+ });
245
+
246
+ await this.prisma.person_contact.updateMany({
247
+ where: {
248
+ person_id: personUser.person_id,
249
+ type_id: emailContactTypeId,
250
+ },
251
+ data: { value: newEmail },
252
+ });
253
+
254
+ await this.mail.send({
255
+ to: newEmail,
256
+ subject: `Email alterado`,
257
+ body: getChangeEmailEmail(),
258
+ });
259
+
260
+ return this.getToken(newUser);
261
+ }
262
+
263
+ async resetPassword({ code, newPassword, confirmNewPassword }: ResetDTO) {
264
+ if (newPassword !== confirmNewPassword) {
265
+ throw new BadRequestException('Senhas não conferem');
266
+ }
267
+
268
+ try {
269
+ const decodedCode = this.jwt.decode(code);
270
+
271
+ console.log({ decodedCode });
272
+
273
+ const { id } = decodedCode;
274
+
275
+ const user = await this.prisma.user.findFirst({
276
+ where: {
277
+ id,
278
+ code,
279
+ },
280
+ });
281
+
282
+ if (user) {
283
+ const salt = await genSalt();
284
+ const password = await hash(confirmNewPassword, salt);
285
+
286
+ await this.prisma.user.update({
287
+ where: {
288
+ id: user.id,
289
+ },
290
+ data: {
291
+ password,
292
+ code: null,
293
+ },
294
+ });
295
+
296
+ await this.mail.send({
297
+ to: user.email,
298
+ subject: `Senha recuperada`,
299
+ body: getResetPasswordEmail(),
300
+ });
301
+
302
+ return this.getToken(user);
303
+ }
304
+
305
+ return false;
306
+ } catch (error: any) {
307
+ throw new BadRequestException(
308
+ `Invalid code. ${error?.message ?? String(error)}`,
309
+ );
310
+ }
311
+ }
312
+
313
+ async otp({ token, code }: OtpDTO) {
314
+ const data = this.jwt.decode(token);
315
+
316
+ const user = await this.prisma.user.findFirst({
317
+ where: {
318
+ id: data['id'],
319
+ code: String(code),
320
+ },
321
+ });
322
+
323
+ if (!user) {
324
+ throw new NotFoundException('Código inválido');
325
+ }
326
+
327
+ await this.prisma.user.update({
328
+ where: {
329
+ id: user.id,
330
+ },
331
+ data: {
332
+ code: null,
333
+ },
334
+ });
335
+
336
+ return this.getToken(user);
337
+ }
338
+
339
+ async login({ email, password }: LoginDTO) {
340
+ /*
341
+ await this.mail.send({
342
+ to: email,
343
+ subject: `Novo login no CoinBitClub`,
344
+ body: getUserLoginEmail(),
345
+ });
346
+ */
347
+ return this.loginWithEmailAndPassword(email, password);
348
+ }
349
+
350
+ async verify(id: number) {
351
+ return this.prisma.user.findUnique({
352
+ where: { id },
353
+ });
354
+ }
355
+ }