import { checkStoreSupportCode } from '../helpers/pocketbaseApi';
export function onRequestOptions() {
return new Response(null, {
status: 204,
headers: {
'content-type': 'application/json;charset=UTF-8',
'Access-Control-Allow-Origin': '*',
'Access-Control-Allow-Methods': 'GET, HEAD, POST, OPTIONS',
'Access-Control-Allow-Headers': '*',
},
});
}
export async function onRequest({ request, env }) {
if (request.method !== 'POST') {
return new Response('Method not allowed', { status: 405 });
}
const { supportCode } = await request.json();
if (!supportCode) {
return new Response('Missing support code', { status: 400 });
}
try {
const { status, error, storeHash } = await checkStoreSupportCode(supportCode, env);
if (error) {
return new Response(JSON.stringify({ status, error }));
}
return new Response(JSON.stringify({ status, storeHash }));
} catch (err) {
console.log(err);
return new Response(JSON.stringify({ status: 'error', error: 'An unknown error occured' }));
}
}