File tree Expand file tree Collapse file tree 5 files changed +46
-5
lines changed Expand file tree Collapse file tree 5 files changed +46
-5
lines changed Original file line number Diff line number Diff line change @@ -10,7 +10,7 @@ import { PluralNamingStrategy } from '../strategies/plural-naming.strategy';
1010 imports : [
1111 TypeOrmModule . forRoot ( {
1212 type : 'mongodb' ,
13- host : 'localhost ' ,
13+ host : '127.0.0.1 ' ,
1414 port : 27017 ,
1515 database : 'c4cOpsTest' ,
1616 // username: 'root',
Original file line number Diff line number Diff line change 1+ import { IsEmail , IsUrl } from 'class-validator' ;
12import { Entity , Column } from 'typeorm' ;
2- import { Status , Role , Team } from './types' ;
3+ import { Role , Status , Team } from './types' ;
34
45@Entity ( )
56export class User {
@@ -16,12 +17,14 @@ export class User {
1617 lastName : string ;
1718
1819 @Column ( )
20+ @IsEmail ( )
1921 email : string ;
2022
2123 @Column ( )
2224 profilePicture : string | null ;
2325
2426 @Column ( )
27+ @IsUrl ( )
2528 linkedin : string | null ;
2629
2730 @Column ( )
Original file line number Diff line number Diff line change 11import {
22 DefaultValuePipe ,
33 ParseBoolPipe ,
4+ ParseIntPipe ,
45 Query ,
56 Body ,
67 Controller ,
78 Get ,
89 Param ,
910 Patch ,
10- ParseIntPipe ,
1111} from '@nestjs/common' ;
1212import { UpdateUserDTO } from './update-user.dto' ;
1313import { UsersService } from './users.service' ;
@@ -25,6 +25,11 @@ export class UsersController {
2525 return this . usersService . findAll ( getAllMembers ) ;
2626 }
2727
28+ @Get ( '/:userId' )
29+ getUser ( @Param ( 'userId' , ParseIntPipe ) userId : number ) {
30+ return this . usersService . findOne ( userId ) ;
31+ }
32+
2833 @Patch ( ':userId' )
2934 async updateUser (
3035 @Body ( ) updateUserDTO : UpdateUserDTO ,
Original file line number Diff line number Diff line change 11import {
2- Injectable ,
32 BadRequestException ,
3+ Injectable ,
44 UnauthorizedException ,
55} from '@nestjs/common' ;
66import { InjectRepository } from '@nestjs/typeorm' ;
@@ -35,6 +35,39 @@ export class UsersService {
3535 return users ;
3636 }
3737
38+ async findOne ( userId : number ) {
39+ const user = await this . usersRepository . findOneBy ( { userId } ) ;
40+
41+ if ( ! user ) {
42+ throw new BadRequestException ( 'User not found' ) ;
43+ }
44+
45+ const currentUser = getCurrentUser ( ) ;
46+
47+ const currentStatus = currentUser . status ;
48+ const targetStatus = user . status ;
49+ switch ( currentStatus ) {
50+ //admin can access all users
51+ case Status . ADMIN :
52+ break ;
53+ //recruiter can access applicant, and themselves
54+ case Status . RECRUITER :
55+ if ( targetStatus == Status . APPLICANT ) {
56+ break ;
57+ } else if ( currentUser . userId !== user . userId ) {
58+ throw new BadRequestException ( 'User not found' ) ;
59+ }
60+ break ;
61+ //everyone else can only access themselves
62+ default :
63+ if ( currentUser . userId !== user . userId ) {
64+ throw new BadRequestException ( 'User not found' ) ;
65+ }
66+ }
67+
68+ return user ;
69+ }
70+
3871 async updateUser (
3972 updateUserDTO : UpdateUserDTO ,
4073 userId : number ,
Original file line number Diff line number Diff line change @@ -2,7 +2,7 @@ import { Status } from './types';
22import { User } from './user.entity' ;
33
44export const getCurrentUser = ( ) : User => ( {
5- userId : 999 ,
5+ userId : 1 ,
66 status : Status . ADMIN ,
77 firstName : 'jimmy' ,
88 lastName : 'jimmy2' ,
You can’t perform that action at this time.
0 commit comments