import type { RequestHandler } from './$types';
import {
	centrawarungJson,
	centrawarungOptionsResponse,
	centrawarungServerError,
	guardCentrawarungGet,
	listBackofficeUsersForCentrawarung
} from '$lib/server/centrawarung-api';

export const GET: RequestHandler = async (event) => {
	const authError = await guardCentrawarungGet(event);
	if (authError) return authError;

	try {
		const result = await listBackofficeUsersForCentrawarung(event.url);
		return centrawarungJson(result);
	} catch {
		return centrawarungServerError('Gagal mengambil data backoffice user.');
	}
};

export const OPTIONS: RequestHandler = async () => centrawarungOptionsResponse();
