-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathoptimise-services-and-exceptions.js
85 lines (74 loc) · 2.03 KB
/
optimise-services-and-exceptions.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
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
'use strict'
const readServicesAndExceptions = require('./read-services-and-exceptions')
const datesBetween = require('./lib/dates-between')
const WEEKDAYS = [
// JS Date ordering
'sunday',
'monday',
'tuesday',
'wednesday',
'thursday',
'friday',
'saturday',
]
const noWeekday = {
monday: false,
tuesday: false,
wednesday: false,
thursday: false,
friday: false,
saturday: false,
sunday: false,
}
const formatDate = isoDate => isoDate.split('-').join('')
const optimiseServicesAndExceptions = async function* (readFile, timezone, filters = {}, opt = {}) {
const weekdaysMap = new Map()
const svcsAndExceptions = readServicesAndExceptions(readFile, timezone, filters, {
...opt,
exposeStats: true,
weekdaysMap,
})
for await (let [serviceId, dates, svc, nrOfDates, removedDates] of svcsAndExceptions) {
const nrOfDefaultDates = []
for (let wd = 0; wd < WEEKDAYS.length; wd++) {
const defaultDates = datesBetween(
svc.start_date, svc.end_date,
{...noWeekday, [WEEKDAYS[wd]]: true},
timezone,
weekdaysMap,
)
nrOfDefaultDates[wd] = defaultDates.length
}
let changed = false
svc = {...svc}
for (let wd = 0; wd < 7; wd++) {
// todo: make this customisable
const flag = nrOfDates[wd] > nrOfDefaultDates[wd] / 2 | 0 ? '1' : '0'
changed = changed || (flag !== svc[WEEKDAYS[wd]])
svc[WEEKDAYS[wd]] = flag
}
const exceptions = []
for (const date of dates) {
const wd = weekdaysMap.get(date)
if (svc[WEEKDAYS[wd]] === '1') continue
exceptions.push({
service_id: serviceId,
date: formatDate(date),
exception_type: '1', // added
})
}
for (const date of removedDates) {
const wd = weekdaysMap.get(date)
if (svc[WEEKDAYS[wd]] === '0') continue
exceptions.push({
service_id: serviceId,
date: formatDate(date),
exception_type: '2', // removed
})
}
// todo [breaking]: remove serviceId (idx 0), move svc first,
// follow read-services-and-exceptions here
yield [serviceId, changed, svc, exceptions]
}
}
module.exports = optimiseServicesAndExceptions