From fd701e9ce5151508a664ddcae80571c30ccbb715 Mon Sep 17 00:00:00 2001 From: Sulka Haro Date: Thu, 12 Jan 2023 07:59:41 +0200 Subject: [PATCH] Fixes a bug introduced in #7273 (#7825) * Fixes a bug introduced in #7273 * Oops somehow managed to commit a wrong file version * Cleaner code based on feedback from Ben --- lib/data/ddata.js | 49 ++++++++++++++++++++++++++++------------------- 1 file changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/data/ddata.js b/lib/data/ddata.js index caab8e04139..0ee1d9ebc7f 100644 --- a/lib/data/ddata.js +++ b/lib/data/ddata.js @@ -265,33 +265,42 @@ function init () { ddata.tempbasalTreatments = ddata.processDurations(tempbasalTreatments, false); // filter temp target - var tempTargetTreatments = ddata.treatments.filter(function filterTargets (tt) { + var tempTargetTreatments = ddata.treatments.filter(function filterTargets (t) { + return t.eventType && t.eventType.indexOf('Temporary Target') > -1; + }); + + function convertTempTargetTreatmentUnites (_treatments) { + + let treatments = _.cloneDeep(_treatments); - // Clone the treatment before modifying it - let t = _.cloneDeep(tt); + for (let i = 0; i < treatments.length; i++) { - //check for a units being sent - if (t.units) { - if (t.units == 'mmol') { - //convert to mgdl + let t = treatments[i]; + let converted = false; + + // if treatment is in mmol, convert to mg/dl + if (Object.prototype.hasOwnProperty.call(t,'units')) { + if (t.units == 'mmol') { + //convert to mgdl + t.targetTop = t.targetTop * consts.MMOL_TO_MGDL; + t.targetBottom = t.targetBottom * consts.MMOL_TO_MGDL; + t.units = 'mg/dl'; + converted = true; + } + } + + //if we have a temp target thats below 20, assume its mmol and convert to mgdl for safety. + if (!converted && (t.targetTop < 20 || t.targetBottom < 20)) { t.targetTop = t.targetTop * consts.MMOL_TO_MGDL; t.targetBottom = t.targetBottom * consts.MMOL_TO_MGDL; t.units = 'mg/dl'; } } - //if we have a temp target thats below 20, assume its mmol and convert to mgdl for safety. - if (t.targetTop < 20) { - t.targetTop = t.targetTop * consts.MMOL_TO_MGDL; - t.units = 'mg/dl'; - } - if (t.targetBottom < 20) { - t.targetBottom = t.targetBottom * consts.MMOL_TO_MGDL; - t.units = 'mg/dl'; - } - return t.eventType && t.eventType.indexOf('Temporary Target') > -1; - }); - if (preserveOrignalTreatments) - tempTargetTreatments = _.cloneDeep(tempTargetTreatments); + return treatments; + } + + if (preserveOrignalTreatments) tempTargetTreatments = _.cloneDeep(tempTargetTreatments); + tempTargetTreatments = convertTempTargetTreatmentUnites(tempTargetTreatments); ddata.tempTargetTreatments = ddata.processDurations(tempTargetTreatments, false); };