-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcreateEvent.js
More file actions
103 lines (91 loc) · 3.08 KB
/
createEvent.js
File metadata and controls
103 lines (91 loc) · 3.08 KB
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
const ics = require('ics');
const { writeFileSync } = require('fs');
const createEvent = resolved => {
try {
console.log('>>>> NOW TRYING <<<<<<<');
//do the work to get all the dates, times, location, description information
const allDates = resolved.text.match(/\d{2}([\/.-])\d{2}\1\d{4}/g);
const dateRange = resolved.text.match(
/\d{2}([\/.-])\d{2}\1\d{4}\s\w[to]\s\d{2}([\/.-])\d{2}\1\d{4}/g,
);
const dateRangeArray = dateRange[0].split(' to ');
const allTimes = resolved.text.match(/\d{2}([\:])\d{2}/g);
//hard coded based on knowing set description
const allDescriptions = resolved.text.match(/Quinn IMRT/g);
const eachLineInArray = resolved.text.split('\n');
const replaceNewline = resolved.text.replace(/\n/g, ' ');
const totalNumberOfAppointments = replaceNewline.match(
/Scheduled:* ([\d.]+)/,
)[1];
const relevantDates = allDates.filter(
date => !dateRangeArray.includes(date),
);
//i am working off the unconfirmed assumption that the date range will
//always be the first two elements of the allDates array
const appointmentDates = allDates.splice(2);
const appointmentTimes = allTimes.splice(1);
const formatTime = time => {
const oneTime = time.split(':');
formattedHour = oneTime[0];
formattedMinute = oneTime[1];
let result;
if (formattedHour < 18) {
let formattedArray = [formattedHour, formattedMinute];
result = formattedArray.map(function(x) {
return parseInt(x, 10);
});
}
return result;
};
let formattedTimes = [];
for (let index = 0; index < appointmentTimes.length; index++) {
const element = appointmentTimes[index];
let result = formatTime(element);
formattedTimes.push(result);
}
//format the date to ics format
// { weeks, days, hours, minutes, seconds }
// [2000, 1, 5, 10, 0] (January 5, 2000)
const formatDate = (date, time) => {
const oneDate = date.split('/');
// being explicit here to make this more readable
formattedDay = oneDate[0];
formattedMonth = oneDate[1];
formattedYear = oneDate[2];
const formattedDateArray = [formattedYear, formattedMonth, formattedDay];
const result = formattedDateArray.map(function(x) {
return parseInt(x, 10);
});
return result;
};
let formattedDates = [];
for (let index = 0; index < appointmentDates.length; index++) {
const element = appointmentDates[index];
let result = formatDate(element);
formattedDates.push(result.concat(formattedTimes[index]));
}
let diaryEvents = [];
for (let i = 0; i < appointmentDates.length; i++) {
diaryEvents[i] = {
title: 'Hospital Appointment',
description: allDescriptions[i],
start: formattedDates[i],
//ideally below will not be hardcoded
duration: { minutes: 15 },
productId: 'mork',
};
}
const { error, value } = ics.createEvents(diaryEvents);
console.log(value, 'i dunno');
if (error) {
console.log(error);
return;
}
//what do we get when we run
console.log(value);
writeFileSync(`${__dirname}/public/events/event.ics`, value);
} catch (error) {
console.log(error);
}
};
module.exports = { createEvent };