2026-06-04 13:05:50 +00:00

47 lines
1.5 KiB
TypeScript

// routes/api/delete/obligation.ts
import { deleteObligation } from "../../../services/financhalServices.ts";
import { define } from "../../../utils.ts";
import { SessionCookie } from "../../auth/callback.ts";
import { invalidateBalanceCache } from "../../../services/dailyBalanceCache.ts";
export const handler = define.handlers({
async POST(ctx) {
const formData = await ctx.req.formData();
const obligation_id = formData.get("obligation_id");
const sender = formData.get("sender");
if (!obligation_id || !sender) {
return new Response("Missing obligation_id or sender", { status: 400 });
}
// Get user_id from session for cache invalidation
const stateData: SessionCookie = ctx.state.data;
// Delete the obligation using your service function
await deleteObligation(obligation_id.toString());
// Invalidate balance cache for this user
if (stateData.user_id) {
try {
await invalidateBalanceCache(stateData.user_id);
} catch (cacheError) {
console.error("Failed to invalidate cache:", cacheError);
// Non-fatal error - continue with redirect
}
}
// Redirect back to the sender URL (must be absolute)
const senderPath = sender.toString();
let redirectUrl = senderPath;
try {
if (senderPath.startsWith("/")) {
const url = new URL(ctx.req.url);
redirectUrl = url.origin + senderPath;
}
} catch (_) {
// fallback: just use senderPath
}
return Response.redirect(redirectUrl, 303);
},
});