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.
64 lines
2.1 KiB
TypeScript
64 lines
2.1 KiB
TypeScript
import { NextRequest, NextResponse } from 'next/server';
|
|
import { postgresClient } from '@/lib/services/postgres-client';
|
|
|
|
export async function GET(request: NextRequest) {
|
|
const { searchParams } = request.nextUrl;
|
|
const search = searchParams.get('q') ?? '';
|
|
const queue = searchParams.get('queue') ?? '';
|
|
const priority = searchParams.get('priority') ?? '';
|
|
const page = Math.max(1, parseInt(searchParams.get('page') ?? '1'));
|
|
const limit = 30;
|
|
const offset = (page - 1) * limit;
|
|
|
|
const conditions: string[] = [
|
|
't.status != 5',
|
|
't.is_deleted = false',
|
|
"c.user_defined_fields->>'MSP Service Model' = 'Wulf Managed'",
|
|
];
|
|
const params: unknown[] = [];
|
|
|
|
if (search) {
|
|
params.push(`%${search}%`);
|
|
conditions.push(`(t.title ILIKE $${params.length} OR t.ticket_number ILIKE $${params.length} OR c.company_name ILIKE $${params.length})`);
|
|
}
|
|
if (queue) {
|
|
params.push(parseInt(queue));
|
|
conditions.push(`t.queue_id = $${params.length}`);
|
|
}
|
|
if (priority) {
|
|
params.push(parseInt(priority));
|
|
conditions.push(`t.priority = $${params.length}`);
|
|
}
|
|
|
|
const where = conditions.join(' AND ');
|
|
|
|
const [rows, countRow] = await Promise.all([
|
|
postgresClient.query(`
|
|
SELECT t.id, t.ticket_number, t.title, t.status, t.priority,
|
|
t.create_date, t.last_activity_date, t.due_date_time,
|
|
t.queue_id, q.label as queue_label,
|
|
c.company_name,
|
|
r.first_name || ' ' || r.last_name as assigned_to
|
|
FROM tickets t
|
|
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}
|
|
ORDER BY t.last_activity_date DESC NULLS LAST
|
|
LIMIT ${limit} OFFSET ${offset}
|
|
`, params),
|
|
postgresClient.query(`
|
|
SELECT COUNT(*) as total
|
|
FROM tickets t
|
|
INNER JOIN companies c ON c.id = t.company_id
|
|
WHERE ${where}
|
|
`, params),
|
|
]);
|
|
|
|
return NextResponse.json({
|
|
tickets: rows.rows,
|
|
total: parseInt(countRow.rows[0]?.total ?? '0'),
|
|
page,
|
|
limit,
|
|
});
|
|
}
|