Skip to content

Commit

Permalink
I think I may have unbelievably solved the ridiculous hybrid user ove…
Browse files Browse the repository at this point in the history
…rride/median cost calculation for the cost by cancer view. Man, what a pain in the ass that was.
  • Loading branch information
davemanroth committed May 12, 2021
1 parent 461540d commit aa2924f
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 29 deletions.
70 changes: 42 additions & 28 deletions src/models/CostModel.js
Original file line number Diff line number Diff line change
Expand Up @@ -225,13 +225,28 @@ class CostModel {
};
const tcpc = totalCostPerCancer[cancer];

/**
* Initialize a special medAndUser hybrid total to keep track
* of costs that include drugs with user overridden prices. This
* total will be used specifically for viewing costs by cancer.
*/
tcpc.totals.medAndUser = 0;

// Risk strats loop
Object.keys(totalDosageByType[cancer].risk_strats).forEach( (rs) => {
const sourceDrugDosage = totalDosageByType[cancer].risk_strats[rs].drugs;
let currMedTotal = 0;


// Drugs loop
Object.keys(sourceDrugDosage).forEach( (drug) => {

/**
* Initialize a currHybridCost variable that will store the current
* drug's cost total using the median and overriden drug prices
* (if there is a user overridden drug price). Eventually this variable's
* total will be added to the medAndUser total once the price tier loop ends
*/
let currHybridCost = 0;
if (!tcpc.drugs.hasOwnProperty(drug)) {
tcpc.drugs[drug] = {
name: sourceDrugDosage[drug].name,
Expand All @@ -243,10 +258,6 @@ class CostModel {
const currCancerDrugPrices = this.#prices[drug].prices;
const hasOverride = Object.keys(currCancerDrugPrices).includes("override");
tcpc.drugs[drug].total_dosage += dosageTotal;
if (hasOverride) {
tcpc.totals["medAndUser"] = 0;
//console.log("override identified", tcpc.totals["medAndUser"]);
}

// Price tiers loop where costs are computed
Object.keys(currCancerDrugPrices).forEach( (tier, num) => {
Expand All @@ -260,44 +271,47 @@ class CostModel {
if (!isNaN(price)) {
const dosagePrice = dosageTotal * price;
tcpc.drugs[drug].costs[tier] += dosagePrice;
if (hasOverride) {
if (tier === "override") {
currMedTotal += dosagePrice;

/**
* To get the right hybrid median/user override cost, the default
* will be to add the median tier cost to the currHybridCost var.
* However, if the current drug /does/ have a user override drug price,
* we want to add the overridden drug price cost to the currHybridCost var
* instead of the median cost.
*
* Thus the hybrid total cost will be the sum of
* any overriden drug price cost plus the median of all other drug price costs,
* minus the median price cost of the drug price that is overriden
*/
if (tier === "med") {
if (hasOverride) {
currHybridCost += 0;
}
if (tier === "med") {
currMedTotal += 0;
else {
currHybridCost += dosagePrice;
}
}
if (tier === "override") {
currHybridCost += dosagePrice;
}
tcpc.totals[tier] += dosagePrice;
} // if price is a number (!isNaN)
});
// End price tiers loop

if (tcpc.drugs[drug].costs.hasOwnProperty("override") ) {
currMedTotal += tcpc.drugs[drug].costs["override"];
currMedTotal -= tcpc.drugs[drug].costs["med"];
tcpc.totals["medAndUser"] += currMedTotal;
//console.log("Curr med total", currMedTotal);
//console.log("Override", tcpc.drugs[drug].costs["override"]);
//console.log("Med", tcpc.drugs[drug].costs["med"]);
//console.log("Med and User", tcpc.totals["medAndUser"]);
}
//console.log("Drug totals", tcpc.totals);

// Update special medAndUser prop with hybrid med/user override cost
// total
//console.log(tcpc.totals["medAndUser"]);

// Reset temp hybrid total variable for next drug loop iteration
currMedTotal = 0;
/**
* Update the special medAndUser total with hybrid user/med current drug total,
* then reset for next drug loop iteration.
*/
tcpc.totals.medAndUser += currHybridCost;
currHybridCost = 0;

// Make sure to update the generic drug dosage totals before leaving loop
tcpc.totals.dosage += dosageTotal;
});
// End drugs loop

}); // Risk strats forEach
//console.log(totalCostPerCancer[cancer].drugs);
const costArr = this.objToArray(totalCostPerCancer[cancer].drugs);
totalCostPerCancer[cancer].drugs = sortObjects(costArr);
}); // Cancers forEach
Expand Down
16 changes: 15 additions & 1 deletion src/models/__tests__/CostModel.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import {
user
} from '../fixtures/cost';

// override: ATRA, 9.3

import CostModel from '../CostModel';

const cm = new CostModel();
Expand Down Expand Up @@ -33,12 +35,24 @@ describe('Total cost per cancer', () => {
});

test('should have apl, totals, drugs props with data in them', () => {
console.log(tcpc);
//console.log(tcpc);
expect(tcpc.hasOwnProperty('apl')).toBeTruthy();
expect(tcpc.apl.drugs.length).toEqual(4);
expect(tcpc.apl.totals.hasOwnProperty('override')).toBeTruthy();
//const keysToCompare = ['name', 'totals', 'drugs'];
//expect(Object.keys(tcpc.apl).every( ()
});

test('should show an override total of 12276', () => {
expect(tcpc.apl.totals.override).toEqual(12276);
});

test('should show a med total of 141754.45', () => {
expect(tcpc.apl.totals.med.toFixed(2)).toEqual("141754.45");
});

test('should show a medAndUser total of 140988.85', () => {
expect(tcpc.apl.totals.medAndUser.toFixed(2)).toEqual("140988.85");
});
});

0 comments on commit aa2924f

Please sign in to comment.