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

Ahnfikd7/create accept reject endpoint #57

Merged
merged 5 commits into from
Apr 7, 2024
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
23 changes: 22 additions & 1 deletion apps/backend/src/applications/applications.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ import {
NotFoundException,
UnauthorizedException,
} from '@nestjs/common';
import { Response } from './types';
import { Decision, Response } from './types';
import { ApplicationsService } from './applications.service';
import { CurrentUserInterceptor } from '../interceptors/current-user.interceptor';
import { AuthGuard } from '@nestjs/passport';
Expand Down Expand Up @@ -40,6 +40,27 @@ export class ApplicationsController {
return await this.applicationsService.submitApp(application, user);
}

@Post('/decision/:appId')
@UseGuards(AuthGuard('jwt'))
async makeDecision(
@Param('appId', ParseIntPipe) applicantId: number,
@Body('decision') decision: Decision,
@Request() req,
): Promise<void> {
//Authorization check for admin and recruiters
if (![UserStatus.ADMIN, UserStatus.RECRUITER].includes(req.user.status)) {
throw new UnauthorizedException();
}

//Check if the string decision matches with the Decision enum
const decisionEnum: Decision = Decision[decision];
if (!decisionEnum) {
throw new BadRequestException('Invalid decision value');
}

//Delegate the decision making to the service.
await this.applicationsService.processDecision(applicantId, decisionEnum);
}
@UseGuards(AuthGuard('jwt'))
@Get('/')
async getApplications(
Expand Down
34 changes: 33 additions & 1 deletion apps/backend/src/applications/applications.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,12 @@ import {
getCurrentSemester,
getCurrentYear,
} from './utils';
import { Response } from './types';
import { Decision, Response } from './types';
import * as crypto from 'crypto';
import { User } from '../users/user.entity';
import { Position, ApplicationStage, ApplicationStep } from './types';
import { GetAllApplicationResponseDTO } from './dto/get-all-application.response.dto';
import { stagesMap } from './applications.constants';

@Injectable()
export class ApplicationsService {
Expand Down Expand Up @@ -94,6 +95,37 @@ export class ApplicationsService {
throw new UnauthorizedException();
}

/**
* Updates the application stage of the applicant.
* Moves the stage to either the next stage or to rejected.
*
* @param applicantId the id of the applicant.
* @param decision enum that contains either the applicant was 'ACCEPT' or 'REJECT'
* @returns { void } only updates the stage of the applicant.
*/
async processDecision(
applicantId: number,
decision: Decision,
): Promise<void> {
const application = await this.findCurrent(applicantId);

let newStage: ApplicationStage;
chromium-52 marked this conversation as resolved.
Show resolved Hide resolved
if (decision === Decision.REJECT) {
newStage = ApplicationStage.REJECTED;
} else {
const stagesArr = stagesMap[application.position];
const stageIndex = stagesArr.indexOf(application.stage);
if (stageIndex === -1) {
return;
}
newStage = stagesArr[stageIndex + 1];
}
application.stage = newStage;

//Save the updated stage
await this.applicationsRepository.save(application);
}

async findAll(userId: number): Promise<Application[]> {
const apps = await this.applicationsRepository.find({
where: { user: { id: userId } },
Expand Down
5 changes: 5 additions & 0 deletions apps/backend/src/applications/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,8 @@ export enum Position {
PM = 'PRODUCT_MANAGER',
DESIGNER = 'DESIGNER',
}

export enum Decision {
ACCEPT = 'ACCEPT',
REJECT = 'REJECT',
}
Loading