1- import { Injectable , NotFoundException } from '@nestjs/common' ;
1+ import {
2+ Injectable ,
3+ BadRequestException ,
4+ UnauthorizedException ,
5+ NotFoundException ,
6+ } from '@nestjs/common' ;
27import { InjectRepository } from '@nestjs/typeorm' ;
3- import { Repository } from 'typeorm' ;
4-
8+ import { MongoRepository } from 'typeorm' ;
59import { User } from './user.entity' ;
10+ import { UpdateUserDTO } from './update-user.dto' ;
611import { Status } from './types' ;
12+ import { getCurrentUser } from './utils' ;
713
814@Injectable ( )
915export class UsersService {
10- constructor ( @InjectRepository ( User ) private repo : Repository < User > ) { }
16+ constructor (
17+ @InjectRepository ( User )
18+ private usersRepository : MongoRepository < User > ,
19+ ) { }
1120
1221 async create ( email : string , firstName : string , lastName : string ) {
13- const userId = ( await this . repo . count ( ) ) + 1 ;
14- const user = this . repo . create ( {
15- id : userId ,
16- status : Status . STANDARD ,
22+ const userId = ( await this . usersRepository . count ( ) ) + 1 ;
23+ const user = this . usersRepository . create ( {
24+ userId,
25+ status : Status . MEMBER ,
1726 firstName,
1827 lastName,
1928 email,
2029 } ) ;
2130
22- return this . repo . save ( user ) ;
31+ return this . usersRepository . save ( user ) ;
2332 }
2433
25- findOne ( id : number ) {
26- if ( ! id ) {
27- return null ;
34+ async findAll ( getAllMembers : boolean ) : Promise < User [ ] > {
35+ if ( ! getAllMembers ) return [ ] ;
36+
37+ const currentUser = getCurrentUser ( ) ;
38+
39+ if ( currentUser . status === Status . APPLICANT ) {
40+ throw new UnauthorizedException ( ) ;
2841 }
2942
30- return this . repo . findOneBy ( { id } ) ;
43+ const users : User [ ] = await this . usersRepository . find ( {
44+ where : {
45+ status : { $not : { $eq : Status . APPLICANT } } ,
46+ } ,
47+ } ) ;
48+
49+ return users ;
50+ }
51+
52+ async findOne ( userId : number ) {
53+ const user = await this . usersRepository . findOneBy ( { userId } ) ;
54+
55+ if ( ! user ) {
56+ throw new BadRequestException ( 'User not found' ) ;
57+ }
58+
59+ const currentUser = getCurrentUser ( ) ;
60+
61+ const currentStatus = currentUser . status ;
62+ const targetStatus = user . status ;
63+ switch ( currentStatus ) {
64+ //admin can access all users
65+ case Status . ADMIN :
66+ break ;
67+ //recruiter can access applicant, and themselves
68+ case Status . RECRUITER :
69+ if ( targetStatus == Status . APPLICANT ) {
70+ break ;
71+ } else if ( currentUser . userId !== user . userId ) {
72+ throw new BadRequestException ( 'User not found' ) ;
73+ }
74+ break ;
75+ //everyone else can only access themselves
76+ default :
77+ if ( currentUser . userId !== user . userId ) {
78+ throw new BadRequestException ( 'User not found' ) ;
79+ }
80+ }
81+
82+ return user ;
83+ }
84+
85+ async updateUser (
86+ updateUserDTO : UpdateUserDTO ,
87+ userId : number ,
88+ ) : Promise < User > {
89+ const user : User = await this . usersRepository . findOne ( {
90+ where : {
91+ userId : { $eq : userId } ,
92+ } ,
93+ } ) ;
94+
95+ if ( ! user ) {
96+ throw new BadRequestException ( `User ${ userId } not found.` ) ;
97+ }
98+
99+ const currentUser = getCurrentUser ( ) ;
100+
101+ if ( currentUser . status !== Status . ADMIN && userId !== currentUser . userId ) {
102+ throw new UnauthorizedException ( ) ;
103+ }
104+
105+ await this . usersRepository . update ( { userId } , updateUserDTO ) ;
106+ return await this . usersRepository . findOne ( {
107+ where : {
108+ userId : { $eq : userId } ,
109+ } ,
110+ } ) ;
31111 }
32112
113+ /* TODO merge these methods with the above methods */
33114 find ( email : string ) {
34- return this . repo . find ( { where : { email } } ) ;
115+ return this . usersRepository . find ( { where : { email } } ) ;
35116 }
36117
37118 async update ( id : number , attrs : Partial < User > ) {
@@ -43,7 +124,7 @@ export class UsersService {
43124
44125 Object . assign ( user , attrs ) ;
45126
46- return this . repo . save ( user ) ;
127+ return this . usersRepository . save ( user ) ;
47128 }
48129
49130 async remove ( id : number ) {
@@ -53,6 +134,7 @@ export class UsersService {
53134 throw new NotFoundException ( 'User not found' ) ;
54135 }
55136
56- return this . repo . remove ( user ) ;
137+ return this . usersRepository . remove ( user ) ;
57138 }
139+ /* TODO merge these methods with the above methods */
58140}
0 commit comments