diff --git a/app/api/mobile/dashboard/route.ts b/app/api/mobile/dashboard/route.ts index b893e55..e3e6317 100644 --- a/app/api/mobile/dashboard/route.ts +++ b/app/api/mobile/dashboard/route.ts @@ -1,19 +1,30 @@ import { NextResponse } from 'next/server'; import { postgresClient } from '@/lib/services/postgres-client'; +async function getManagedClassificationFilter(): Promise { + const result = await postgresClient.query( + `SELECT setting_value FROM kiosk_settings WHERE setting_key = 'included_classifications'` + ); + const value = result.rows[0]?.setting_value || ''; + const ids = value ? value.split(',').map((s: string) => parseInt(s.trim(), 10)).filter((n: number) => !isNaN(n)) : []; + return ids.length > 0 ? `c.classification::integer IN (${ids.join(',')})` : 'true'; +} + export async function GET() { + const classFilter = await getManagedClassificationFilter(); + const [byStatus, byQueue, byPriority, recentActivity, sla] = await Promise.all([ 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' + INNER JOIN companies c ON c.id = t.company_id AND ${classFilter} 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' + INNER JOIN companies c ON c.id = t.company_id AND ${classFilter} 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 @@ -21,7 +32,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' + INNER JOIN companies c ON c.id = t.company_id AND ${classFilter} 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 @@ -32,7 +43,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 - INNER JOIN companies c ON c.id = t.company_id AND c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed' + INNER JOIN companies c ON c.id = t.company_id AND ${classFilter} 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 @@ -47,7 +58,7 @@ export async function GET() { 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' + INNER JOIN companies c ON c.id = t.company_id AND ${classFilter} 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 de13f60..74f6dd9 100644 --- a/app/api/mobile/tickets/route.ts +++ b/app/api/mobile/tickets/route.ts @@ -1,6 +1,15 @@ import { NextRequest, NextResponse } from 'next/server'; import { postgresClient } from '@/lib/services/postgres-client'; +async function getManagedCompanyFilter(): Promise { + const result = await postgresClient.query( + `SELECT setting_value FROM kiosk_settings WHERE setting_key = 'included_classifications'` + ); + const value = result.rows[0]?.setting_value || ''; + const ids = value ? value.split(',').map((s: string) => parseInt(s.trim(), 10)).filter((n: number) => !isNaN(n)) : []; + return ids.length > 0 ? `c.classification::integer IN (${ids.join(',')})` : 'true'; +} + export async function GET(request: NextRequest) { const { searchParams } = request.nextUrl; const search = searchParams.get('q') ?? ''; @@ -10,10 +19,12 @@ export async function GET(request: NextRequest) { const limit = 30; const offset = (page - 1) * limit; + const managedFilter = await getManagedCompanyFilter(); + const conditions: string[] = [ 't.status != 5', 't.is_deleted = false', - "c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed'", + managedFilter, ]; const params: unknown[] = [];