fix: correct access dashboard status counts
This commit is contained in:
@@ -55,6 +55,7 @@ interface AccessRow {
|
||||
host_name: string;
|
||||
visit_from: string | Date;
|
||||
visit_to: string | Date;
|
||||
status: string;
|
||||
check_in_at: string | Date | null;
|
||||
check_out_at: string | Date | null;
|
||||
}
|
||||
@@ -745,10 +746,13 @@ export function createBusinessRouter(deps: BusinessRouterDeps = { query }): Rout
|
||||
`
|
||||
SELECT
|
||||
vr.id AS visit_request_id, v.name AS visitor_name, v.company, vr.zone_name, h.full_name AS host_name,
|
||||
vr.visit_from, vr.visit_to,
|
||||
vr.visit_from, vr.visit_to, vr.status,
|
||||
min(ae.event_at) FILTER (WHERE ae.direction = 'IN') AS check_in_at,
|
||||
max(ae.event_at) FILTER (WHERE ae.direction = 'OUT') AS check_out_at,
|
||||
(max(ae.event_at) FILTER (WHERE ae.direction = 'IN')) > COALESCE(max(ae.event_at) FILTER (WHERE ae.direction = 'OUT'), 'epoch') AS inside
|
||||
COALESCE(
|
||||
(max(ae.event_at) FILTER (WHERE ae.direction = 'IN')) > COALESCE(max(ae.event_at) FILTER (WHERE ae.direction = 'OUT'), 'epoch'),
|
||||
false
|
||||
) AS inside
|
||||
FROM visit_requests vr
|
||||
JOIN visitors v ON v.id = vr.visitor_id
|
||||
JOIN users h ON h.id = vr.host_id
|
||||
@@ -765,9 +769,10 @@ export function createBusinessRouter(deps: BusinessRouterDeps = { query }): Rout
|
||||
zoneName: row.zone_name ?? undefined,
|
||||
visitFrom: toIso(row.visit_from)!,
|
||||
visitTo: toIso(row.visit_to)!,
|
||||
status: row.status,
|
||||
checkInAt: toIso(row.check_in_at),
|
||||
checkOutAt: toIso(row.check_out_at),
|
||||
inside: row.inside,
|
||||
inside: Boolean(row.inside),
|
||||
})));
|
||||
}));
|
||||
|
||||
@@ -776,8 +781,14 @@ export function createBusinessRouter(deps: BusinessRouterDeps = { query }): Rout
|
||||
const result = await dbQuery<{ today_visits: string; pending: string; approved: string; total: string }>(
|
||||
`
|
||||
SELECT
|
||||
count(*) FILTER (WHERE visit_from::date = now()::date) AS today_visits,
|
||||
count(*) FILTER (WHERE status = 'PENDING') AS pending,
|
||||
count(*) FILTER (
|
||||
WHERE visit_from::date = now()::date
|
||||
AND status IN ('PENDING', 'APPROVED')
|
||||
) AS today_visits,
|
||||
count(*) FILTER (
|
||||
WHERE status = 'PENDING'
|
||||
AND visit_to >= now()
|
||||
) AS pending,
|
||||
count(*) FILTER (WHERE status = 'APPROVED') AS approved,
|
||||
count(*) AS total
|
||||
FROM visit_requests
|
||||
|
||||
Reference in New Issue
Block a user