32 lines
1.1 KiB
TypeScript
32 lines
1.1 KiB
TypeScript
import { json } from '@sveltejs/kit';
|
|
import type { RequestHandler } from './$types';
|
|
import { apiBody, apiFailure } from '#lib/server/api-http.js';
|
|
import { addPieceUsage, getShow } from '#lib/server/shows.js';
|
|
import {
|
|
auditTechnical,
|
|
authenticateTechnical,
|
|
idempotentResponse
|
|
} from '#lib/server/technical-access.js';
|
|
|
|
export const POST: RequestHandler = async ({ request, params }) => {
|
|
try {
|
|
const actor = authenticateTechnical(request, true);
|
|
const body = await apiBody(request);
|
|
const result = idempotentResponse(actor, request, body, () => {
|
|
const created = addPieceUsage(params.id, Number(body.revision), {
|
|
pieceId: String(body.pieceId ?? ''),
|
|
sectionId: body.sectionId ? String(body.sectionId) : null,
|
|
position: body.position == null ? undefined : Number(body.position)
|
|
});
|
|
auditTechnical(actor, 'usage.create', 'usage', created.id);
|
|
return { status: 201, body: { data: getShow(params.id), usageId: created.id } };
|
|
});
|
|
return json(result.body, {
|
|
status: result.status,
|
|
headers: { 'Idempotency-Replayed': result.duplicate ? 'true' : 'false' }
|
|
});
|
|
} catch (cause) {
|
|
return apiFailure(cause);
|
|
}
|
|
};
|