From 4016c8b6b54debace24f4d0aceecfcd457993c64 Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Wed, 10 Dec 2025 08:32:22 +0100 Subject: [PATCH 1/2] Redone --- src/routes/resources/main.ts | 93 +++++++++++++++++++----------------- 1 file changed, 49 insertions(+), 44 deletions(-) diff --git a/src/routes/resources/main.ts b/src/routes/resources/main.ts index c0bf1f8..5f279fb 100644 --- a/src/routes/resources/main.ts +++ b/src/routes/resources/main.ts @@ -477,58 +477,63 @@ export default async function resourceRoutes(server: FastifyInstance) { // UPDATE (inkl. Soft-Delete/Archive) server.put("/resource/:resource/:id", async (req, reply) => { - const {resource, id} = req.params as { resource: string; id: string } - const body = req.body as Record + try { + const {resource, id} = req.params as { resource: string; id: string } + const body = req.body as Record - const tenantId = (req.user as any)?.tenant_id - const userId = (req.user as any)?.user_id + const tenantId = (req.user as any)?.tenant_id + const userId = (req.user as any)?.user_id - if (!tenantId || !userId) { - return reply.code(401).send({error: "Unauthorized"}) - } - - const table = resourceConfig[resource].table - - //TODO: HISTORY - - const normalizeDate = (val: any) => { - const d = new Date(val) - return isNaN(d.getTime()) ? null : d - } - - console.log(body) - - Object.keys(body).forEach((key) => { - if(key.includes("_at") || key.includes("At")) { - body[key] = normalizeDate(body[key]) + if (!tenantId || !userId) { + return reply.code(401).send({error: "Unauthorized"}) } - }) - const [updated] = await server.db - .update(table) - .set({...body, updated_at: new Date().toISOString(), updated_by: userId}) - .where(and( - eq(table.id, id), - eq(table.tenant, tenantId))) - .returning() + const table = resourceConfig[resource].table - //const diffs = diffObjects(oldItem, newItem); + //TODO: HISTORY + + const normalizeDate = (val: any) => { + const d = new Date(val) + return isNaN(d.getTime()) ? null : d + } + + console.log(body) + + Object.keys(body).forEach((key) => { + if(key.includes("_at") || key.includes("At")) { + body[key] = normalizeDate(body[key]) + } + }) + + const [updated] = await server.db + .update(table) + .set({...body, updated_at: new Date().toISOString(), updated_by: userId}) + .where(and( + eq(table.id, id), + eq(table.tenant, tenantId))) + .returning() + + //const diffs = diffObjects(oldItem, newItem); - /*for (const d of diffs) { - await insertHistoryItem(server, { - entity: resource, - entityId: id, - action: d.type, - created_by: userId, - tenant_id: tenantId, - oldVal: d.oldValue ? String(d.oldValue) : null, - newVal: d.newValue ? String(d.newValue) : null, - text: `Feld "${d.label}" ${d.typeLabel}: ${d.oldValue ?? ""} → ${d.newValue ?? ""}`, - }); - }*/ + /*for (const d of diffs) { + await insertHistoryItem(server, { + entity: resource, + entityId: id, + action: d.type, + created_by: userId, + tenant_id: tenantId, + oldVal: d.oldValue ? String(d.oldValue) : null, + newVal: d.newValue ? String(d.newValue) : null, + text: `Feld "${d.label}" ${d.typeLabel}: ${d.oldValue ?? ""} → ${d.newValue ?? ""}`, + }); + }*/ + + return updated + } catch (err) { + console.log("ERROR /resource/projects/:id", err) + } - return updated }) } From 58836fb0efa26e2f28e38ef663be580adf9281e9 Mon Sep 17 00:00:00 2001 From: florianfederspiel Date: Wed, 10 Dec 2025 08:39:44 +0100 Subject: [PATCH 2/2] Redone --- src/routes/resources/main.ts | 28 ++++++++++++++++------------ 1 file changed, 16 insertions(+), 12 deletions(-) diff --git a/src/routes/resources/main.ts b/src/routes/resources/main.ts index 5f279fb..7d781bd 100644 --- a/src/routes/resources/main.ts +++ b/src/routes/resources/main.ts @@ -373,13 +373,13 @@ export default async function resourceRoutes(server: FastifyInstance) { // ------------------------------------------------------------- // DETAIL (mit JOINS) // ------------------------------------------------------------- - server.get("/resource/:resource/:id", async (req, reply) => { + server.get("/resource/:resource/:id/:no_relations?", async (req, reply) => { try { const { id } = req.params as { id: string } const tenantId = req.user?.tenant_id if (!tenantId) return reply.code(400).send({ error: "No tenant selected" }) - const {resource} = req.params as { resource: string } + const {resource, no_relations} = req.params as { resource: string, no_relations?: boolean } const table = resourceConfig[resource].table const projRows = await server.db @@ -402,20 +402,24 @@ export default async function resourceRoutes(server: FastifyInstance) { ...projRows[0] } - if(resourceConfig[resource].mtoLoad) { - for await (const relation of resourceConfig[resource].mtoLoad ) { - if(data[relation]) { - data[relation] = (await server.db.select().from(resourceConfig[relation + "s"].table).where(eq(resourceConfig[relation + "s"].table.id, data[relation])))[0] + if(!no_relations) { + if(resourceConfig[resource].mtoLoad) { + for await (const relation of resourceConfig[resource].mtoLoad ) { + if(data[relation]) { + data[relation] = (await server.db.select().from(resourceConfig[relation + "s"].table).where(eq(resourceConfig[relation + "s"].table.id, data[relation])))[0] + } + } + } + + if(resourceConfig[resource].mtmLoad) { + for await (const relation of resourceConfig[resource].mtmLoad ) { + console.log(relation) + data[relation] = await server.db.select().from(resourceConfig[relation].table).where(eq(resourceConfig[relation].table[resource.substring(0,resource.length - 1)],id)) } } } - if(resourceConfig[resource].mtmLoad) { - for await (const relation of resourceConfig[resource].mtmLoad ) { - console.log(relation) - data[relation] = await server.db.select().from(resourceConfig[relation].table).where(eq(resourceConfig[relation].table[resource.substring(0,resource.length - 1)],id)) - } - } + return data