diff --git a/packages/api-v2/src/major/major.service.ts b/packages/api-v2/src/major/major.service.ts index 7867ceb3a..eba5acd77 100644 --- a/packages/api-v2/src/major/major.service.ts +++ b/packages/api-v2/src/major/major.service.ts @@ -141,6 +141,29 @@ export class MajorService { return true; } + isValidCatalogueYear( + majorName: string, + catalogYear: number, + concentrationName: string + ):boolean{ + + const majorsByCatalogue = this.findByMajorAndYear(majorName, catalogYear); + + if(!majorsByCatalogue){ + this.logger.debug( + { + message: "Invalid catalogue year for major", + majorName, + catalogYear, + concentrationName, + }, + MajorService.formatMajorServiceCtx("isValidCatalogueYear") + ) + return false; + } + return true; + } + private static formatMajorServiceCtx(methodName: string) { return formatServiceCtx(MajorService.name, methodName); } diff --git a/packages/api-v2/src/plan/plan.errors.ts b/packages/api-v2/src/plan/plan.errors.ts new file mode 100644 index 000000000..626c4c3f9 --- /dev/null +++ b/packages/api-v2/src/plan/plan.errors.ts @@ -0,0 +1,21 @@ +// InvalidMajor +// InvalidCatalogYear +// InvalidConcentration + +export class InvalidMajor extends Error { + constructor() { + super(); + } +} + +export class InvalidCatalogYear extends Error { + constructor() { + super(); + } +} + +export class InvalidConcentration extends Error { + constructor() { + super(); + } +} diff --git a/packages/api-v2/src/plan/plan.service.ts b/packages/api-v2/src/plan/plan.service.ts index 98f408b89..027a2039d 100644 --- a/packages/api-v2/src/plan/plan.service.ts +++ b/packages/api-v2/src/plan/plan.service.ts @@ -7,6 +7,7 @@ import { CreatePlanDto, UpdatePlanDto } from "@graduate/common"; import { Plan } from "./entities/plan.entity"; import { formatServiceCtx } from "../../src/utils"; import { MajorService } from "../major/major.service"; +import { InvalidCatalogYear, InvalidConcentration, InvalidMajor } from "./plan.errors"; @Injectable() export class PlanService { @@ -140,8 +141,8 @@ export class PlanService { * TODO: Fix the DTO issue that populates undefined values for fields not * present. https://github.com/sandboxnu/graduatenu/issues/533 */ - const isMajorInfoUpdate = - newMajorName && newCatalogYear && newConcentrationName; + // It is necessary for this to be OR because we need to run an update if any of these are true. + const isMajorInfoUpdate = newMajorName || newCatalogYear || newConcentrationName; /** Wipe Major => Remove existing major from the plan. */ const isWipeMajorUpdate = @@ -159,7 +160,6 @@ export class PlanService { { message: "Either update all major fields or only the schedule", id }, this.formatPlanServiceCtx("update") ); - return null; } // validate the major info if major is being updated @@ -179,16 +179,36 @@ export class PlanService { }, this.formatPlanServiceCtx("update") ); + throw new InvalidMajor(); + } + + + const isValidMajorCatalogueYear = this.majorService.isValidCatalogueYear( + newMajorName, + newCatalogYear, + newConcentrationName + ); + + if(!isValidMajorCatalogueYear){ + this.logger.debug( + { + message: "Attempting to add plan with an invalid catalogue year", + newMajorName, + newCatalogYear, + }, + this.formatPlanServiceCtx("update") + ); + throw new InvalidCatalogYear(); return null; } const isValidConcentrationForMajor = - this.majorService.isValidConcentrationForMajor( - newMajorName, - newCatalogYear, - newConcentrationName - ); + this.majorService.isValidConcentrationForMajor( + newMajorName, + newCatalogYear, + newConcentrationName + ); if (!isValidConcentrationForMajor) { this.logger.debug( @@ -201,7 +221,7 @@ export class PlanService { this.formatPlanServiceCtx("update") ); - return null; + throw new InvalidConcentration(); } }