Skip to content

Commit

Permalink
Refactor to encapsulate duplicated settings logic (nightscout#5426)
Browse files Browse the repository at this point in the history
* Encapsulate duplicate settings checks inside functions

* Simplify settings::isAlarmEventEnabled()
  • Loading branch information
jakobsandberg authored and sulkaharo committed Jan 12, 2020
1 parent 81d926a commit 937aa47
Showing 1 changed file with 24 additions and 18 deletions.
42 changes: 24 additions & 18 deletions lib/settings.js
Original file line number Diff line number Diff line change
Expand Up @@ -290,34 +290,40 @@ function init () {
return enabled;
}

function isAlarmEventEnabled (notify) {
var enabled = false;
function isUrgentHighAlarmEnabled(notify) {
return notify.eventName === 'high' && notify.level === levels.URGENT && settings.alarmUrgentHigh;
}

if ('high' !== notify.eventName && 'low' !== notify.eventName) {
enabled = true;
} else if (notify.eventName === 'high' && notify.level === levels.URGENT && settings.alarmUrgentHigh) {
enabled = true;
} else if (notify.eventName === 'high' && settings.alarmHigh) {
enabled = true;
} else if (notify.eventName === 'low' && notify.level === levels.URGENT && settings.alarmUrgentLow) {
enabled = true;
} else if (notify.eventName === 'low' && settings.alarmLow) {
enabled = true;
}
function isHighAlarmEnabled(notify) {
return notify.eventName === 'high' && settings.alarmHigh;
}

return enabled;
function isUrgentLowAlarmEnabled(notify) {
return notify.eventName === 'low' && notify.level === levels.URGENT && settings.alarmUrgentLow;
}

function isLowAlarmEnabled(notify) {
return notify.eventName === 'low' && settings.alarmLow;
}

function isAlarmEventEnabled (notify) {
return ('high' !== notify.eventName && 'low' !== notify.eventName)
|| isUrgentHighAlarmEnabled(notify)
|| isHighAlarmEnabled(notify)
|| isUrgentLowAlarmEnabled(notify)
|| isLowAlarmEnabled(notify);
}

function snoozeMinsForAlarmEvent (notify) {
var snoozeTime;

if (notify.eventName === 'high' && notify.level === levels.URGENT && settings.alarmUrgentHigh) {
if (isUrgentHighAlarmEnabled(notify)) {
snoozeTime = settings.alarmUrgentHighMins;
} else if (notify.eventName === 'high' && settings.alarmHigh) {
} else if (isHighAlarmEnabled(notify)) {
snoozeTime = settings.alarmHighMins;
} else if (notify.eventName === 'low' && notify.level === levels.URGENT && settings.alarmUrgentLow) {
} else if (isUrgentLowAlarmEnabled(notify)) {
snoozeTime = settings.alarmUrgentLowMins;
} else if (notify.eventName === 'low' && settings.alarmLow) {
} else if (isLowAlarmEnabled(notify)) {
snoozeTime = settings.alarmLowMins;
} else if (notify.level === levels.URGENT) {
snoozeTime = settings.alarmUrgentMins;
Expand Down

0 comments on commit 937aa47

Please sign in to comment.