From fe9f806c5054c8a5e99f58e4261b08275db9a46f Mon Sep 17 00:00:00 2001 From: lorentz Date: Wed, 18 Mar 2026 00:08:41 -0400 Subject: [PATCH] =?UTF-8?q?feat:=20mobile=20=E2=80=94=20filter=20all=20que?= =?UTF-8?q?ries=20to=20Wulf=20Managed=20companies=20only?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit All mobile dashboard and ticket list queries now INNER JOIN companies on user_defined_fields->>'MSP Service Model' = 'Wulf Managed', scoping all stats (open total, by priority, by queue, SLA, recent activity) and ticket list to managed clients only. 349 open tickets in scope. --- app/api/mobile/dashboard/route.ts | 22 +++++++++++++--------- app/api/mobile/tickets/route.ts | 10 +++++++--- 2 files changed, 20 insertions(+), 12 deletions(-) diff --git a/app/api/mobile/dashboard/route.ts b/app/api/mobile/dashboard/route.ts index 9ae0c69..b893e55 100644 --- a/app/api/mobile/dashboard/route.ts +++ b/app/api/mobile/dashboard/route.ts @@ -6,12 +6,14 @@ export async function GET() { postgresClient.query(` SELECT t.status, COUNT(*) as count FROM tickets t + INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' WHERE t.status != 5 AND t.is_deleted = false GROUP BY t.status ORDER BY count DESC `), postgresClient.query(` SELECT t.queue_id, q.label as queue_label, COUNT(*) as count FROM tickets t + INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' LEFT JOIN queues q ON q.value = t.queue_id WHERE t.status != 5 AND t.is_deleted = false GROUP BY t.queue_id, q.label ORDER BY count DESC LIMIT 8 @@ -19,6 +21,7 @@ export async function GET() { postgresClient.query(` SELECT t.priority, p.label, COUNT(*) as count FROM tickets t + INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' LEFT JOIN priorities p ON p.value = t.priority WHERE t.status != 5 AND t.is_deleted = false GROUP BY t.priority, p.label ORDER BY count DESC @@ -29,7 +32,7 @@ export async function GET() { c.company_name, q.label as queue_label, r.first_name || ' ' || r.last_name as assigned_to FROM tickets t - LEFT JOIN companies c ON c.id = t.company_id + INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' LEFT JOIN queues q ON q.value = t.queue_id LEFT JOIN resources r ON r.id = t.assigned_resource_id WHERE t.status != 5 AND t.is_deleted = false AND t.last_activity_date IS NOT NULL @@ -37,14 +40,15 @@ export async function GET() { `), postgresClient.query(` SELECT - COUNT(*) FILTER (WHERE first_response_date_time IS NOT NULL - AND EXTRACT(EPOCH FROM (first_response_date_time - create_date))/3600 <= 1) as resp_met, - COUNT(*) FILTER (WHERE first_response_date_time IS NOT NULL) as resp_total, - COUNT(*) FILTER (WHERE resolved_date_time IS NOT NULL - AND EXTRACT(EPOCH FROM (resolved_date_time - create_date))/3600 <= 24) as res_met, - COUNT(*) FILTER (WHERE resolved_date_time IS NOT NULL) as res_total - FROM tickets - WHERE create_date >= NOW() - INTERVAL '30 days' AND is_deleted = false + COUNT(*) FILTER (WHERE t.first_response_date_time IS NOT NULL + AND EXTRACT(EPOCH FROM (t.first_response_date_time - t.create_date))/3600 <= 1) as resp_met, + COUNT(*) FILTER (WHERE t.first_response_date_time IS NOT NULL) as resp_total, + COUNT(*) FILTER (WHERE t.resolved_date_time IS NOT NULL + AND EXTRACT(EPOCH FROM (t.resolved_date_time - t.create_date))/3600 <= 24) as res_met, + COUNT(*) FILTER (WHERE t.resolved_date_time IS NOT NULL) as res_total + FROM tickets t + INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' + WHERE t.create_date >= NOW() - INTERVAL '30 days' AND t.is_deleted = false `), ]); diff --git a/app/api/mobile/tickets/route.ts b/app/api/mobile/tickets/route.ts index e7be076..de13f60 100644 --- a/app/api/mobile/tickets/route.ts +++ b/app/api/mobile/tickets/route.ts @@ -10,7 +10,11 @@ export async function GET(request: NextRequest) { const limit = 30; const offset = (page - 1) * limit; - const conditions: string[] = ['t.status != 5', 't.is_deleted = false']; + const conditions: string[] = [ + 't.status != 5', + 't.is_deleted = false', + "c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed'", + ]; const params: unknown[] = []; if (search) { @@ -36,7 +40,7 @@ export async function GET(request: NextRequest) { c.company_name, r.first_name || ' ' || r.last_name as assigned_to FROM tickets t - LEFT JOIN companies c ON c.id = t.company_id + INNER JOIN companies c ON c.id = t.company_id LEFT JOIN queues q ON q.value = t.queue_id LEFT JOIN resources r ON r.id = t.assigned_resource_id WHERE ${where} @@ -46,7 +50,7 @@ export async function GET(request: NextRequest) { postgresClient.query(` SELECT COUNT(*) as total FROM tickets t - LEFT JOIN companies c ON c.id = t.company_id + INNER JOIN companies c ON c.id = t.company_id WHERE ${where} `, params), ]);