From f3a4d915f5def8e698ba46999ff26aa77d567aab Mon Sep 17 00:00:00 2001 From: saravanakumardb1 Date: Fri, 20 Mar 2026 06:23:34 -0700 Subject: [PATCH] fix(support-cases): prevent crash when auto-triage encounters undefined tags - supportCase.tags is optional in SupportCaseDoc schema - Spreading undefined throws TypeError at runtime - Fixed both [...supportCase.tags] and .includes() call with ?? [] fallback - Added regression test for undefined tags case - 9 support-cases tests passing --- .../src/modules/support-cases/routes.test.ts | 24 +++++++++++++++++++ .../src/modules/support-cases/routes.ts | 4 ++-- 2 files changed, 26 insertions(+), 2 deletions(-) diff --git a/services/platform-service/src/modules/support-cases/routes.test.ts b/services/platform-service/src/modules/support-cases/routes.test.ts index ddba2d94..f3ccc5c3 100644 --- a/services/platform-service/src/modules/support-cases/routes.test.ts +++ b/services/platform-service/src/modules/support-cases/routes.test.ts @@ -225,6 +225,30 @@ describe('supportCaseRoutes', () => { expect(body.tagsAdded).toContain('category:billing'); }); + it('POST /support/cases/:id/auto-triage handles undefined tags gracefully', async () => { + repoMock.getCase.mockResolvedValue({ + id: 'sup_3', + productId: 'lysnrai', + title: 'Slow dashboard loading', + description: 'Pages take 10s to load', + priority: 'low', + // tags intentionally omitted — undefined + }); + repoMock.updateCase.mockImplementation( + async (_id: string, _pid: string, updates: Record) => ({ + ...updates, + id: 'sup_3', + }) + ); + const app = await buildApp({ sub: 'admin_1', productId: 'lysnrai', role: 'admin' }); + + const res = await app.inject({ method: 'POST', url: '/api/support/cases/sup_3/auto-triage' }); + expect(res.statusCode).toBe(200); + const body = res.json(); + expect(body.newPriority).toBe('medium'); + expect(body.tagsAdded).toContain('auto-triaged:medium'); + }); + // ── Case Metrics ─────────────────────────────────────── it('GET /support/metrics returns case metrics with SLA compliance', async () => { diff --git a/services/platform-service/src/modules/support-cases/routes.ts b/services/platform-service/src/modules/support-cases/routes.ts index 4aae81f0..a7f9e647 100644 --- a/services/platform-service/src/modules/support-cases/routes.ts +++ b/services/platform-service/src/modules/support-cases/routes.ts @@ -254,7 +254,7 @@ export async function supportCaseRoutes(app: FastifyInstance) { const text = `${supportCase.title} ${supportCase.description ?? ''}`.toLowerCase(); let suggestedPriority: string = supportCase.priority; - const tags = [...supportCase.tags]; + const tags = [...(supportCase.tags ?? [])]; // Priority heuristics if (text.includes('outage') || text.includes('production down') || text.includes('data loss')) { @@ -293,7 +293,7 @@ export async function supportCaseRoutes(app: FastifyInstance) { triaged: true, previousPriority: supportCase.priority, newPriority: suggestedPriority, - tagsAdded: dedupedTags.filter(t => !supportCase.tags.includes(t)), + tagsAdded: dedupedTags.filter(t => !(supportCase.tags ?? []).includes(t)), case: updated, }; });