push to maple-street
This commit is contained in:
@@ -0,0 +1,158 @@
|
||||
/**
|
||||
* Manager dashboard read model. One snapshot for the next five weeks plus
|
||||
* operational queues that otherwise live in separate admin panels.
|
||||
*/
|
||||
|
||||
import { and, eq, gte, isNull, lte, notExists, sql } from "drizzle-orm";
|
||||
import { db } from "@/db";
|
||||
import {
|
||||
assignments,
|
||||
eventPhotographers,
|
||||
events,
|
||||
notifications,
|
||||
serviceInstances,
|
||||
slots,
|
||||
users,
|
||||
} from "@/db/schema";
|
||||
import { calendarInstances, type CalendarInstance } from "@/db/calendar";
|
||||
import { addDaysISO, dateInTimezone, CHURCH_TIMEZONE } from "@/lib/tz";
|
||||
|
||||
export interface DashboardSnapshot {
|
||||
today: string;
|
||||
horizonEnd: string;
|
||||
instances: CalendarInstance[];
|
||||
nextService: CalendarInstance | null;
|
||||
seats: number;
|
||||
filled: number;
|
||||
open: number;
|
||||
proposed: number;
|
||||
confirmed: number;
|
||||
pendingApprovals: number;
|
||||
activePeople: number;
|
||||
upcomingEvents: number;
|
||||
eventsNeedingPhotographer: number;
|
||||
failedNotifications: number;
|
||||
unconfirmedNext: number;
|
||||
}
|
||||
|
||||
export async function dashboardSnapshot(): Promise<DashboardSnapshot> {
|
||||
const today = dateInTimezone(new Date(), CHURCH_TIMEZONE);
|
||||
const horizonEnd = addDaysISO(today, 34);
|
||||
const instances = await calendarInstances(today, horizonEnd);
|
||||
const nextService = instances[0] ?? null;
|
||||
|
||||
const start = new Date(`${today}T00:00:00Z`);
|
||||
const end = new Date(`${horizonEnd}T23:59:59Z`);
|
||||
|
||||
const [proposedRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(assignments)
|
||||
.innerJoin(slots, eq(slots.id, assignments.slotId))
|
||||
.innerJoin(serviceInstances, eq(serviceInstances.id, slots.serviceInstanceId))
|
||||
.where(
|
||||
and(
|
||||
eq(assignments.status, "proposed"),
|
||||
gte(serviceInstances.startsAt, start),
|
||||
lte(serviceInstances.startsAt, end),
|
||||
),
|
||||
);
|
||||
|
||||
const [confirmedRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(assignments)
|
||||
.innerJoin(slots, eq(slots.id, assignments.slotId))
|
||||
.innerJoin(serviceInstances, eq(serviceInstances.id, slots.serviceInstanceId))
|
||||
.where(
|
||||
and(
|
||||
eq(assignments.status, "confirmed"),
|
||||
gte(serviceInstances.startsAt, start),
|
||||
lte(serviceInstances.startsAt, end),
|
||||
),
|
||||
);
|
||||
|
||||
const [pendingRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(users)
|
||||
.where(eq(users.status, "pending_approval"));
|
||||
|
||||
const [activeRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(users)
|
||||
.where(and(eq(users.status, "active"), isNull(users.deactivatedAt)));
|
||||
|
||||
const [eventsRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(events)
|
||||
.where(gte(events.startsAt, new Date()));
|
||||
|
||||
const [eventsMissingPhotoRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(events)
|
||||
.where(
|
||||
and(
|
||||
gte(events.startsAt, new Date()),
|
||||
notExists(
|
||||
db
|
||||
.select({ one: sql`1` })
|
||||
.from(eventPhotographers)
|
||||
.where(
|
||||
and(
|
||||
eq(eventPhotographers.eventId, events.id),
|
||||
eq(eventPhotographers.role, "photographer"),
|
||||
),
|
||||
),
|
||||
),
|
||||
),
|
||||
);
|
||||
|
||||
const [failedRow] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(notifications)
|
||||
.where(eq(notifications.status, "failed"));
|
||||
|
||||
let unconfirmedNext = 0;
|
||||
if (nextService) {
|
||||
const [row] = await db
|
||||
.select({ count: sql<number>`count(*)` })
|
||||
.from(assignments)
|
||||
.innerJoin(slots, eq(slots.id, assignments.slotId))
|
||||
.where(
|
||||
and(
|
||||
eq(slots.serviceInstanceId, nextService.id),
|
||||
eq(assignments.status, "proposed"),
|
||||
),
|
||||
);
|
||||
unconfirmedNext = Number(row?.count ?? 0);
|
||||
}
|
||||
|
||||
const seats = instances.reduce((sum, row) => sum + row.seats, 0);
|
||||
const filled = instances.reduce((sum, row) => sum + row.filled, 0);
|
||||
|
||||
return {
|
||||
today,
|
||||
horizonEnd,
|
||||
instances,
|
||||
nextService,
|
||||
seats,
|
||||
filled,
|
||||
open: Math.max(0, seats - filled),
|
||||
proposed: Number(proposedRow?.count ?? 0),
|
||||
confirmed: Number(confirmedRow?.count ?? 0),
|
||||
pendingApprovals: Number(pendingRow?.count ?? 0),
|
||||
activePeople: Number(activeRow?.count ?? 0),
|
||||
upcomingEvents: Number(eventsRow?.count ?? 0),
|
||||
eventsNeedingPhotographer: Number(eventsMissingPhotoRow?.count ?? 0),
|
||||
failedNotifications: Number(failedRow?.count ?? 0),
|
||||
unconfirmedNext,
|
||||
};
|
||||
}
|
||||
|
||||
/** Upcoming instances with an open seat, for the "needs attention" list. */
|
||||
export async function coverageGaps(limit = 6): Promise<CalendarInstance[]> {
|
||||
const today = dateInTimezone(new Date(), CHURCH_TIMEZONE);
|
||||
const rows = await calendarInstances(today, addDaysISO(today, 34));
|
||||
return rows
|
||||
.filter((row) => row.seats > row.filled)
|
||||
.slice(0, limit);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user