-
Notifications
You must be signed in to change notification settings - Fork 0
/
gcal.js
126 lines (103 loc) · 2.94 KB
/
gcal.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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
const gcalendar = require('@googleapis/calendar')
const config = require('./config/default.js')
const {
getStartOfToday,
getStartOfTomorrow,
getTodaysDate,
getTomorrowsDate,
} = require('./date-fns.js');
const auth = new gcalendar.auth.GoogleAuth({
keyFile: './gauth.json',
scopes: ['https://www.googleapis.com/auth/calendar'],
})
const gcal = gcalendar.calendar({ version: 'v3', auth })
const getCurrentEventFromGCalEventsList = (eventsList, thingName) => {
if (!eventsList || !eventsList?.length) return
/*
const events = eventsList.map((ev) => ({
id: ev.id,
summary: ev.summary,
start: ev.start.dateTime || ev.start.date,
description: ev.description,
}));
console.log(events)
*/
let currEv = null;
const thingNameNoSpaces = thingName.replace(/\s/g, '');
eventsList.forEach(ev => {
const indexOfFirstSpace = ev.summary.indexOf(' ');
if (indexOfFirstSpace === -1) return;
const evThingName = ev.summary.substr(1 + indexOfFirstSpace);
if (evThingName.replace(/\s/g, '') === thingNameNoSpaces) {
currEv = {
...ev,
count: Number(ev.summary.substr(0, indexOfFirstSpace)) || 0,
};
}
});
return currEv;
};
const getTodaysEvents = async () => {
const res = await gcal.events.list(
{
calendarId: config.calendarId,
timeMin: getStartOfToday(config.timezoneOffsetInMinutes).toISOString(),
timeMax: getStartOfTomorrow(config.timezoneOffsetInMinutes).toISOString(),
singleEvents: true,
orderBy: 'startTime',
}
);
return res?.data?.items;
};
const getCurrEvent = async (thingName) => {
const todaysEvents = await getTodaysEvents();
return getCurrentEventFromGCalEventsList(todaysEvents, thingName)
};
const createGCalEvent = async (num, thingName) => {
return await gcal.events.insert(
{
calendarId: config.calendarId,
resource: {
start: {
date: getTodaysDate(config.timezoneOffsetInMinutes),
},
end: {
date: getTomorrowsDate(config.timezoneOffsetInMinutes),
},
colorId: config.defaultEventColorId,
summary: `${num} ${thingName}`,
description: num
}
}
);
};
const updateGCalEvent = async (eventToUpdate, num, thingName) => {
return gcal.events.update(
{
calendarId: config.calendarId,
eventId: eventToUpdate.id,
resource: {
...eventToUpdate,
summary: `${num + eventToUpdate.count} ${thingName}`,
description: !eventToUpdate.description ? num : `${eventToUpdate.description}+${num}`,
},
}
);
};
const addToCurrTotal = async (currEvent, num, thingName) => {
let resp;
if (!currEvent) {
resp = await createGCalEvent(num, thingName);
}
else {
resp = await updateGCalEvent(currEvent, num, thingName);
}
return {
...resp.data,
count: Number(resp.data.summary.substr(0, resp.data.summary.indexOf(' '))),
}
};
module.exports = {
getCurrEvent,
addToCurrTotal,
};