From 099068fbf77b71b64e556d76e4ca5263d6b523ca Mon Sep 17 00:00:00 2001 From: Ivan Alglave <ivanalglave@outlook.fr> Date: Sun, 11 Dec 2022 15:14:41 +0100 Subject: [PATCH] fix: PUT not outputs and saves correctly the internship --- src/internships/dao/internships.dao.ts | 8 +++++--- src/shared/HttpError.ts | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/internships/dao/internships.dao.ts b/src/internships/dao/internships.dao.ts index be98077..091d4d5 100644 --- a/src/internships/dao/internships.dao.ts +++ b/src/internships/dao/internships.dao.ts @@ -1,7 +1,7 @@ import { Injectable, NotFoundException } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; -import { CONFLICT } from 'src/shared/HttpError'; +import { BAD_REQUEST, CONFLICT, INTERNAL } from 'src/shared/HttpError'; import { STATE_STUDENT_ENTERS_INTERNSHIP_INFORMATION } from 'src/shared/InternshipState'; import { CreateInternshipDto } from '../dto/create-internship.dto'; import { InternshipDto } from '../dto/internship.dto'; @@ -58,6 +58,7 @@ export class InternshipDao { ): Promise<Internship | void> => new Promise((resolve, reject) => { // Check if information modification is allowed -> current state is information input by student and updating is allowed + if (studentId !== internship.studentId) reject(BAD_REQUEST); const decoratedInternship = this.toInternshipDtoWithTracking(internship); this._internshipModel.findOneAndReplace( { @@ -71,7 +72,8 @@ export class InternshipDao { }, (err, value) => { if (err) reject(err.message); - resolve(value); + // if (typeof value !== typeof Internship) reject(INTERNAL); + resolve(value as Internship); }, ); }); @@ -90,7 +92,7 @@ export class InternshipDao { return { ...createInternshipDto, tracking: { - state: 'state-1', + state: STATE_STUDENT_ENTERS_INTERNSHIP_INFORMATION, }, }; }; diff --git a/src/shared/HttpError.ts b/src/shared/HttpError.ts index 70f9310..aa1d356 100644 --- a/src/shared/HttpError.ts +++ b/src/shared/HttpError.ts @@ -3,10 +3,14 @@ import { NotFoundException, ConflictException, UnprocessableEntityException, + BadRequestException, + InternalServerErrorException, } from '@nestjs/common'; export const NOT_FOUND = new NotFoundException(); export const CONFLICT = new ConflictException(); +export const BAD_REQUEST = new BadRequestException(); +export const INTERNAL = new InternalServerErrorException(); export const BAD_TRACKING_STATE = (badState: string) => new UnprocessableEntityException(`Unknown state [${badState}]`); export const CUSTOM = (reason: string, errorStatus: number) => -- GitLab