-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.ts
76 lines (63 loc) · 2.12 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
import { select } from "@inquirer/prompts";
import { Separator } from "@inquirer/select";
import { format, parse } from "date-fns";
import chalk from "chalk";
import type { Workout } from "./types.ts";
import { StcApi } from "./api.ts";
const token = process.env.STC_TOKEN;
if (!token) {
throw new Error("Token must be specified in STC_TOKEN env var");
}
// TODO: Make selected gyms configurable in the app
const clubs = (process.env.CLUBS || "").split(",");
const api = new StcApi(token);
const [workouts, bookings] = await Promise.all([
api.getWorkouts(clubs),
api.getBookings(),
]);
const bookedWorkoutIds = new Set(bookings.map((b) => b.workoutSessionId));
const dayGroupFormat = "y/LL/dd";
const workoutsByDay = workouts
.filter((w) => !w.name.startsWith("Virtual "))
.reduce<{ [day: string]: Workout[] }>((acc, w) => {
const day = format(new Date(w.duration.start), dayGroupFormat);
if (!acc[day]) {
acc[day] = [];
}
return { ...acc, [day]: [...acc[day], w] };
}, {});
const choices: any[] = [];
Object.entries(workoutsByDay).forEach(([day, dayWorkouts]) => {
choices.push(
new Separator(
chalk.blue(
format(parse(day, dayGroupFormat, new Date()), "EEEE dd/LL/y"),
),
),
);
dayWorkouts.forEach((w) => {
const disabled = bookedWorkoutIds.has(w.id)
? `(${chalk.yellowBright("already booked")})`
: w.slots <= 0
? `(${chalk.red("workout is full")})`
: new Date(w.bookable.earliest) > new Date()
? "(too early to book)"
: false;
choices.push({
name: `${format(new Date(w.duration.start), "HH:mm")} ${w.name.trim()}${!disabled ? ` (${chalk.green(w.slots)} slot${w.slots > 1 ? "s" : ""})` : ""} - ${w.club.name}`,
value: w.id + "",
disabled,
});
});
});
const selectedWorkoutId = await select({
message: "Select a workout",
pageSize: 80,
choices,
});
if (selectedWorkoutId) {
const bookedWorkout = await api.createBooking(selectedWorkoutId as number);
console.log(chalk.green(`Workout "${bookedWorkout.name}" has been booked!`));
} else {
console.log(chalk.red("Booking process cancelled"));
}