-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathDisallowTooSoonOrFrequentRule.js
34 lines (27 loc) · 1.39 KB
/
DisallowTooSoonOrFrequentRule.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
'use strict';
var _ = require('underscore'),
moment = require('moment'),
BaseRule = require('./BaseRule');
module.exports = BaseRule.extend({
apply: function(state) {
var maxDecreases = this._config.MaximumDecreasesToUsePerDay,
lastIncrease = state.provisioning.LastIncreaseDateTime,
lastDecrease = state.provisioning.LastDecreaseDateTime,
nextAllowedIncrease = moment(lastIncrease).add(this._config.MinimumMinutesBetweenIncreases, 'minutes'),
nextAllowedDecAfterDec = moment(lastDecrease).add(this._config.MinimumMinutesBetweenDecreases, 'minutes'),
nextAllowedDecAfterInc = moment(lastIncrease).add(this._config.MinimumMinutesBeforeDecreaseAfterIncrease, 'minutes'),
nextAllowedDecrease = nextAllowedDecAfterDec;
if (nextAllowedDecAfterInc.isAfter(nextAllowedDecAfterDec)) {
nextAllowedDecrease = nextAllowedDecAfterInc;
}
if (this.isIncreasing(state) && nextAllowedIncrease.isAfter(state.currentTime) && !_.isUndefined(lastIncrease)) {
state.isAllowedToChange = false;
}
if (this.isDecreasing(state) && !_.isUndefined(lastDecrease)) {
// decrease is being planned - should we stop it?
if (nextAllowedDecrease.isAfter(state.currentTime) || state.provisioning.NumberOfDecreasesToday >= maxDecreases) {
state.isAllowedToChange = false;
}
}
},
});