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

48 lines
1.2 KiB
TypeScript

// routes/api/retrieve/income.ts
import { define } from "../../../utils.ts";
import { SessionCookie } from "../../auth/callback.ts";
import { getIncomesByUser } 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 income records for this user only (data isolation)
const incomes = await getIncomesByUser(user_id);
return new Response(
JSON.stringify({
success: true,
incomes,
count: incomes.length,
}),
{
status: 200,
headers: { "Content-Type": "application/json" },
},
);
} catch (error) {
console.error("Error fetching incomes:", error);
return new Response(
JSON.stringify({ error: String(error) }),
{
status: 500,
headers: { "Content-Type": "application/json" },
},
);
}
},
});