Skip to content

Commit

Permalink
Merge pull request #347 from micronutrientsupport/andan_intervention-…
Browse files Browse the repository at this point in the history
…recalculate

feat: reflect standardised format for baseline assumptions endpoint, …
  • Loading branch information
bgsandan authored Jan 17, 2024
2 parents 28c7b5b + 440f98c commit af5451f
Show file tree
Hide file tree
Showing 3 changed files with 48 additions and 23 deletions.
63 changes: 42 additions & 21 deletions src/controllers/intervention.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import {
import {parse} from 'excel-formula-parser';
import transformJS from 'js-to-json-logic';
import * as jsonLogicParser from 'json-logic-js';
import {inspect} from 'util';
import {
AuthenticatedRequest,
AuthenticationCheckInterceptorInterceptor,
Expand Down Expand Up @@ -507,7 +506,7 @@ export class InterventionController {

const updated = await this.interventionListRepository.findById(id);

console.log(intervention[0]);
//console.log(intervention[0]);
return new StandardJsonResponse<Array<InterventionList>>(
`Intervention ownership updated.`,
[updated],
Expand Down Expand Up @@ -639,6 +638,26 @@ export class InterventionController {
};
const baselineAssumptions =
await this.interventionBaselineAssumptionsRepository.find(filter);

// Replace Excel Formulae with JsonLogic for interpretation on the frontend
if (baselineAssumptions[0].baselineAssumptions) {
baselineAssumptions[0].baselineAssumptions.actuallyFortified =
replaceExcelFormulaeWothJsonLogic(
baselineAssumptions[0].baselineAssumptions
?.actuallyFortified as InterventionDataFields,
);
baselineAssumptions[0].baselineAssumptions.potentiallyFortified =
replaceExcelFormulaeWothJsonLogic(
baselineAssumptions[0].baselineAssumptions
?.potentiallyFortified as InterventionDataFields,
);
baselineAssumptions[0].baselineAssumptions.averageFortificationLevel =
replaceExcelFormulaeWothJsonLogic(
baselineAssumptions[0].baselineAssumptions
?.averageFortificationLevel as InterventionDataFields,
);
}

return new StandardJsonResponse<Array<InterventionBaselineAssumptions>>(
`Intervention data returned.`,
baselineAssumptions,
Expand Down Expand Up @@ -776,7 +795,7 @@ export class InterventionController {
costs.costBreakdown.map(value => {
return replaceExcelFormulaeWothJsonLogic(value);
});
console.log('Replaing Year0Total Formula', costs.year0TotalFormula);
//console.log('Replaing Year0Total Formula', costs.year0TotalFormula);
costs.year0TotalFormula = formulaToJsonLogic(
costs.year0TotalFormula as string,
{},
Expand Down Expand Up @@ -892,6 +911,9 @@ export class InterventionController {
rowIndex: {
type: 'number',
},
targetVal: {
type: 'number',
},
year0: {
type: 'number',
},
Expand Down Expand Up @@ -981,14 +1003,16 @@ export class InterventionController {
interventionUpdateDeltaList: InterventionUpdateDelta[],
): Promise<StandardJsonResponse<Array<InterventionList>>> {
console.log('Patch');
console.log(interventionUpdateDeltaList);
// console.log(interventionUpdateDeltaList);

const tx =
await this.interventionDataRepository.dataSource.beginTransaction(
IsolationLevel.READ_COMMITTED,
);
interventionUpdateDeltaList.map(async delta => {
console.log({delta});
const interventionUpdateDelta = new InterventionData(delta);
console.log({interventionUpdateDelta});
await this.interventionDataRepository.updateAll(
interventionUpdateDelta,
{
Expand Down Expand Up @@ -1061,7 +1085,6 @@ export class InterventionController {
});

jsonLogicParser.add_operation('average', (...values) => {
console.error('average', values);
if (values.length === 0) return 0;

const sum = values.reduce((acc, curr) => acc + Number(curr), 0);
Expand Down Expand Up @@ -1241,20 +1264,20 @@ export class InterventionController {
dataVals: any,
intervention: InterventionList,
) {
console.log(`${row.rowIndex}, Year${year}`);
// console.log(`${row.rowIndex}, Year${year}`);

const formula = formulaToJsonLogic(row[`year${year}Formula`], fullData);
// console.log(inspect(year0Formula, false, null, false));
const newValue = jsonLogicParser.apply(formula);
const dataValsIndex = fullData[row.rowIndex].index;

if (dataVals[dataValsIndex][`year${year}`] != newValue) {
console.log(`Row #${row.rowIndex}`);
console.log(row[`year${year}Formula`]);
console.log(inspect(formula, false, null, false));
console.log(
`Year${year}: ${dataVals[dataValsIndex][`year${year}`]} => ${newValue}`,
);
// console.log(`Row #${row.rowIndex}`);
// console.log(row[`year${year}Formula`]);
// console.log(inspect(formula, false, null, false));
// console.log(
// `Year${year}: ${dataVals[dataValsIndex][`year${year}`]} => ${newValue}`,
// );

// Update the data for jsonLogic
fullData[row.rowIndex][`year${year}`] = newValue;
Expand All @@ -1263,13 +1286,13 @@ export class InterventionController {
dataVals[dataValsIndex][`year${year}`] = parseFloat(newValue);
await this.interventionDataRepository.update(dataVals[dataValsIndex]);

console.log('---------------');
// console.log('---------------');
} else {
console.log(`Row #${row.rowIndex}`);
console.log(row[`year${year}Formula`]);
console.log(inspect(formula, false, null, false));
console.log(`${dataVals[dataValsIndex][`year${year}`]} => ${newValue}`);
console.log('---------------');
// console.log(`Row #${row.rowIndex}`);
// console.log(row[`year${year}Formula`]);
// console.log(inspect(formula, false, null, false));
// console.log(`${dataVals[dataValsIndex][`year${year}`]} => ${newValue}`);
// console.log('---------------');
}
}

Expand Down Expand Up @@ -1316,8 +1339,6 @@ export class InterventionController {
},
);

console.error(fullData[275]);

(fullData as any)['regexes'] = {
premix: 'Premix - .*',
demographics: 'Demographics',
Expand All @@ -1332,7 +1353,7 @@ export class InterventionController {
await this.interventionPremixCostRepository.find(premixFilter)
)[0];

console.log(premixCost);
// console.log(premixCost);

(fullData as any)['premix'] = {
year0: premixCost.premixCostPerMt,
Expand Down
5 changes: 3 additions & 2 deletions src/models/intervention-baseline-assumptions.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export class InterventionBaselineAssumptions extends Entity {
baselineAssumptions?: {
potentiallyFortified: {};
actuallyFortified: {};
averageFortificationLevel: {};
};

// Define well-known properties here
Expand All @@ -56,5 +57,5 @@ export interface InterventionBaselineAssumptionsRelations {
// describe navigational properties here
}

export type InterventionBaselineAssumptionsWithRelations = InterventionBaselineAssumptions &
InterventionBaselineAssumptionsRelations;
export type InterventionBaselineAssumptionsWithRelations =
InterventionBaselineAssumptions & InterventionBaselineAssumptionsRelations;
3 changes: 3 additions & 0 deletions src/models/intervention-data.model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -390,6 +390,9 @@ export class InterventionData extends Entity {
[prop: string]: any;

constructor(data?: Partial<InterventionData>) {
if (data && Object.prototype.hasOwnProperty.call(data, 'targetVal')) {
data.year0 = data['targetVal'];
}
super(data);
}
}
Expand Down

0 comments on commit af5451f

Please sign in to comment.