Skip to content

Commit

Permalink
Fixes a bug introduced in nightscout#7273 (nightscout#7825)
Browse files Browse the repository at this point in the history
* Fixes a bug introduced in nightscout#7273

* Oops somehow managed to commit a wrong file version

* Cleaner code based on feedback from Ben
  • Loading branch information
sulkaharo authored Jan 12, 2023
1 parent c1de8a5 commit fd701e9
Showing 1 changed file with 29 additions and 20 deletions.
49 changes: 29 additions & 20 deletions lib/data/ddata.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);

};
Expand Down

0 comments on commit fd701e9

Please sign in to comment.