Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add getUser endpoint #10

Merged
merged 5 commits into from
Oct 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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;
ananyar807 marked this conversation as resolved.
Show resolved Hide resolved

@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');
ananyar807 marked this conversation as resolved.
Show resolved Hide resolved
}

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) {
ananyar807 marked this conversation as resolved.
Show resolved Hide resolved
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
Loading