-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathNotificationManager.js
77 lines (71 loc) · 2.29 KB
/
NotificationManager.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
const SheetManager = require("./SheetManager");
const { getCurrentLoadShedding } = require("./load-shedding-functions");
const { getLoadSheddingStatus } = require("./web-scraper");
/**
* This is for maping time
*/
const timeMapValues = [
['00:00:00', []],
['01:00:00', []],
['02:00:00', []],
['03:00:00', []],
['04:00:00', []],
['05:00:00', []],
['06:00:00', []],
['07:00:00', []],
['08:00:00', []],
['09:00:00', []],
['10:00:00', []],
['11:00:00', []],
['12:00:00', []],
['13:00:00', []],
['14:00:00', []],
['15:00:00', []],
['16:00:00', []],
['17:00:00', []],
['18:00:00', []],
['19:00:00', []],
['20:00:00', []],
['21:00:00', []],
['22:00:00', []],
['23:00:00', []],
];
class NotificationManager {
subscriptions = [];
schedules = new Map(timeMapValues);
constructor(subscriptions) {
this.subscriptions = subscriptions;
}
/**
*
*/
upcomingNotifications = (currentLoadSheddingStatus) => new Promise(resolve => {
const lastSubscription = this.subscriptions.length - 1;
const subscription = this.subscriptions[lastSubscription];
new SheetManager()
.extractLoadsheddingScheduleFromSheet(subscription['areaId'])
.then(async data => {
const { schedule, area } = data;
// await getLoadSheddingStatus().then(stage => {
getCurrentLoadShedding(schedule, area['block'], currentLoadSheddingStatus)
.then(loadsheddingSchedule => {
loadsheddingSchedule['schedule'].forEach(timeStamp => {
let mapKey = timeStamp['start'];
const addNotification = [...this.schedules.get(mapKey), { timeStamp, subscription }]
this.schedules.set(mapKey, addNotification);
});
resolve(this.schedules);
});
// });
});
});
syncNotifications = () => new Promise(resolve => {
this.upcomingNotifications().then((notifications) => {
resolve(notifications);
});
});
getNotifications = () => {
return this.subscriptions;
}
}
module.exports = NotificationManager;