Skip to content

Commit

Permalink
Add getUser endpoint (#10)
Browse files Browse the repository at this point in the history
* worked on getUser method and user parameters
---------

Co-authored-by: Kenny Jung <[email protected]>
Co-authored-by: Kenny Jung <[email protected]>
  • Loading branch information
3 people authored Oct 23, 2023
1 parent a72c127 commit c56ccc5
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
2 changes: 1 addition & 1 deletion apps/backend/src/app/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { PluralNamingStrategy } from '../strategies/plural-naming.strategy';
imports: [
TypeOrmModule.forRoot({
type: 'mongodb',
host: 'localhost',
host: '127.0.0.1',
port: 27017,
database: 'c4cOpsTest',
// username: 'root',
Expand Down
5 changes: 4 additions & 1 deletion apps/backend/src/users/user.entity.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { IsEmail, IsUrl } from 'class-validator';
import { Entity, Column } from 'typeorm';
import { Status, Role, Team } from './types';
import { Role, Status, Team } from './types';

@Entity()
export class User {
Expand All @@ -16,12 +17,14 @@ export class User {
lastName: string;

@Column()
@IsEmail()
email: string;

@Column()
profilePicture: string | null;

@Column()
@IsUrl()
linkedin: string | null;

@Column()
Expand Down
7 changes: 6 additions & 1 deletion apps/backend/src/users/users.controller.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
import {
DefaultValuePipe,
ParseBoolPipe,
ParseIntPipe,
Query,
Body,
Controller,
Get,
Param,
Patch,
ParseIntPipe,
} from '@nestjs/common';
import { UpdateUserDTO } from './update-user.dto';
import { UsersService } from './users.service';
Expand All @@ -25,6 +25,11 @@ export class UsersController {
return this.usersService.findAll(getAllMembers);
}

@Get('/:userId')
getUser(@Param('userId', ParseIntPipe) userId: number) {
return this.usersService.findOne(userId);
}

@Patch(':userId')
async updateUser(
@Body() updateUserDTO: UpdateUserDTO,
Expand Down
35 changes: 34 additions & 1 deletion apps/backend/src/users/users.service.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import {
Injectable,
BadRequestException,
Injectable,
UnauthorizedException,
} from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
Expand Down Expand Up @@ -35,6 +35,39 @@ export class UsersService {
return users;
}

async findOne(userId: number) {
const user = await this.usersRepository.findOneBy({ userId });

if (!user) {
throw new BadRequestException('User not found');
}

const currentUser = getCurrentUser();

const currentStatus = currentUser.status;
const targetStatus = user.status;
switch (currentStatus) {
//admin can access all users
case Status.ADMIN:
break;
//recruiter can access applicant, and themselves
case Status.RECRUITER:
if (targetStatus == Status.APPLICANT) {
break;
} else if (currentUser.userId !== user.userId) {
throw new BadRequestException('User not found');
}
break;
//everyone else can only access themselves
default:
if (currentUser.userId !== user.userId) {
throw new BadRequestException('User not found');
}
}

return user;
}

async updateUser(
updateUserDTO: UpdateUserDTO,
userId: number,
Expand Down
2 changes: 1 addition & 1 deletion apps/backend/src/users/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Status } from './types';
import { User } from './user.entity';

export const getCurrentUser = (): User => ({
userId: 999,
userId: 1,
status: Status.ADMIN,
firstName: 'jimmy',
lastName: 'jimmy2',
Expand Down

0 comments on commit c56ccc5

Please sign in to comment.