@@ -10,31 +10,7 @@ import { UserLoginRequestDto } from './dto/user-login-request.dto';
1010
1111@Injectable ( )
1212export class UsersService {
13- constructor ( private prismaService : PrismaService , private authService : AuthService ) {
14- // create default user if there are none in DB
15- this . userList ( ) . then ( userList => {
16- if ( userList . length === 0 ) {
17- const defaultEmail = '[email protected] ' ; 18- const defaultPassword = '123456' ;
19-
20- this . create ( {
21- email : defaultEmail ,
22- password : defaultPassword ,
23- firstName : 'fname' ,
24- lastName : 'lname'
25- } ) . then (
26- user => {
27- console . log ( '###########################' ) ;
28- console . log ( '## CREATING DEFAULT USER ##' ) ;
29- console . log ( '###########################' ) ;
30- console . log ( '' ) ;
31- console . log ( `The user with the email "${ defaultEmail } " and password "${ defaultPassword } " was created` ) ;
32- console . log ( `The Api key is: ${ user . apiKey } ` ) ;
33- }
34- ) ;
35- }
36- } ) ;
37- }
13+ constructor ( private prismaService : PrismaService , private authService : AuthService ) { }
3814
3915 userList ( ) : Promise < User [ ] > {
4016 return this . prismaService . user . findMany ( ) ;
@@ -57,23 +33,20 @@ export class UsersService {
5733 return new UserLoginResponseDto ( userData , null ) ;
5834 } catch ( err ) {
5935 if ( err . original . constraint === 'user_email_key' ) {
60- throw new HttpException (
61- `User with email '${ err . errors [ 0 ] . value } ' already exists` ,
62- HttpStatus . CONFLICT ,
63- ) ;
36+ throw new HttpException ( `User with email '${ err . errors [ 0 ] . value } ' already exists` , HttpStatus . CONFLICT ) ;
6437 }
6538
6639 throw new HttpException ( err , HttpStatus . INTERNAL_SERVER_ERROR ) ;
6740 }
6841 }
6942
7043 async findOne ( id : string ) : Promise < User > {
71- return this . prismaService . user . findOne ( { where : { id } } )
44+ return this . prismaService . user . findOne ( { where : { id } } ) ;
7245 }
7346
7447 async get ( id : string ) : Promise < UserDto > {
75- const user = await this . findOne ( id )
76- return new UserDto ( user )
48+ const user = await this . findOne ( id ) ;
49+ return new UserDto ( user ) ;
7750 }
7851
7952 async update ( id : string , userDto : UpdateUserDto ) : Promise < UserLoginResponseDto > {
@@ -83,51 +56,45 @@ export class UsersService {
8356 email : userDto . email ,
8457 firstName : userDto . firstName ,
8558 lastName : userDto . lastName ,
86- }
87- } )
59+ } ,
60+ } ) ;
8861 const token = this . authService . signToken ( user ) ;
8962 return new UserLoginResponseDto ( user , token ) ;
9063 }
9164
9265 async generateNewApiKey ( user : User ) : Promise < string > {
93- const newApiKey = this . authService . generateApiKey ( )
66+ const newApiKey = this . authService . generateApiKey ( ) ;
9467 await this . prismaService . user . update ( {
9568 where : { id : user . id } ,
9669 data : {
97- apiKey : newApiKey
98- }
99- } )
70+ apiKey : newApiKey ,
71+ } ,
72+ } ) ;
10073 return newApiKey ;
10174 }
10275
10376 async changePassword ( user : User , newPassword : string ) : Promise < boolean > {
10477 await this . prismaService . user . update ( {
10578 where : { id : user . id } ,
10679 data : {
107- password : await this . authService . encryptPassword ( newPassword )
108- }
109- } )
80+ password : await this . authService . encryptPassword ( newPassword ) ,
81+ } ,
82+ } ) ;
11083 return true ;
11184 }
11285
11386 async login ( userLoginRequestDto : UserLoginRequestDto ) {
11487 const user = await this . prismaService . user . findOne ( {
115- where : { email : userLoginRequestDto . email }
116- } )
88+ where : { email : userLoginRequestDto . email } ,
89+ } ) ;
11790 if ( ! user ) {
118- throw new HttpException (
119- 'Invalid email or password.' ,
120- HttpStatus . BAD_REQUEST ,
121- ) ;
91+ throw new HttpException ( 'Invalid email or password.' , HttpStatus . BAD_REQUEST ) ;
12292 }
12393
12494 const isMatch = await this . authService . compare ( userLoginRequestDto . password , user . password ) ;
12595
12696 if ( ! isMatch ) {
127- throw new HttpException (
128- 'Invalid email or password.' ,
129- HttpStatus . BAD_REQUEST ,
130- ) ;
97+ throw new HttpException ( 'Invalid email or password.' , HttpStatus . BAD_REQUEST ) ;
13198 }
13299
133100 const token = this . authService . signToken ( user ) ;
0 commit comments