forked from canada-ca/tracker
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathupdate-month-summary.js
More file actions
45 lines (37 loc) · 1002 Bytes
/
update-month-summary.js
File metadata and controls
45 lines (37 loc) · 1002 Bytes
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
35
36
37
38
39
40
41
42
43
44
45
const updateMonthSummary = (
calculatePercentages,
createSummary,
createSummaryEdge,
loadSummaryByDate,
removeSummaryEdge,
removeSummary,
) => async ({ dateToRemove, dateToAdd, domain, domainId }) => {
// Remove summary edge
const currentEdge = await removeSummaryEdge({
domainId,
monthToRemove: dateToRemove,
})
if (typeof currentEdge === 'undefined') {
return undefined
}
// Remove summary
await removeSummary({ summaryId: currentEdge._to })
const currentSummary = await loadSummaryByDate({
domain,
startDate: dateToAdd,
})
const { totalMessages, percentages } = calculatePercentages(
currentSummary.categoryTotals,
)
currentSummary.totalMessages = totalMessages
currentSummary.categoryPercentages = percentages
const summaryDBInfo = await createSummary({ currentSummary })
await createSummaryEdge({
domainId,
summaryId: summaryDBInfo._id,
startDate: dateToAdd,
})
}
module.exports = {
updateMonthSummary,
}