21 lines
1 KiB
TypeScript
21 lines
1 KiB
TypeScript
import { requireStaff } from '../../../utils/guard';
|
|
import { staffSchema, updateStaff } from '../../../utils/booking';
|
|
import { recordAudit } from '../../../utils/audit';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
const admin = await requireStaff(event);
|
|
const id = getRouterParam(event, 'id') ?? '';
|
|
const parsed = staffSchema.safeParse(await readBody(event).catch(() => ({})));
|
|
if (!parsed.success) throw createError({ statusCode: 400, statusMessage: 'Invalid staff member.' });
|
|
try {
|
|
const staff = await updateStaff(id, parsed.data);
|
|
await recordAudit({ actorId: admin.id, action: 'staff.updated', target: id, meta: { slug: staff.slug } });
|
|
return { staff: { id: staff.id, slug: staff.slug } };
|
|
} catch (e) {
|
|
const code = (e as { code?: string }).code;
|
|
if (code === 'P2002') throw createError({ statusCode: 409, statusMessage: 'That slug is already in use.' });
|
|
if (code === 'P2025') throw createError({ statusCode: 404, statusMessage: 'Staff member not found.' });
|
|
throw e;
|
|
}
|
|
});
|