Phase 1A done

This commit is contained in:
2026-05-03 21:16:52 +02:00
parent 49190c5d7e
commit b8995b3a1b
21 changed files with 1038 additions and 3 deletions
@@ -0,0 +1,42 @@
import crypto from "node:crypto";
import { and, asc, eq } from "drizzle-orm";
import { db } from "../client.js";
import { circuitSections } from "../schema/circuit-sections.js";
export class CircuitSectionRepository {
async listByCircuitList(circuitListId: string) {
return db
.select()
.from(circuitSections)
.where(eq(circuitSections.circuitListId, circuitListId))
.orderBy(asc(circuitSections.sortOrder));
}
async createDefaults(circuitListId: string) {
const defaults = [
{ key: "lighting", displayName: "Lighting", prefix: "-1F", sortOrder: 10 },
{ key: "single_phase", displayName: "Single-phase circuits", prefix: "-2F", sortOrder: 20 },
{ key: "three_phase", displayName: "Three-phase circuits", prefix: "-3F", sortOrder: 30 },
{ key: "unassigned", displayName: "Unassigned", prefix: "-UF", sortOrder: 90 },
];
for (const entry of defaults) {
const existing = await db
.select({ id: circuitSections.id })
.from(circuitSections)
.where(
and(eq(circuitSections.circuitListId, circuitListId), eq(circuitSections.key, entry.key))
)
.limit(1);
if (existing.length) {
continue;
}
await db.insert(circuitSections).values({
id: crypto.randomUUID(),
circuitListId,
...entry,
});
}
}
}