Skip to content

Commit c56ccc5

Browse files
Add getUser endpoint (#10)
* worked on getUser method and user parameters --------- Co-authored-by: Kenny Jung <dukyoung01@naver.com> Co-authored-by: Kenny Jung <kenny.jung@rapyuta-robotics.com>
1 parent a72c127 commit c56ccc5

File tree

5 files changed

+46
-5
lines changed

5 files changed

+46
-5
lines changed

apps/backend/src/app/app.module.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff 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',

apps/backend/src/users/user.entity.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
1+
import { IsEmail, IsUrl } from 'class-validator';
12
import { Entity, Column } from 'typeorm';
2-
import { Status, Role, Team } from './types';
3+
import { Role, Status, Team } from './types';
34

45
@Entity()
56
export 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()

apps/backend/src/users/users.controller.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import {
22
DefaultValuePipe,
33
ParseBoolPipe,
4+
ParseIntPipe,
45
Query,
56
Body,
67
Controller,
78
Get,
89
Param,
910
Patch,
10-
ParseIntPipe,
1111
} from '@nestjs/common';
1212
import { UpdateUserDTO } from './update-user.dto';
1313
import { 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,

apps/backend/src/users/users.service.ts

Lines changed: 34 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
import {
2-
Injectable,
32
BadRequestException,
3+
Injectable,
44
UnauthorizedException,
55
} from '@nestjs/common';
66
import { 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,

apps/backend/src/users/utils.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import { Status } from './types';
22
import { User } from './user.entity';
33

44
export const getCurrentUser = (): User => ({
5-
userId: 999,
5+
userId: 1,
66
status: Status.ADMIN,
77
firstName: 'jimmy',
88
lastName: 'jimmy2',

0 commit comments

Comments
 (0)