unit(D8): assign/clear route (schedule.publish guard, rule-1 gate, audit) + active-assignment-in-instance query

This commit is contained in:
Tiel Coder
2026-11-18 18:35:53 +00:00
parent e47ef839a7
commit 71b993e663
2 changed files with 241 additions and 0 deletions
+42
View File
@@ -0,0 +1,42 @@
import { and, eq, sql } from "drizzle-orm";
import { assignments } from "@/db/schema";
/**
* Assignment queries — unit D8.
*
* `activeAssignmentInInstance` returns a volunteer's single active assignment
* within one service instance, or null. It keys on the instance id rather than
* a date range: rule 1 (BUILD-SPEC §7) is "one active assignment per service
* instance", and a volunteer may serve two different instances on the same day
* (e.g. both Sunday services). A date range would wrongly block that.
*/
export interface ActiveAssignment {
id: string;
slotId: string;
userId: string;
status: string;
}
export async function activeAssignmentInInstance(
db: typeof import("@/db").db,
input: { userId: string; instanceId: string },
): Promise<ActiveAssignment | null> {
const rows = await db
.select({
id: assignments.id,
slotId: assignments.slotId,
userId: assignments.userId,
status: assignments.status,
})
.from(assignments)
.where(
and(
eq(assignments.userId, input.userId),
sql`${assignments.slotId} IN (SELECT id FROM slots WHERE service_instance_id = ${input.instanceId})`,
sql`${assignments.status} IN ('proposed','confirmed','completed')`,
),
)
.limit(1);
return rows[0] ?? null;
}