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

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

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

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