-
Notifications
You must be signed in to change notification settings - Fork 0
/
mod.ts
65 lines (57 loc) · 1.68 KB
/
mod.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
import { format, serve } from "./deps.ts";
import {
addForecastData,
fetchForecastAssignments,
fetchForecastUsers,
ForecastAssignment,
} from "./src/forecast.ts";
import {
Absence,
addTimetasticData,
fetchTimetasticHolidays,
fetchTimetasticUsers,
} from "./src/timetastic.ts";
import { calculateWorkless, mergeUsers } from "./src/utils.ts";
export interface User {
workless: boolean;
email: string;
timetastic_id?: number;
forecast_id?: number;
harvest_id?: number;
first_name: string;
last_name: string;
absence?: Absence;
assignment?: ForecastAssignment;
}
async function handler(req: Request) {
const url = new URL(req.url);
const checkDate = url.searchParams.get("date") ||
format(new Date(), "yyyy-MM-dd");
const token = req.headers.get("Authorization");
if (token != `Bearer ${Deno.env.get("API_TOKEN")}`) {
return new Response("Not authorized", { status: 401 });
}
try {
const forecastUsers = await fetchForecastUsers();
const timetasticUsers = await fetchTimetasticUsers();
const timetasticHolidays = await fetchTimetasticHolidays(checkDate);
const forecastAssignments = await fetchForecastAssignments(checkDate);
const userLookup = mergeUsers(forecastUsers, timetasticUsers);
addTimetasticData(timetasticHolidays, userLookup);
addForecastData(forecastAssignments, userLookup);
calculateWorkless(userLookup);
const result = {
date: checkDate,
users: userLookup,
};
return new Response(JSON.stringify(result), {
status: 200,
headers: {
"content-type": "application/json",
},
});
} catch (error) {
return new Response(error, { status: 500 });
}
}
serve(handler);