File tree Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Expand file tree Collapse file tree 2 files changed +36
-2
lines changed Original file line number Diff line number Diff line change
1
+ import { AccessLevel } from '@prisma/client' ;
2
+ import z from 'zod' ;
3
+
4
+ import { removeNotNumbers } from '../utils' ;
5
+
6
+ const UserSchema = z . object ( {
7
+ id : z . string ( ) . uuid ( ) ,
8
+ name : z . string ( ) ,
9
+ lastName : z . string ( ) ,
10
+ login : z . string ( ) . transform ( ( v ) => v . toLowerCase ( ) ) ,
11
+ password : z . string ( ) ,
12
+ phone : z . string ( ) . transform ( removeNotNumbers ) ,
13
+ accessLevel : z . nativeEnum ( AccessLevel ) ,
14
+ createdAt : z . string ( ) ,
15
+ updatedAt : z . string ( ) . nullable ( ) . optional ( ) ,
16
+ } ) ;
17
+
18
+ const CreateUserSchema = UserSchema . pick ( {
19
+ name : true ,
20
+ lastName : true ,
21
+ phone : true ,
22
+ } ) ;
23
+
24
+ const UpdateUserSchema = UserSchema . omit ( {
25
+ id : true ,
26
+ accessLevel : true ,
27
+ createdAt : true ,
28
+ updatedAt : true ,
29
+ } ) . partial ( ) ;
30
+
31
+ export { CreateUserSchema , UpdateUserSchema } ;
Original file line number Diff line number Diff line change @@ -3,12 +3,14 @@ import { Injectable } from '@nestjs/common';
3
3
import { PrismaService } from '../prisma/prisma.service' ;
4
4
import { CreateUserDTO } from './dtos/CreateUserDTO' ;
5
5
import { UpdateUserDTO } from './dtos/UpdateUserDTO' ;
6
+ import { CreateUserSchema , UpdateUserSchema } from './types' ;
6
7
7
8
@Injectable ( )
8
9
export class UsersService {
9
10
constructor ( private readonly prismaService : PrismaService ) { }
10
11
11
- async store ( { name, lastName, phone } : CreateUserDTO ) {
12
+ async store ( body : CreateUserDTO ) {
13
+ const { name, lastName, phone } = CreateUserSchema . parse ( body ) ;
12
14
await this . prismaService . user . create ( {
13
15
data : {
14
16
name,
@@ -22,12 +24,13 @@ export class UsersService {
22
24
}
23
25
24
26
async update ( id : string , body : UpdateUserDTO ) {
27
+ const payload = UpdateUserSchema . parse ( body ) ;
25
28
await this . prismaService . user . update ( {
26
29
where : {
27
30
id,
28
31
} ,
29
32
data : {
30
- ...body ,
33
+ ...payload ,
31
34
updatedAt : new Date ( ) . toISOString ( ) ,
32
35
} ,
33
36
} ) ;
You can’t perform that action at this time.
0 commit comments