-
-
Notifications
You must be signed in to change notification settings - Fork 219
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Expire all tasks and targets calculations at the end of reporting per…
…iod (#6320) All tasks and targets are recalculated when reporting interval turns over. Moves CalendarInterval into a shared lib. #6267 (cherry picked from commit 2337464)
- Loading branch information
1 parent
1609ee1
commit c4f4807
Showing
13 changed files
with
1,186 additions
and
102 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
{ | ||
"name": "@medic/calendar-interval", | ||
"version": "1.0.0", | ||
"description": "Provides helper function when dealing with monthly calendar intervals", | ||
"main": "src/index.js", | ||
"scripts": { | ||
"test": "mocha ./test" | ||
}, | ||
"author": "", | ||
"license": "Apache-2.0", | ||
"devDependencies": { | ||
"chai": "^4.2.0", | ||
"mocha": "^6.1.4", | ||
"sinon": "^7.3.2" | ||
}, | ||
"dependencies": { | ||
"moment": "^2.24.0" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
const moment = require('moment'); | ||
|
||
const normalizeStartDate = function(intervalStartDate) { | ||
intervalStartDate = parseInt(intervalStartDate); | ||
|
||
if (isNaN(intervalStartDate) || intervalStartDate <= 0 || intervalStartDate > 31) { | ||
intervalStartDate = 1; | ||
} | ||
|
||
return intervalStartDate; | ||
}; | ||
|
||
const getMinimumStartDate = function(intervalStartDate, relativeDate) { | ||
return moment | ||
.min( | ||
moment(relativeDate).subtract(1, 'month').date(intervalStartDate).startOf('day'), | ||
moment(relativeDate).startOf('month') | ||
) | ||
.valueOf(); | ||
}; | ||
|
||
const getMinimumEndDate = function(intervalStartDate, nextMonth, relativeDate) { | ||
return moment | ||
.min( | ||
moment(relativeDate).add(nextMonth ? 1 : 0, 'month').date(intervalStartDate - 1).endOf('day'), | ||
moment(relativeDate).add(nextMonth ? 1 : 0, 'month').endOf('month') | ||
) | ||
.valueOf(); | ||
}; | ||
|
||
module.exports = { | ||
// Returns the timestamps of the start and end of the current calendar interval | ||
// @param {Number} [intervalStartDate=1] - day of month when interval starts (1 - 31) | ||
// | ||
// if `intervalStartDate` exceeds month's day count, the start/end of following/current month is returned | ||
// f.e. `intervalStartDate` === 31 would generate next intervals : | ||
// [12-31 -> 01-30], [01-31 -> 02-[28|29]], [03-01 -> 03-30], [03-31 -> 04-30], [05-01 -> 05-30], [05-31 - 06-30] | ||
getCurrent: function(intervalStartDate) { | ||
intervalStartDate = normalizeStartDate(intervalStartDate); | ||
|
||
if (intervalStartDate === 1) { | ||
return { | ||
start: moment().startOf('month').valueOf(), | ||
end: moment().endOf('month').valueOf() | ||
}; | ||
} | ||
|
||
if (intervalStartDate <= moment().date()) { | ||
return { | ||
start: moment().date(intervalStartDate).startOf('day').valueOf(), | ||
end: getMinimumEndDate(intervalStartDate, true) | ||
}; | ||
} | ||
|
||
return { | ||
start: getMinimumStartDate(intervalStartDate), | ||
end: getMinimumEndDate(intervalStartDate) | ||
}; | ||
} | ||
}; |
22 changes: 9 additions & 13 deletions
22
.../karma/unit/services/calendar-interval.js → shared-libs/calendar-interval/test/index.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.