Skip to content

Commit 82ae274

Browse files
authored
Cafe sign up handling (#1144)
Fix various bugs with the cafe page, namely: - Stopping users from signing up to passed - Stopping users from abandoning shifts the day before - Fix timezone and local time isssues (you can now sign up on Mondays) - Fix day names - Hide sign up fields for non logged in members
1 parent 7e9e58c commit 82ae274

5 files changed

Lines changed: 75 additions & 16 deletions

File tree

src/routes/(app)/committees/cafe/+page.server.ts

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -110,6 +110,13 @@ export const actions: Actions = {
110110

111111
const { date, worker, timeSlot } = form.data;
112112

113+
const parsedDate = dayjs(date, "YYYY-MM-DD", true);
114+
if (!parsedDate.isValid()) {
115+
return fail(400, { form });
116+
}
117+
const startOfDay = parsedDate.startOf("day").toDate();
118+
const nextDay = parsedDate.add(1, "day").startOf("day").toDate();
119+
113120
const member = worker || user.studentId;
114121
if (!member) {
115122
return fail(400, { form });
@@ -124,7 +131,7 @@ export const actions: Actions = {
124131
}
125132
}
126133
const dayShifts = await prisma.cafeShift.findMany({
127-
where: { date: date },
134+
where: { date: { gte: startOfDay, lt: nextDay } },
128135
include: { worker: { select: { studentId: true } } },
129136
});
130137

@@ -138,6 +145,12 @@ export const actions: Actions = {
138145
type: "error",
139146
});
140147
}
148+
if (!isSetByAdmin && parsedDate.isBefore(dayjs(), "day")) {
149+
return message(form, {
150+
message: m.cafe_error_sign_on_after(),
151+
type: "error",
152+
});
153+
}
141154
// Check if the user already has a shift
142155
const tempShift = dayShifts.filter(
143156
(shift) =>
@@ -153,7 +166,7 @@ export const actions: Actions = {
153166
try {
154167
await prisma.cafeShift.create({
155168
data: {
156-
date: date,
169+
date: startOfDay,
157170
worker: {
158171
connect: {
159172
studentId: member,
@@ -179,14 +192,25 @@ export const actions: Actions = {
179192
} else {
180193
// There is already a shift.
181194
if (cafeShift.worker.studentId === member) {
182-
await prisma.cafeShift.delete({ where: { id: cafeShift.id } });
183-
return message(form, {
184-
message:
185-
member === user.studentId
186-
? m.cafe_quit_shift()
187-
: m.cafe_quit_shift_for_other({ name: member }),
188-
type: "success",
189-
});
195+
const shiftDate = dayjs(cafeShift.date);
196+
if (isSetByAdmin || shiftDate > dayjs().add(1, "day")) {
197+
await prisma.cafeShift.delete({ where: { id: cafeShift.id } });
198+
return message(form, {
199+
message:
200+
member === user.studentId
201+
? m.cafe_quit_shift()
202+
: m.cafe_quit_shift_for_other({ name: member }),
203+
type: "success",
204+
});
205+
} else {
206+
return message(form, {
207+
message:
208+
shiftDate > dayjs().subtract(1, "day")
209+
? m.cafe_error_sign_off_close()
210+
: m.cafe_error_sign_off_after(),
211+
type: "error",
212+
});
213+
}
190214
} else {
191215
// There is already a shift in this location, but it is not attributed to the user we are trying to edit.
192216
if (isSetByAdmin) {

src/routes/(app)/committees/cafe/CafeBookingCalendar.svelte

Lines changed: 27 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
import dayjs from "dayjs";
44
import weekYear from "dayjs/plugin/weekYear";
55
import weekOfYear from "dayjs/plugin/weekOfYear";
6-
import localeData from "dayjs/plugin/localeData";
76
import { enhance } from "$app/forms";
87
import Pagination from "$lib/components/Pagination.svelte";
98
import { isAuthorized } from "$lib/utils/authorization";
@@ -14,13 +13,34 @@
1413
import { tick } from "svelte";
1514
import type { ShiftWithWorker, Ciabatta } from "./types";
1615
import { TimeSlot } from "./types";
16+
import "dayjs/locale/en-gb";
17+
import { languageTag } from "$paraglide/runtime";
18+
import { page } from "$app/state";
1719
1820
dayjs.extend(weekOfYear);
1921
dayjs.extend(weekYear);
20-
dayjs.extend(localeData);
2122
2223
const getWeekdayName = (weekday: number): string => {
23-
return dayjs.weekdays(false)[weekday + 1] ?? "";
24+
let locale: string;
25+
switch (languageTag()) {
26+
case "sv": {
27+
locale = "sv-SE";
28+
break;
29+
}
30+
case "en": {
31+
locale = "en-GB";
32+
break;
33+
}
34+
default: {
35+
locale = "sv-SE";
36+
break;
37+
}
38+
}
39+
if (weekday < 0 || weekday > 6) return "";
40+
41+
// Reference Monday: Jan 5, 1970 was a Monday
42+
const referenceMonday = new Date(Date.UTC(1970, 0, 5 + weekday));
43+
return referenceMonday.toLocaleDateString(locale, { weekday: "long" });
2444
};
2545
2646
let {
@@ -40,6 +60,9 @@
4060
timeSlot: TimeSlot,
4161
user: AuthUser,
4262
) {
63+
if (!page.data.user?.memberId) {
64+
return false;
65+
}
4366
let worker = shifts.find(
4467
(s) => dayjs(s.date).isSame(day, "day") && s.timeSlot === timeSlot,
4568
)?.worker;
@@ -224,7 +247,7 @@
224247
({ update }) =>
225248
update({ reset: false })}
226249
>
227-
<input type="hidden" name="date" value={day} />
250+
<input type="hidden" name="date" value={day.format("YYYY-MM-DD")} />
228251
<input type="hidden" name="timeSlot" value={timeSlot} />
229252
{#if canEditWorkers && editing}
230253
<MemberSearchInput

src/routes/(app)/committees/cafe/types.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { Prisma } from "@prisma/client";
22
import { TimeSlot as PrismaTimeSlot } from "@prisma/client";
3+
import dayjs from "dayjs";
34
import { z } from "zod";
45
export type { CiabattaOfTheWeek as Ciabatta } from "@prisma/client";
56

@@ -12,7 +13,12 @@ export const TimeSlot = {
1213
export type TimeSlot = (typeof TimeSlot)[keyof typeof TimeSlot];
1314

1415
export const scheduleForm = z.object({
15-
date: z.date(),
16+
date: z
17+
.string()
18+
.regex(/^\d{4}-\d{2}-\d{2}$/)
19+
.refine((val) => dayjs(val, "YYYY-MM-DD", true).isValid(), {
20+
message: "Invalid date, expected format YYYY-MM-DD",
21+
}),
1622
worker: z.string().optional(),
1723
timeSlot: z.nativeEnum(TimeSlot),
1824
});

src/translations/en.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -911,11 +911,14 @@
911911
"cafe_quit_shift": "Abandoned the shift.",
912912
"cafe_quit_shift_for_other": "Removed {name} from the shift.",
913913
"cafe_changed_ciabatta": "Successfully changed ciabatta.",
914-
"cafe_error_already_have_shift": "Can't sign up, you already have a shift that day. If you need to sign up to more shifts, contact the Vice Head of the Cafe",
914+
"cafe_error_already_have_shift": "Can't sign up, you already have a shift that day. If you need to sign up to more shifts, contact the Vice Head of the Cafe.",
915915
"cafe_error_only_daymanagers": "Only day managers can sign up there.",
916916
"cafe_error_no_edit_worker_perms": "You don't have permissions to sign up for other people.",
917917
"cafe_error_worker_not_exist": "Worker {name} does not exist.",
918918
"cafe_error_no_ciabatta_edit_perms": "You are not permitted to edit ciabattas.",
919919
"cafe_error_no_week_viewing_perms": "You are not permitted to view this week",
920+
"cafe_error_sign_off_close": "Not permitted to abandon a shift so close in time. If you need to, contact the Vice Head of the Cafe.",
921+
"cafe_error_sign_off_after": "Not permitted to abandon an already completed shift.",
922+
"cafe_error_sign_on_after": "You can't sign up for a shift that's already happened.",
920923
"cafe_closed": "Closed"
921924
}

src/translations/sv.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -914,5 +914,8 @@
914914
"cafe_error_worker_not_exist": "Jobbare {name} existerar inte.",
915915
"cafe_error_no_ciabatta_edit_perms": "Du har inte tillåtelse att redigera ciabattas.",
916916
"cafe_error_no_week_viewing_perms": "Du har inte tillåtelse att se denna veckan.",
917+
"cafe_error_sign_off_close": "Du har inte tillåtelse att avanmäla dig så tätt inpå. Om du måste, kontaka Vice Cafémästare.",
918+
"cafe_error_sign_off_after": "Du har inte tillåtelse att avanmäla dig från avklarade skift.",
919+
"cafe_error_sign_on_after": "Du kan inte anmäla dig till ett pass som redan hänt.",
917920
"cafe_closed": "Stängt"
918921
}

0 commit comments

Comments
 (0)