-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathVendoController.ts
42 lines (37 loc) · 1.15 KB
/
VendoController.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
import { DateTime } from "luxon";
import { Profile, RequestType, retrieveCombinedConnections, retrieveConnections } from "./requests.ts";
import { Controller, Get, Queries, Res, Route, Tags, type TsoaResponse } from "tsoa";
import type { Journey } from "../../models/connection.ts";
class VendoQuery {
evaNumber!: string;
type!: RequestType;
profile!: Profile;
when?: string;
duration?: number = 60;
results?: number = 1000;
}
@Route("timetable/vendo")
@Tags("Vendo")
export class VendoController extends Controller {
@Get()
async getVendoJourneys(
@Queries() query: VendoQuery,
@Res() badRequestResponse: TsoaResponse<400, { reason: string }>
): Promise<Journey[]> {
query = {
...query,
when: query.when ?? DateTime.now().set({ second: 0, millisecond: 0 }).toISO()
};
if (!DateTime.fromISO(query.when!).isValid) {
return badRequestResponse(400, { reason: "Invalid date" });
}
if (query.profile === Profile.COMBINED) {
return (await retrieveCombinedConnections(query)).map((connection) => ({
connections: [connection]
}));
}
return (await retrieveConnections(query)).map((connection) => ({
connections: [connection]
}));
}
}