2025-06-30 18:58:24 +02:00
|
|
|
/*
|
|
|
|
SPDX-FileCopyrightText: © 2025 Hornwitser <code@hornwitser.no>
|
|
|
|
SPDX-License-Identifier: AGPL-3.0-or-later
|
|
|
|
*/
|
2025-06-11 21:05:17 +02:00
|
|
|
import { z } from "zod/v4-mini";
|
2025-06-23 12:48:09 +02:00
|
|
|
import { readSchedule, writeSchedule } from "~/server/database";
|
2025-06-11 21:05:17 +02:00
|
|
|
import { broadcastEvent } from "~/server/streams";
|
|
|
|
import { apiScheduleSchema } from "~/shared/types/api";
|
|
|
|
import { applyUpdatesToArray } from "~/shared/utils/update";
|
2025-03-11 14:11:05 +01:00
|
|
|
|
|
|
|
export default defineEventHandler(async (event) => {
|
2025-06-09 16:51:05 +02:00
|
|
|
const session = await requireServerSession(event);
|
2025-03-11 14:11:05 +01:00
|
|
|
|
2025-06-23 00:17:22 +02:00
|
|
|
if (session.account.type !== "admin" && session.account.type !== "crew") {
|
2025-03-11 14:11:05 +01:00
|
|
|
throw createError({
|
|
|
|
status: 403,
|
|
|
|
statusMessage: "Forbidden",
|
|
|
|
message: "Only crew and admin accounts can edit the schedule.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
const { success, error, data: update } = apiScheduleSchema.safeParse(await readBody(event));
|
|
|
|
if (!success) {
|
|
|
|
throw createError({
|
|
|
|
status: 400,
|
|
|
|
statusText: "Bad Request",
|
|
|
|
message: z.prettifyError(error),
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
|
|
|
if (update.deleted) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
statusMessage: "Not implemented",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2025-03-11 14:11:05 +01:00
|
|
|
const schedule = await readSchedule();
|
2025-06-11 21:05:17 +02:00
|
|
|
|
|
|
|
if (schedule.deleted) {
|
|
|
|
throw createError({
|
|
|
|
statusCode: 400,
|
|
|
|
statusMessage: "Not implemented",
|
|
|
|
});
|
|
|
|
}
|
2025-03-11 14:11:05 +01:00
|
|
|
|
|
|
|
// Validate edit restrictions for crew
|
2025-06-23 00:17:22 +02:00
|
|
|
if (session.account.type === "crew") {
|
2025-06-11 21:05:17 +02:00
|
|
|
if (update.locations?.length) {
|
2025-03-11 14:11:05 +01:00
|
|
|
throw createError({
|
|
|
|
status: 403,
|
|
|
|
statusMessage: "Forbidden",
|
|
|
|
message: "Only admin accounts can edit locations.",
|
|
|
|
});
|
|
|
|
}
|
2025-06-11 21:05:17 +02:00
|
|
|
for (const event of update.events ?? []) {
|
|
|
|
const original = schedule.events?.find(e => e.id === event.id);
|
|
|
|
if (original && !original.deleted && !original.crew) {
|
2025-03-11 14:11:05 +01:00
|
|
|
throw createError({
|
|
|
|
status: 403,
|
|
|
|
statusMessage: "Forbidden",
|
|
|
|
message: "Only admin accounts can edit public events.",
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2025-06-11 21:05:17 +02:00
|
|
|
// Update schedule
|
|
|
|
const updatedFrom = schedule.updatedAt;
|
|
|
|
update.updatedAt = new Date().toISOString();
|
|
|
|
if (update.events) {
|
|
|
|
for (const event of update.events) event.updatedAt = update.updatedAt;
|
|
|
|
applyUpdatesToArray(update.events, schedule.events = schedule.events ?? []);
|
|
|
|
}
|
|
|
|
if (update.locations) {
|
|
|
|
for (const location of update.locations) location.updatedAt = update.updatedAt;
|
|
|
|
applyUpdatesToArray(update.locations, schedule.locations = schedule.locations ?? []);
|
|
|
|
}
|
|
|
|
if (update.roles) {
|
|
|
|
for (const role of update.roles) role.updatedAt = update.updatedAt;
|
|
|
|
applyUpdatesToArray(update.roles, schedule.roles = schedule.roles ?? []);
|
|
|
|
}
|
|
|
|
if (update.shifts) {
|
|
|
|
for (const shift of update.shifts) shift.updatedAt = update.updatedAt;
|
|
|
|
applyUpdatesToArray(update.shifts, schedule.shifts = schedule.shifts ?? []);
|
|
|
|
}
|
2025-03-11 14:11:05 +01:00
|
|
|
|
|
|
|
await writeSchedule(schedule);
|
2025-06-11 21:05:17 +02:00
|
|
|
await broadcastEvent({
|
|
|
|
type: "schedule-update",
|
|
|
|
updatedFrom,
|
|
|
|
data: update,
|
|
|
|
});
|
2025-03-11 14:11:05 +01:00
|
|
|
})
|