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
This commit is contained in:
saravanakumardb1 2026-03-20 06:23:34 -07:00
parent 63322a2d07
commit f3a4d915f5
2 changed files with 26 additions and 2 deletions

View File

@ -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<string, unknown>) => ({
...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 () => {

View File

@ -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,
};
});