fix: scrip engine condition matching and ticket GET endpoint
- 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).
This commit is contained in:
@@ -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
|
||||
|
||||
@@ -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;
|
||||
});
|
||||
|
||||
|
||||
Reference in New Issue
Block a user