This repository has been archived by the owner on Apr 6, 2024. It is now read-only.
/
App.js
77 lines (71 loc) · 3.06 KB
/
App.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
/* eslint-disable prefer-const */
const axios = require('axios');
const express = require('express');
const clone = require('just-clone');
const ical = require('ical-generator');
const { convert } = require('./utils/icalToFromJson');
const parseUserConfig = require('./utils/parseUserConfig');
const createEventConfig = require('./utils/createEventConfig');
const app = express();
const PORT = process.env.PORT || 3000;
app.listen(PORT, () => {
console.log(`Server is running on ::${PORT}`);
});
const throwErr = (err, req, res) => {
res.setHeader('Content-type', 'application/json');
req.connection.destroy();
res.end(JSON.stringify(err));
};
app.get('/', async (req, res) => {
userConfigData = await req.query?.userConfig ? await axios.get(req.query?.userConfig) : void 0;
if (!userConfigData) throwErr('userConfig unvalid or not found', req, res);
let userConfig = parseUserConfig(userConfigData.data);
// if (!userConfigData) throwErr('userConfig unvalid or not found');
let rawCal = await axios.get(userConfig.url);
let jsonCal = convert(rawCal.data);
let events = clone(jsonCal.VCALENDAR[0].VEVENT);
if (userConfig.type == 'blist') {
if (userConfig.matchPart) {
for (const item of userConfig.matchPart) events = events.filter((event) => !event.SUMMARY.includes(item));
}
if (userConfig.matchString) {
for (const item of userConfig.matchString) events = events.filter((event) => !(event.SUMMARY == item));
}
}
const newCalendar = ical({
'prodId': '//Google Inc//Google Calendar//EN',
'name': userConfig.name,
'description': jsonCal.VCALENDAR[0]['X-WR-CALDESC'],
'scale': jsonCal.VCALENDAR[0]?.CALSCALE,
'method': jsonCal.VCALENDAR[0]?.METHOD,
});
for (const event of events) {
const eventConfig = createEventConfig(event);
if (userConfig.limit?.start) {
const condition = [
parseInt(eventConfig.date.start.year) >= userConfig.limit.start.year,
parseInt(eventConfig.date.start.month) >= userConfig.limit.start.month,
parseInt(eventConfig.date.start.day) >= userConfig.limit.start.day,
];
if (!condition.includes(false)) newCalendar.createEvent(eventConfig.data);
} else if (userConfig.limit?.start && userConfig.limit?.end) {
const condition = [
parseInt(eventConfig.date.start.year) >= userConfig.limit.start.year,
parseInt(eventConfig.date.start.month) >= userConfig.limit.start.month,
parseInt(eventConfig.date.start.day) >= userConfig.limit.start.day,
parseInt(eventConfig.date.end.year) <= userConfig.limit.end.year,
parseInt(eventConfig.date.end.month) <= userConfig.limit.end.month,
parseInt(eventConfig.date.end.day) <= userConfig.limit.end.day,
];
if (!condition.includes(false)) newCalendar.createEvent(eventConfig.data);
} else {
newCalendar.createEvent(eventConfig.data);
}
}
// FOR DEVELOPMENT PURPOSES ONLY
// res.setHeader('Content-type', 'application/json');
// res.end(JSON.stringify(newCalendar));
// ENABLE FOR PRODUCTION
newCalendar.serve(res);
// console.log('requested');
});