2026-02-14 02:20:19 +00:00

48 lines
1.3 KiB
TypeScript

// routes/api/retrieve/obligations.ts
import { define } from "../../../utils.ts";
import { SessionCookie } from "../../auth/callback.ts";
import { getObligationsByUser } from "../../../services/financhalServices.ts";
export const handler = define.handlers({
async GET(ctx) {
try {
const stateData: SessionCookie = ctx.state.data;
const { user_id } = stateData;
if (!user_id) {
return new Response(
JSON.stringify({ error: "Unauthorized" }),
{
status: 401,
headers: { "Content-Type": "application/json" },
},
);
}
// Get all obligation records for this user only (data isolation)
const obligations = await getObligationsByUser(user_id);
return new Response(
JSON.stringify({
success: true,
obligations,
count: obligations.length,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (error) {
console.error("Error fetching obligations:", error);
return new Response(
JSON.stringify({ error: String(error) }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
},
});