-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.ts
114 lines (94 loc) · 4.15 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
112
113
114
import { createDirectus, rest } from '@directus/sdk';
import { get_actions } from './routes/actions/get_actions';
import { get_collectors } from './routes/collectors/get_collectors';
import { get_materials } from './routes/materials/get_materials';
import { create_event } from './routes/events/create_event';
import { get_events } from './routes/events/get_events';
import { get_actions_and_materials_for_role } from './business_logic/get_actions_and_materials_for_role';
import { get_aggregate_data } from './business_logic/get_aggregates';
const client = createDirectus('https://enaleia.directus.app').with(rest());
Bun.serve({
fetch: async (req) => {
const url = new URL(req.url);
if (url.pathname === "/v1") {
return new Response("All directus data - V1");
}
if (url.pathname === "/v1/boats") {
return new Response("All Boats data!");
}
if (url.pathname === "/v1/create_event") {
const body = await req.json(); // Ensure to parse the request body
if (!body) {
return new Response("Invalid request body", { status: 400 });
}
const { role, actionId, weights, details }: { role: any, actionId: string, weights: any, details: any } = body;
const params = { role, actionId, weights, details };
try {
const result = await create_event(client, params)
return new Response(JSON.stringify(result), { status: 201 });
} catch (error) {
return new Response("Error creating item", { status: 500 });
}
}
if (url.pathname === "/v1/actions") {
const rolesParam = url.searchParams.get("roles"); // Get roles from query parameters
const roles = rolesParam ? rolesParam.split(',') : []; // Split into an array
try {
const result = await get_actions(client, roles);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
if (url.pathname === "/v1/events") {
const eventId = url.searchParams.get("eventId"); // Get roleId from query parameters
try {
const result = await get_events(client, eventId);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
if (url.pathname === "/v1/materials") {
const rolesParam = url.searchParams.get("roles"); // Get roles from query parameters
const roles = rolesParam ? rolesParam.split(',') : []; // Split into an array
try {
const result = await get_materials(client, roles);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
if (url.pathname === "/v1/get_actions_and_materials_for_role") {
const rolesParam = url.searchParams.get("roles"); // Get roles from query parameters
const roles = rolesParam ? rolesParam.split(',') : []; // Split into an array
if (!roles) {
return new Response("Missing roleId parameter", { status: 400 });
}
try {
const result = await get_actions_and_materials_for_role(client, roles);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
if (url.pathname === "/v1/aggregates") {
try {
const result = await get_aggregate_data(client);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
if (url.pathname === "/v1/collectors") {
try {
const roleId = url.searchParams.get("collectorId"); // Get roleId from query parameters
const result = await get_collectors(client, roleId);
return new Response(JSON.stringify(result), { status: 200 });
} catch (error) {
return new Response("Error fetching data", { status: 500 });
}
}
return new Response("404!", { status: 404 });
},
});