Changes
This commit is contained in:
69
src/utils/history.ts
Normal file
69
src/utils/history.ts
Normal file
@@ -0,0 +1,69 @@
|
||||
import { FastifyInstance } from "fastify"
|
||||
|
||||
export async function insertHistoryItem(
|
||||
server: FastifyInstance,
|
||||
params: {
|
||||
tenant_id: number
|
||||
created_by: string | null
|
||||
entity: string
|
||||
entityId: string | number
|
||||
action: "created" | "updated" | "unchanged" | "deleted" | "archived"
|
||||
oldVal?: any
|
||||
newVal?: any
|
||||
text?: string
|
||||
}
|
||||
) {
|
||||
const textMap = {
|
||||
created: `Neuer Eintrag in ${params.entity} erstellt`,
|
||||
updated: `Eintrag in ${params.entity} geändert`,
|
||||
archived: `Eintrag in ${params.entity} archiviert`,
|
||||
deleted: `Eintrag in ${params.entity} gelöscht`
|
||||
}
|
||||
|
||||
const columnMap: Record<string, string> = {
|
||||
customers: "customer",
|
||||
vendors: "vendor",
|
||||
projects: "project",
|
||||
plants: "plant",
|
||||
contacts: "contact",
|
||||
inventoryitems: "inventoryitem",
|
||||
products: "product",
|
||||
profiles: "profile",
|
||||
absencerequests: "absencerequest",
|
||||
events: "event",
|
||||
tasks: "task",
|
||||
vehicles: "vehicle",
|
||||
costcentres: "costcentre",
|
||||
ownaccounts: "ownaccount",
|
||||
documentboxes: "documentbox",
|
||||
hourrates: "hourrate",
|
||||
services: "service",
|
||||
roles: "role",
|
||||
checks: "check",
|
||||
spaces: "space",
|
||||
trackingtrips: "trackingtrip",
|
||||
createddocuments: "createddocument",
|
||||
inventoryitemgroups: "inventoryitemgroup"
|
||||
}
|
||||
|
||||
const fkColumn = columnMap[params.entity]
|
||||
if (!fkColumn) {
|
||||
server.log.warn(`Keine History-Spalte für Entity: ${params.entity}`)
|
||||
return
|
||||
}
|
||||
|
||||
const entry = {
|
||||
tenant: params.tenant_id,
|
||||
created_by: params.created_by,
|
||||
text: params.text || textMap[params.action],
|
||||
action: params.action,
|
||||
[fkColumn]: params.entityId,
|
||||
oldVal: params.oldVal ? JSON.stringify(params.oldVal) : null,
|
||||
newVal: params.newVal ? JSON.stringify(params.newVal) : null
|
||||
}
|
||||
|
||||
const { error } = await server.supabase.from("historyitems").insert([entry])
|
||||
if (error) { // @ts-ignore
|
||||
console.log(error)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user