38 lines
1.0 KiB
TypeScript
38 lines
1.0 KiB
TypeScript
import { serve } from "https://deno.land/std@0.168.0/http/server.ts";
|
|
import { mydb } from "../../lib/mySupabase.ts";
|
|
import { corsHeaders, jsonResponse, errorResponse } from "../../lib/utils.ts";
|
|
import { validateAuth } from "../../lib/auth.ts";
|
|
|
|
serve(async (req) => {
|
|
if (req.method === "OPTIONS") {
|
|
return new Response("ok", { headers: corsHeaders() });
|
|
}
|
|
|
|
try {
|
|
const auth = await validateAuth(req);
|
|
if (!auth) {
|
|
return errorResponse("Não autorizado", 401);
|
|
}
|
|
|
|
if (req.method !== "GET") {
|
|
return errorResponse("Method not allowed", 405);
|
|
}
|
|
|
|
const res = await mydb
|
|
.from("patient_rewards")
|
|
.select("*")
|
|
.eq("patient_id", auth.userId)
|
|
.single();
|
|
|
|
return jsonResponse({
|
|
points: res.data?.points || 0,
|
|
level: res.data?.level || 1,
|
|
streak_days: res.data?.streak_days || 0,
|
|
achievements: res.data?.achievements || [],
|
|
});
|
|
} catch (error: unknown) {
|
|
const err = error as Error;
|
|
return errorResponse(err.message, 500);
|
|
}
|
|
});
|