Skip to content

Commit

Permalink
Merge pull request #627 from sandboxnu/dylans-concentration-update-512
Browse files Browse the repository at this point in the history
Dylans concentration update 512
  • Loading branch information
dkd2101 authored Oct 12, 2023
2 parents bb86a28 + e7ab6b8 commit 0a4c3d5
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 9 deletions.
23 changes: 23 additions & 0 deletions packages/api-v2/src/major/major.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down
21 changes: 21 additions & 0 deletions packages/api-v2/src/plan/plan.errors.ts
Original file line number Diff line number Diff line change
@@ -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();
}
}
38 changes: 29 additions & 9 deletions packages/api-v2/src/plan/plan.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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 =
Expand All @@ -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
Expand All @@ -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(
Expand All @@ -201,7 +221,7 @@ export class PlanService {
this.formatPlanServiceCtx("update")
);

return null;
throw new InvalidConcentration();
}
}

Expand Down

0 comments on commit 0a4c3d5

Please sign in to comment.