71 lines
2.1 KiB
TypeScript
71 lines
2.1 KiB
TypeScript
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",
|
|
bankstatements: "bankstatement"
|
|
}
|
|
|
|
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)
|
|
}
|
|
}
|