Implement access controlled edit schedule endpoint

Add PATCH /api/schedule endpoint for editing the schedule in a manner
that's access controlled.
This commit is contained in:
Hornwitser 2025-03-11 14:11:05 +01:00
parent bb306ee938
commit 5255ed698e
3 changed files with 121 additions and 0 deletions

View file

@ -49,3 +49,15 @@ export interface Schedule {
roles?: Role[],
rota?: Shift[],
}
export type ChangeRecord<T extends { id: string }> =
| { op: "set", data: T }
| { op: "del", data: { id: string }}
;
export interface SchedulePatch {
locations?: ChangeRecord<ScheduleLocation>[],
events?: ChangeRecord<ScheduleEvent>[],
roles?: ChangeRecord<Role>[],
rota?: ChangeRecord<Shift>[],
}

21
shared/utils/changes.ts Normal file
View file

@ -0,0 +1,21 @@
import type { ChangeRecord } from "~/shared/types/schedule";
export function applyChange<T extends { id: string }>(change: ChangeRecord<T>, data: T[]) {
const index = data.findIndex(item => item.id === change.data.id);
if (change.op === "del") {
if (index !== -1)
data.splice(index, 1);
} else if (change.op === "set") {
if (index !== -1)
data.splice(index, 1, change.data);
else
data.push(change.data)
}
}
export function applyChangeArray<T extends { id: string }>(changes: ChangeRecord<T>[], data: T[]) {
// Note: quadratic complexity due to findIndex in applyChange
for (const change of changes) {
applyChange(change, data);
}
}