-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.ts
111 lines (96 loc) · 2.64 KB
/
index.ts
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
import fetch, { RequestInit, HeadersInit } from "node-fetch";
import ObjectsToCsv from "objects-to-csv";
import dayjs from "dayjs";
interface Schedule {
id: string;
month: string;
days: string;
year: string;
scheduleDescriptionId: string;
}
interface ScheduleDescription {
id: string;
month: string;
days: string;
year: string;
scheduleDescriptionId: string;
name: string;
description: string;
typeId: string;
color: string;
order: string;
notificationType: string;
notificationText: string;
notificationDaysBefore: string;
masterId: string;
slaveId: string;
}
interface ResultAPI {
schedules: Schedule[];
scheduleDescription: ScheduleDescription[];
}
interface CalendarEntry {
Subject: string;
"Start Date": string;
"Start Time"?: string
"End Date"?: string
"End Time"?: string
"All Day Event": "True";
Description?: string
Location?: string
Private?: boolean
}
const myHeaders: HeadersInit = {};
myHeaders["accept"] = "application/json, text/plain, */*";
myHeaders["content-type"] = "application/x-www-form-urlencoded; charset=UTF-8";
const raw = "number=135b&streetId=11609783";
const requestOptions: RequestInit = {
method: "POST",
headers: myHeaders,
body: raw,
redirect: "follow",
};
fetch(
"https://pluginssl.ecoharmonogram.pl/api/v1/plugin/v1/schedules",
requestOptions
)
.then((response) => {
return response.text();
})
.then((response) => {
return JSON.parse(response.trim());
})
.then((result: ResultAPI) => {
const mappedSchedules: Record<string, ScheduleDescription> =
result.scheduleDescription.reduce((list, current) => {
list[current.id] = current;
return list;
}, {} as Record<string, ScheduleDescription>);
result.schedules.forEach(({ scheduleDescriptionId, days, month, year }) => {
if (!mappedSchedules[scheduleDescriptionId]) {
return;
}
if (mappedSchedules[scheduleDescriptionId]!.name === "TERMINY PŁATNOŚCI") {
return;
}
days.split(";").forEach((day) => {
const entry: CalendarEntry = {
Subject: mappedSchedules[scheduleDescriptionId]!.name,
"Start Date": dayjs()
.month(parseInt(month, 10) - 1)
.date(parseInt(day, 10))
.set("year", parseInt(year, 10))
.format("MM/DD/YYYY"),
"All Day Event": "True",
};
calendar.push(entry);
});
});
(async () => {
const csv = new ObjectsToCsv(calendar);
// Save to file:
await csv.toDisk("./wywoz.csv", { bom: true });
})();
})
.catch((error) => console.log("error", error));
const calendar: CalendarEntry[] = [];