-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcreate-manual-calendar.mjs
66 lines (57 loc) · 1.7 KB
/
create-manual-calendar.mjs
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
import ICalCalendar from "ical-generator";
import {getVtimezoneComponent} from "@touch4it/ical-timezones";
import { DateTime } from "luxon";
const TIMEZONE = "Europe/Kiev"
export default function createCalendar(raw, name)
{
const now = DateTime.local({zone: TIMEZONE })
const startOfWeek = now.startOf("week")
const groups = {};
const states = {
"maybe": "Можливо відключення",
"no": "Планове відключення",
"yes": "Світло є",
"first": "Можливо відключення в перші 30 хв",
"second": "Можливо відключення в другі 30 хв"
};
for (const [group, groupSchedule] of Object.entries(raw.data))
{
const cal = new ICalCalendar();
cal.timezone({
name: `blackouts.${name}.${group}`,
generator: getVtimezoneComponent
});
for (const [dayOfWeek, dayScedule] of Object.entries(groupSchedule))
{
const day = startOfWeek.plus({day: Number(dayOfWeek) - 1})
const hours = Object.keys(dayScedule)
for (let i = 0; i < hours.length;)
{
const hour = hours[i]
const status = dayScedule[hour]
if (status == "yes")
{
i++;
continue;
}
const start = day.plus({hours: Number(hour) - 1})
while (dayScedule[hours[i]] === status)
{
i++
}
const end = day.plus({hours: Number(hours[i] ? hours[i] : 25) - 1})
cal.createEvent({
start,
end,
timezone: TIMEZONE,
summary: states[status],
repeating: {
freq: "WEEKLY"
}
});
}
}
groups[group] = cal
}
return groups
}