From 3a98027f5e82cd6871564a630addd791bf180f1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Gjermund=20H=C3=B8s=C3=B8ien=20Wiggen?= Date: Sun, 7 Jun 2026 21:48:04 +0200 Subject: [PATCH] fix: scrip engine condition matching and ticket GET endpoint MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - engine.ts: remove transaction_type-based scrip filtering. Now uses condition evaluator exclusively (matching by scrip.condition_type against transaction types was incorrect — 'OnResolve' ≠ 'StatusChange'). All non-disabled, queue-matching scrips are evaluated. - tickets.ts: fix GET /:id — replace broken Drizzle 'with' relation with manual CF join. Set up Drizzle relations needed for proper schema typing (customFieldValues → customFields). --- src/routes/tickets.ts | 17 +++++++++++++---- src/scrip/engine.ts | 3 --- 2 files changed, 13 insertions(+), 7 deletions(-) diff --git a/src/routes/tickets.ts b/src/routes/tickets.ts index 311900e..f1fbd0e 100644 --- a/src/routes/tickets.ts +++ b/src/routes/tickets.ts @@ -73,12 +73,21 @@ export function createTicketsRouter(db: Db): Hono { const cfValues = await db.query.customFieldValues.findMany({ where: eq(customFieldValues.ticket_id, id), - with: { - customField: true, - }, }); - return c.json({ ...ticket, custom_fields: cfValues }); + const cfIds = [...new Set(cfValues.map(v => v.custom_field_id))]; + const cfRecords = cfIds.length > 0 + ? await db.query.customFields.findMany({ + where: (fields, { inArray }) => inArray(fields.id, cfIds), + }) + : []; + const cfMap = new Map(cfRecords.map(cf => [cf.id, cf])); + const customFieldsMapped = cfValues.map(v => ({ + ...v, + custom_field: cfMap.get(v.custom_field_id) ?? null, + })); + + return c.json({ ...ticket, custom_fields: customFieldsMapped }); }); // PATCH /:id — update ticket diff --git a/src/scrip/engine.ts b/src/scrip/engine.ts index 3690b32..8cc3350 100644 --- a/src/scrip/engine.ts +++ b/src/scrip/engine.ts @@ -46,8 +46,6 @@ export class ScripEngine { return []; } - const transactionTypes = [...new Set(transactions.map((tx) => tx.transaction_type))]; - const allScrips = await this.db.query.scrips.findMany({ orderBy: asc(scrips.sort_order), }); @@ -55,7 +53,6 @@ export class ScripEngine { const matchingScrips = allScrips.filter((scrip) => { if (scrip.disabled) return false; if (scrip.queue_id !== null && scrip.queue_id !== ticketRecord.queue_id) return false; - if (!transactionTypes.includes(scrip.condition_type)) return false; return true; });