Files
FEDEO/src/routes/banking.ts
2025-09-12 19:40:53 +02:00

74 lines
2.1 KiB
TypeScript

import { FastifyInstance } from "fastify";
import jwt from "jsonwebtoken";
import {insertHistoryItem} from "../utils/history";
export default async function bankingRoutes(server: FastifyInstance) {
//Create Banking Statement
server.post("/banking/statements", async (req, reply) => {
if (!req.user) {
return reply.code(401).send({ error: "Unauthorized" });
}
const body = req.body as { data: string };
console.log(body);
const {data,error} = await server.supabase.from("statementallocations").insert({
//@ts-ignore
...body.data,
tenant: req.user.tenant_id,
}).select()
await insertHistoryItem(server,{
entity: "bankstatements",
//@ts-ignore
entityId: data.id,
action: "created",
created_by: req.user.user_id,
tenant_id: req.user.tenant_id,
oldVal: null,
newVal: data,
text: `Buchung erstellt`,
});
if(data && !error){
return reply.send(data)
}
});
//Delete Banking Statement
server.delete("/banking/statements/:id", async (req, reply) => {
if (!req.user) {
return reply.code(401).send({ error: "Unauthorized" });
}
const { id } = req.params as { id?: string }
const {data} = await server.supabase.from("statementallocations").select().eq("id",id).single()
const {error} = await server.supabase.from("statementallocations").delete().eq("id",id)
if(!error){
await insertHistoryItem(server,{
entity: "bankstatements",
entityId: id,
action: "deleted",
created_by: req.user.user_id,
tenant_id: req.user.tenant_id,
oldVal: data,
newVal: null,
text: `Buchung gelöscht`,
});
return reply.send({success:true})
} else {
return reply.code(500).send({error:"Fehler beim löschen"})
}
})
}