From bf6384733004d96e168465b93bbddd4d57375715 Mon Sep 17 00:00:00 2001 From: 9sneha-n <9sneha.n@gmail.com> Date: Fri, 28 Jun 2024 13:57:11 +0530 Subject: [PATCH] refactor: file name change, getGrade as readonly, use Either for error, add role to team member --- ...aseOutbreak.ts => DiseaseOutbreakEvent.ts} | 3 +- .../IncidentManagementTeam.ts | 11 +------- .../incident-management-team/TeamMember.ts | 7 +++-- .../risk-assessment/RiskAssessmentGrading.ts | 28 +++++++++++-------- .../__tests__/RiskAssessmentGrading.spec.ts | 18 ++++++------ 5 files changed, 32 insertions(+), 35 deletions(-) rename src/domain/entities/{DiseaseOutbreak.ts => DiseaseOutbreakEvent.ts} (96%) diff --git a/src/domain/entities/DiseaseOutbreak.ts b/src/domain/entities/DiseaseOutbreakEvent.ts similarity index 96% rename from src/domain/entities/DiseaseOutbreak.ts rename to src/domain/entities/DiseaseOutbreakEvent.ts index a6ccc6e4..62bd4e29 100644 --- a/src/domain/entities/DiseaseOutbreak.ts +++ b/src/domain/entities/DiseaseOutbreakEvent.ts @@ -5,6 +5,7 @@ import { TeamMember } from "./incident-management-team/TeamMember"; import { OrgUnit } from "./OrgUnit"; import { CodedNamedRef, NamedRef } from "./Ref"; import { RiskAssessment } from "./risk-assessment/RiskAssessment"; +import { Maybe } from "../../utils/ts-utils"; type HazardType = | "Biological:Human" @@ -42,7 +43,7 @@ type DiseaseOutbreakEventAttrs = NamedRef & { notified: DateWithNarrative; responseNarrative: string; incidentManager: TeamMember; - notes: string; + notes: Maybe; riskAssessments: RiskAssessment[]; IncidentActionPlan: IncidentActionPlan; IncidentManagementTeam: IncidentManagementTeam; diff --git a/src/domain/entities/incident-management-team/IncidentManagementTeam.ts b/src/domain/entities/incident-management-team/IncidentManagementTeam.ts index ba0b2832..cb9298bf 100644 --- a/src/domain/entities/incident-management-team/IncidentManagementTeam.ts +++ b/src/domain/entities/incident-management-team/IncidentManagementTeam.ts @@ -1,17 +1,8 @@ -import { NamedRef } from "../Ref"; import { Struct } from "../generic/Struct"; import { TeamMember } from "./TeamMember"; -type TeamRole = NamedRef & { - level: number; -}; - -interface RoleTeamMemberMap { - role: TeamRole; - teamMember: TeamMember; -} interface IncidentManagementTeamAttrs { - teamHierarchy: RoleTeamMemberMap[]; + teamHierarchy: TeamMember[]; } export class IncidentManagementTeam extends Struct() {} diff --git a/src/domain/entities/incident-management-team/TeamMember.ts b/src/domain/entities/incident-management-team/TeamMember.ts index 15d3a98b..4e77678e 100644 --- a/src/domain/entities/incident-management-team/TeamMember.ts +++ b/src/domain/entities/incident-management-team/TeamMember.ts @@ -4,14 +4,17 @@ import { Struct } from "../generic/Struct"; type PhoneNumber = string; type Email = string; type IncidentManagerStatus = "Available" | "Unavailable"; -type Postion = string; //TO DO : make a list once available from client. + +export type TeamRole = NamedRef & { + level: number; +}; interface TeamMemberAttrs extends NamedRef { - position: Postion; phone: PhoneNumber; email: Email; status: IncidentManagerStatus; photo: URL; + role: TeamRole; } export class TeamMember extends Struct() { diff --git a/src/domain/entities/risk-assessment/RiskAssessmentGrading.ts b/src/domain/entities/risk-assessment/RiskAssessmentGrading.ts index 92d9be49..b81ba2eb 100644 --- a/src/domain/entities/risk-assessment/RiskAssessmentGrading.ts +++ b/src/domain/entities/risk-assessment/RiskAssessmentGrading.ts @@ -1,13 +1,12 @@ import i18n from "@eyeseetea/feedback-component/locales"; import { Ref } from "../Ref"; import { Struct } from "../generic/Struct"; -import { Maybe } from "../../../utils/ts-utils"; +import { Either } from "../generic/Either"; type WeightedOptions = { label: "Low" | "Medium" | "High"; weight: 1 | 2 | 3; }; - export const LowWeightedOption: WeightedOptions = { label: "Low", weight: 1, @@ -92,7 +91,6 @@ interface RiskAssessmentGradingAttrs extends Ref { capacity: CapacityOptions; reputationalRisk: WeightedOptions; severity: WeightedOptions; - grade: Maybe; } export class RiskAssessmentGrading extends Struct() { @@ -101,11 +99,14 @@ export class RiskAssessmentGrading extends Struct() } public static create(attrs: RiskAssessmentGradingAttrs): RiskAssessmentGrading { - const riskAssessmentGrading = new RiskAssessmentGrading(attrs); - return riskAssessmentGrading._update({ grade: riskAssessmentGrading.calculateGrade() }); + return new RiskAssessmentGrading(attrs); } - calculateGrade(): Grade { + getGrade = (): Either => { + return this.calculateGrade(); + }; + + calculateGrade(): Either { const totalWeight = this.populationAtRisk.weight + this.attackRate.weight + @@ -115,12 +116,15 @@ export class RiskAssessmentGrading extends Struct() this.reputationalRisk.weight + this.severity.weight; - if (totalWeight > 21) throw new Error(i18n.t("Invalid grade")); + if (totalWeight > 21) return Either.error(new Error(i18n.t("Invalid grade"))); + + const grade: Grade = + totalWeight <= 7 + ? "Grade 1" + : totalWeight > 7 && totalWeight <= 14 + ? "Grade 2" + : "Grade 3"; - return totalWeight <= 7 - ? "Grade 1" - : totalWeight > 7 && totalWeight <= 14 - ? "Grade 2" - : "Grade 3"; + return Either.success(grade); } } diff --git a/src/domain/entities/risk-assessment/__tests__/RiskAssessmentGrading.spec.ts b/src/domain/entities/risk-assessment/__tests__/RiskAssessmentGrading.spec.ts index af543c81..d1564b7a 100644 --- a/src/domain/entities/risk-assessment/__tests__/RiskAssessmentGrading.spec.ts +++ b/src/domain/entities/risk-assessment/__tests__/RiskAssessmentGrading.spec.ts @@ -27,10 +27,9 @@ describe("RiskAssessmentGrading", () => { capacity: LowCapacity, reputationalRisk: LowWeightedOption, severity: LowWeightedOption, - grade: undefined, }); - - expect(riskAssessmentGrading.grade).toBe("Grade 1"); + const grade = riskAssessmentGrading.getGrade().value.data; + expect(grade).toBe("Grade 1"); }); it("should be Grade2 if total weight is greater than 7 and less than equal to 14", () => { @@ -44,10 +43,9 @@ describe("RiskAssessmentGrading", () => { capacity: MediumCapacity, reputationalRisk: MediumWeightedOption, severity: MediumWeightedOption, - grade: undefined, }); - - expect(riskAssessmentGrading.grade).toBe("Grade 2"); + const grade = riskAssessmentGrading.getGrade().value.data; + expect(grade).toBe("Grade 2"); }); it("should be Grade3 if score is greater than 14", () => { @@ -61,10 +59,10 @@ describe("RiskAssessmentGrading", () => { capacity: HighCapacity, reputationalRisk: HighWeightedOption, severity: HighWeightedOption, - grade: undefined, }); - expect(riskAssessmentGrading.grade).toBe("Grade 3"); + const grade = riskAssessmentGrading.getGrade().value.data; + expect(grade).toBe("Grade 3"); }); it("should be Grade3 if score is greater than 14", () => { @@ -78,9 +76,9 @@ describe("RiskAssessmentGrading", () => { capacity: LowCapacity, reputationalRisk: HighWeightedOption, severity: HighWeightedOption, - grade: undefined, }); - expect(riskAssessmentGrading.grade).toBe("Grade 3"); + const grade = riskAssessmentGrading.getGrade().value.data; + expect(grade).toBe("Grade 3"); }); });