All files / services/rrm template-gate.service.ts

100% Statements 6/6
100% Branches 0/0
100% Functions 4/4
100% Lines 4/4

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39                                                  81x               81x 241x   236x    
import { inArray } from "drizzle-orm";
import type { DrizzleD1Database } from "drizzle-orm/d1";
import { RRM_LADDER_TEMPLATE_NAMES } from "../../config/whatsapp-templates";
import * as schema from "../../db/schema";
 
/**
 * The scheduler's enrolment gate (design doc §9): nothing is enrolled while
 * `rrm_partner_opener_v1` is not APPROVED, and the other two ladder steps are
 * gated the same way rather than trusting a partial submission.
 *
 * This lived in `routes/admin/rrm/ladder.routes.ts` and was imported from
 * `services/rrm/scheduler.service.ts` — a service reaching into a route module,
 * which pinned the whole 205 KB admin route tree into the cron and queue paths
 * and blocked the admin group from ever being mounted lazily. Its own docstring
 * said it took a raw `DrizzleD1Database` rather than a `Dal` specifically so the
 * scheduler would not have to import the admin route surface; it just lived in
 * the wrong file to make that true.
 *
 * NOTE this reads `rrm_templates`, not `wa_templates`. The two tables are
 * written by different paths and having approvals in one does not open the gate
 * on the other. See docs/operations/rrm-partner-module-guide.md.
 */
export async function areLadderTemplatesApproved(
	db: DrizzleD1Database<typeof schema>,
): Promise<boolean> {
	const rows = await db
		.select({
			name: schema.rrmTemplates.name,
			status: schema.rrmTemplates.status,
		})
		.from(schema.rrmTemplates)
		.where(inArray(schema.rrmTemplates.name, [...RRM_LADDER_TEMPLATE_NAMES]));
 
	const approved = new Set(
		rows.filter((row) => row.status === "APPROVED").map((row) => row.name),
	);
	return RRM_LADDER_TEMPLATE_NAMES.every((name) => approved.has(name));
}