Skip to content

Commit

Permalink
Merge pull request #263 from icefoganalytics/test
Browse files Browse the repository at this point in the history
Fixing CLSFT Calcs and better errors
  • Loading branch information
datajohnson authored Jun 25, 2024
2 parents 4c46e62 + b882d8d commit 46c71f6
Show file tree
Hide file tree
Showing 3 changed files with 148 additions and 102 deletions.
110 changes: 66 additions & 44 deletions src/api/repositories/assessment/assessment-cslft-repository-v2.ts
Original file line number Diff line number Diff line change
Expand Up @@ -116,38 +116,28 @@ export class AssessmentCslftRepositoryV2 {
): Promise<CSLFTAssessmentBase | undefined> {
await this.load(parseInt(`${fundingRequestId}`));

try {
let assess = await this.calculateBase();
assess.id = parseInt(`${assessmentId}`);
let assess = await this.calculateBase();
assess.id = parseInt(`${assessmentId}`);

await this.calculateCosts(assess);
await this.calculateContribution(assess);
await this.calculateParental(assess);
await this.calculateCosts(assess);
await this.calculateContribution(assess);
await this.calculateParental(assess);

let full = await this.postLoad(assess, this.application.id);
let full = await this.postLoad(assess, this.application.id);

assess.csl_assessed_need = full.csl_assessed_need;
assess.csl_assessed_need = full.csl_assessed_need;

await this.calculateAward(assess);
return assess;
} catch (error) {
console.log("ERROR CREATING:", error);
return undefined;
}
await this.calculateAward(assess);
return assess;
}

async loadExisting(
input: CSLFTAssessmentBase,
applicationId: number | string
): Promise<CSLFTAssessmentFull | undefined> {
try {
await this.load(input.funding_request_id);
let full = await this.postLoad(input, applicationId);
return full;
} catch (error) {
console.log("ERROR CREATING:", error);
return undefined;
}
await this.load(input.funding_request_id);
let full = await this.postLoad(input, applicationId);
return full;
}

async postLoad(base: CSLFTAssessmentBase, applicationId: number | string): Promise<CSLFTAssessmentFull> {
Expand Down Expand Up @@ -256,6 +246,18 @@ export class AssessmentCslftRepositoryV2 {
})
.first();

if (!parentMsol) {
console.log(
"PARENTMSOL NOT FOUND",
this.application.academic_year_id,
parentAddress.province_id,
input.family_size
);

throw Error(
`Cannot find Parent Moderate Standard of living for province: ${parentAddress.province_id} and family size: ${input.family_size}`
);
}
input.parent_msol = parentMsol.standard_living_amount ?? 0;
input.parent_discretionary_income = Math.round(input.parent_net_income_total - input.parent_msol);

Expand All @@ -280,7 +282,11 @@ export class AssessmentCslftRepositoryV2 {
input.parent_discretionary_income
);

contribution = await this.db("sfa.parent_contribution_formula")
throw Error(
`Contribution Formula not found for parent discretionary income: ${input.parent_discretionary_income}`
);

/* contribution = await this.db("sfa.parent_contribution_formula")
.where({ academic_year_id: this.application.academic_year_id })
.first();
Expand All @@ -291,7 +297,7 @@ export class AssessmentCslftRepositoryV2 {
input.parent_contribution = input.parent_weekly_contrib * input.study_weeks;
console.log("INSTEAD USING BASIC: ", contribution);
console.log("INSTEAD USING BASIC: ", contribution); */
}
}
}
Expand Down Expand Up @@ -413,15 +419,14 @@ export class AssessmentCslftRepositoryV2 {
//over_award can only be as big as the calculated = clear partial

input.net_amount =
input.calculated_award -
(input.over_award ?? 0) -
input.assessed_amount /* -
(input.over_award ?? 0) */ -
input.previous_cert -
input.previous_disbursement -
(input.return_uncashable_cert ?? 0);
input.previous_disbursement /* -
(input.return_uncashable_cert ?? 0) */;

input.net_amount = Math.round(input.net_amount * 100) / 100;
input.net_amount = input.net_amount == -0 ? 0 : input.net_amount;

return input;
}

Expand Down Expand Up @@ -544,6 +549,12 @@ export class AssessmentCslftRepositoryV2 {
this.application.csl_classification,
this.application.study_accom_code
);

if (this.application.category_id == -1) {
throw Error(
`Cannot determine student category for csl: ${this.application.csl_classification}, study accomodation: ${this.application.study_accom_code}`
);
}
}

async loadLookups() {
Expand Down Expand Up @@ -609,13 +620,7 @@ export class AssessmentCslftRepositoryV2 {
assess.csl_request_amount = this.fundingRequest.csl_request_amount;
assess.csl_full_amt_flag = this.fundingRequest.is_csl_full_amount == true ? 1 : 0;

let family = await calculateFamilySize(
this.db,
this.application.csl_classification,
this.application.id,
!isEmpty(this.application.parent1_sin),
!isEmpty(this.application.parent2_sin)
);
let family = await calculateFamilySize(this.db, this.application.csl_classification, this.application.id);

assess.student_contribution_review = assess.assessment_type_id === 2 ? "YES" : "NO";
assess.spouse_contribution_review = assess.assessment_type_id === 2 ? "YES" : "NO";
Expand Down Expand Up @@ -884,8 +889,6 @@ export class AssessmentCslftRepositoryV2 {
fr ON fr.id = d.funding_request_id INNER JOIN sfa.application ap on ap.id = fr.application_id WHERE ap.academic_year_id = ${this.application.academic_year_id}
AND fr.request_type_id IN (3,4) AND ap.id = ${this.application.id} AND a.id < ${assess.id} GROUP BY d.assessment_id HAVING SUM(d.disbursed_amount) > 0`);

console.log("PREV", assess.id);

if (previousContributions && previousContributions.length > 0) {
assess.student_previous_contribution = previousContributions.reduce((a: number, i: any) => {
return a + i.student_contribution;
Expand Down Expand Up @@ -998,7 +1001,7 @@ export class AssessmentCslftRepositoryV2 {
return this.studentCategories.find((c: any) => c.code == "SDH")?.id || -1;
} else if (cslClassification == 1 && accomodationCode == 2) {
return this.studentCategories.find((c: any) => c.code == "SDA")?.id || -1;
} else if ([2, 5].includes(cslClassification) && accomodationCode == -1) {
} else if ([2, 5].includes(cslClassification) && accomodationCode == 1) {
return this.studentCategories.find((c: any) => c.code == "SIH")?.id || -1;
} else if ([2, 5].includes(cslClassification ?? 0) && accomodationCode == 2) {
return this.studentCategories.find((c: any) => c.code == "SIA")?.id || -1;
Expand Down Expand Up @@ -1037,9 +1040,7 @@ function cleanDollars(input: number | undefined) {
async function calculateFamilySize(
db: Knex,
classification: number,
applicationId: number,
hasParent1: boolean,
hasParent2: boolean
applicationId: number
): Promise<{
family_size: number;
total_dependants: number;
Expand Down Expand Up @@ -1104,7 +1105,6 @@ async function calculateFamilySize(
family.under12_or_disabled = deps.filter((f: any) => f.is_csl_eligible && (f.age < 11 || f.is_disability)).length;
family.over11 = deps.filter((f: any) => f.is_csl_eligible && f.age >= 12).length;
family.post_secondary = deps.filter((f: any) => f.is_post_secondary).length;

family.family_size = classification == 3 ? 2 + family.csl_dependants : 1 + family.csl_dependants;
}
// Single Dependent
Expand All @@ -1119,9 +1119,31 @@ async function calculateFamilySize(
)
.where({ application_id: applicationId, is_eligible: true });

let parentInfo = await db("sfa.application")
.leftJoin("sfa.person as p1", "application.parent1_id", "p1.id")
.leftJoin("sfa.person as p2", "application.parent2_id", "p2.id")
.where("application.id", applicationId)
.select(
"application.id",
"p1.first_name as p1f",
"p1.last_name as p1l",
"p2.first_name as p2f",
"p2.last_name as p2l"
)
.first();

let hasParent1 = false;
let hasParent2 = false;

if (parentInfo.p1f && parentInfo.p1l) {
hasParent1 = true;
}
if (parentInfo.p2f && parentInfo.p2l) {
hasParent2 = true;
}

family.total_dependants = 1 + parentDeps.length;
family.csl_dependants = 1;

family.post_secondary = parentDeps.filter((f: any) => f.is_attend_post_secondary).length + 1;
family.family_size = 1 + parentDeps.length + (hasParent1 ? 1 : 0) + (hasParent2 ? 1 : 0);

Expand Down
81 changes: 46 additions & 35 deletions src/api/routes/admin/csg-threshold-router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -149,20 +149,26 @@ csgThresholdRouter.put(
const { assessment_id, funding_request_id } = req.params;

let repo = new AssessmentCslftRepositoryV2(db);
let recalc = await repo.create(funding_request_id, assessment_id);

delete (recalc as any).id;
delete (recalc as any).assessment_type_id;
(recalc as any).student_contribution_override = null;
(recalc as any).spouse_contribution_override = null;
(recalc as any).parent_contribution_override = null;
(recalc as any).over_award = null;
(recalc as any).return_uncashable_cert = null;
(recalc as any).csl_non_reason_id = null;
(recalc as any).csl_over_reason_id = null;

await db("sfa.assessment").where({ id: assessment_id }).update(recalc);
return res.status(200).json({ data: "Assessment Saved" });

try {
let recalc = await repo.create(funding_request_id, assessment_id);

delete (recalc as any).id;
delete (recalc as any).assessment_type_id;
(recalc as any).student_contribution_override = null;
(recalc as any).spouse_contribution_override = null;
(recalc as any).parent_contribution_override = null;
(recalc as any).over_award = null;
(recalc as any).return_uncashable_cert = null;
(recalc as any).csl_non_reason_id = null;
(recalc as any).csl_over_reason_id = null;

await db("sfa.assessment").where({ id: assessment_id }).update(recalc);
return res.status(200).json({ data: "Assessment Saved" });
} catch (error) {
console.log("CATCHING", error);
return res.status(400).json({ error });
}
}
);

Expand Down Expand Up @@ -285,29 +291,34 @@ csgThresholdRouter.get(
.first();
}

let repo = new AssessmentCslftRepositoryV2(db);
// if there aren't any yet, build a new one
if (!assessment) {
assessment = await repo.create(funding_request.id);
try {
let repo = new AssessmentCslftRepositoryV2(db);
// if there aren't any yet, build a new one
if (!assessment) {
assessment = await repo.create(funding_request.id);

if (assessment) {
assessment = await repo.loadExisting(assessment, application_id);
delete (assessment as any).id;
return res.json({ data: { funding_request, assessment, disbursements: [] } });
}
} else {
assessment = await repo.loadExisting(assessment, funding_request.application_id);

if (assessment) {
let disbursements = await db("sfa.disbursement")
.where({
funding_request_id: funding_request.id,
assessment_id: assessment.id,
})
.orderBy("issue_date")
.orderBy("id");
return res.json({ data: { funding_request, assessment, disbursements } });
if (assessment) {
assessment = await repo.loadExisting(assessment, application_id);
delete (assessment as any).id;
return res.json({ data: { funding_request, assessment, disbursements: [] } });
}
} else {
assessment = await repo.loadExisting(assessment, funding_request.application_id);

if (assessment) {
let disbursements = await db("sfa.disbursement")
.where({
funding_request_id: funding_request.id,
assessment_id: assessment.id,
})
.orderBy("issue_date")
.orderBy("id");
return res.json({ data: { funding_request, assessment, disbursements } });
}
}
} catch (error) {
console.log("catcing error", error);
return res.status(400).send("Error loading assessment - " + (error as any).message);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,29 +77,38 @@ const actions = {
let url = `${CSG_THRESHOLD_URL}/cslft/${applicationId}`;
if (assessmentId) url += `/${assessmentId}`;

axios.get(url).then(async (resp) => {
let application = store.getters.selectedApplication;
commit("SET_APPLICATION", application);
commit("SET_FUNDINGREQUEST", resp.data.data.funding_request);
commit("SET_DISBURSEMENTS", resp.data.data.disbursements);
commit("SET_MSFAA", resp.data.data.msfaa);

let assessment = resp.data.data.assessment;

if (assessment) {
assessment.assessed_date = moment.utc(assessment.assessed_date).format("YYYY-MM-DD");
assessment.classes_start_date = moment.utc(assessment.classes_start_date).format("YYYY-MM-DD");
assessment.classes_end_date = moment.utc(assessment.classes_end_date).format("YYYY-MM-DD");
}

commit("SET_ASSESSMENT", assessment);
commit("SET_LOADING", false);

// child store initializers
//await store.dispatch("csgPartTimeStore/initialize", { app: application, assessment: state.assessment });
//await store.dispatch("csgPartTimeDependentStore/initialize", { app: application, assessment: state.assessment });
//await store.dispatch("csgPartTimeDisabilityStore/initialize", { app: application, assessment: state.assessment });
});
axios
.get(url)
.then(async (resp) => {
let application = store.getters.selectedApplication;
commit("SET_APPLICATION", application);
commit("SET_FUNDINGREQUEST", resp.data.data.funding_request);
commit("SET_DISBURSEMENTS", resp.data.data.disbursements);
commit("SET_MSFAA", resp.data.data.msfaa);

let assessment = resp.data.data.assessment;

if (assessment) {
assessment.assessed_date = moment.utc(assessment.assessed_date).format("YYYY-MM-DD");
assessment.classes_start_date = moment.utc(assessment.classes_start_date).format("YYYY-MM-DD");
assessment.classes_end_date = moment.utc(assessment.classes_end_date).format("YYYY-MM-DD");
}

commit("SET_ASSESSMENT", assessment);
commit("SET_LOADING", false);

// child store initializers
//await store.dispatch("csgPartTimeStore/initialize", { app: application, assessment: state.assessment });
//await store.dispatch("csgPartTimeDependentStore/initialize", { app: application, assessment: state.assessment });
//await store.dispatch("csgPartTimeDisabilityStore/initialize", { app: application, assessment: state.assessment });
})
.catch((err) => {
console.log("FOUND ERROR");
alert(err.response.data);
})
.finally(() => {
commit("SET_LOADING", false);
});
},

async recalculate({ state, dispatch, commit }) {
Expand All @@ -113,6 +122,10 @@ const actions = {
applicationId: state.fundingRequest.application_id,
assessmentId: state.assessment.id,
});
})
.catch((err) => {
console.log("FOUND ERROR");
alert(err.response.data);
});
},

Expand Down

0 comments on commit 46c71f6

Please sign in to comment.