46 lines
1.4 KiB
TypeScript
46 lines
1.4 KiB
TypeScript
// routes/api/delete/income.ts
|
|
import { define } from "../../../utils.ts";
|
|
import { deleteIncome } from "../../../services/financhalServices.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 income_id = formData.get("income_id");
|
|
const sender = formData.get("sender");
|
|
|
|
if (!income_id || !sender) {
|
|
return new Response("Missing income_id or sender", { status: 400 });
|
|
}
|
|
|
|
// Get user_id from session for cache invalidation
|
|
const stateData: SessionCookie = ctx.state.data;
|
|
|
|
await deleteIncome(income_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);
|
|
},
|
|
});
|