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,99 @@
CREATE TABLE `circuit_sections` (
`id` text PRIMARY KEY NOT NULL,
`circuit_list_id` text NOT NULL,
`key` text NOT NULL,
`display_name` text NOT NULL,
`prefix` text NOT NULL,
`sort_order` integer NOT NULL DEFAULT 0,
FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_sections_list_key_unique` ON `circuit_sections` (`circuit_list_id`,`key`);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuit_sections_list_prefix_unique` ON `circuit_sections` (`circuit_list_id`,`prefix`);
--> statement-breakpoint
CREATE TABLE `circuits` (
`id` text PRIMARY KEY NOT NULL,
`circuit_list_id` text NOT NULL,
`section_id` text NOT NULL,
`equipment_identifier` text NOT NULL,
`display_name` text,
`sort_order` integer NOT NULL DEFAULT 0,
`protection_type` text,
`protection_rated_current` real,
`protection_characteristic` text,
`cable_type` text,
`cable_cross_section` text,
`cable_length` real,
`rcd_assignment` text,
`terminal_designation` text,
`voltage` real,
`status` text,
`is_reserve` integer NOT NULL DEFAULT 0,
`remark` text,
FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`section_id`) REFERENCES `circuit_sections`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `circuits_list_equipment_identifier_unique` ON `circuits` (`circuit_list_id`,`equipment_identifier`);
--> statement-breakpoint
CREATE TABLE `circuit_device_rows` (
`id` text PRIMARY KEY NOT NULL,
`circuit_id` text NOT NULL,
`linked_project_device_id` text,
`legacy_consumer_id` text,
`sort_order` integer NOT NULL DEFAULT 0,
`name` text NOT NULL,
`display_name` text NOT NULL,
`phase_type` text,
`connection_kind` text,
`cost_group` text,
`category` text,
`level` text,
`room_id` text,
`room_number_snapshot` text,
`room_name_snapshot` text,
`quantity` integer NOT NULL,
`power_per_unit` real NOT NULL,
`simultaneity_factor` real NOT NULL,
`cos_phi` real,
`remark` text,
`overridden_fields` text,
FOREIGN KEY (`circuit_id`) REFERENCES `circuits`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`linked_project_device_id`) REFERENCES `project_devices`(`id`) ON UPDATE no action ON DELETE set null,
FOREIGN KEY (`room_id`) REFERENCES `rooms`(`id`) ON UPDATE no action ON DELETE set null
);
--> statement-breakpoint
CREATE TABLE `legacy_consumer_circuit_migrations` (
`consumer_id` text PRIMARY KEY NOT NULL,
`circuit_id` text NOT NULL,
`circuit_device_row_id` text NOT NULL,
`circuit_list_id` text NOT NULL,
`created_at_iso` text NOT NULL,
FOREIGN KEY (`circuit_id`) REFERENCES `circuits`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`circuit_device_row_id`) REFERENCES `circuit_device_rows`(`id`) ON UPDATE no action ON DELETE cascade,
FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE TABLE `legacy_consumer_migration_reports` (
`id` text PRIMARY KEY NOT NULL,
`circuit_list_id` text NOT NULL,
`legacy_consumer_count` integer NOT NULL,
`created_circuit_count` integer NOT NULL,
`created_device_row_count` integer NOT NULL,
`duplicate_grouped_count` integer NOT NULL,
`generated_identifier_count` integer NOT NULL,
`unassigned_row_count` integer NOT NULL,
`warnings_json` text NOT NULL,
`generated_identifiers_json` text NOT NULL,
`duplicate_groups_json` text NOT NULL,
`created_at_iso` text NOT NULL,
FOREIGN KEY (`circuit_list_id`) REFERENCES `circuit_lists`(`id`) ON UPDATE no action ON DELETE cascade
);
--> statement-breakpoint
CREATE UNIQUE INDEX `legacy_consumer_migration_reports_list_unique` ON `legacy_consumer_migration_reports` (`circuit_list_id`);
@@ -0,0 +1,61 @@
import crypto from "node:crypto";
import { asc, inArray } from "drizzle-orm";
import { db } from "../client.js";
import { circuitDeviceRows } from "../schema/circuit-device-rows.js";
export class CircuitDeviceRowRepository {
async listByCircuitList(circuitIds: string[]) {
if (!circuitIds.length) {
return [];
}
return db
.select()
.from(circuitDeviceRows)
.where(inArray(circuitDeviceRows.circuitId, circuitIds))
.orderBy(asc(circuitDeviceRows.sortOrder));
}
async create(input: {
circuitId: string;
linkedProjectDeviceId?: string;
legacyConsumerId?: string;
sortOrder: number;
name: string;
displayName: string;
phaseType?: string;
connectionKind?: string;
costGroup?: string;
category?: string;
roomId?: string;
roomNumberSnapshot?: string;
roomNameSnapshot?: string;
quantity: number;
powerPerUnit: number;
simultaneityFactor: number;
cosPhi?: number;
remark?: string;
}) {
await db.insert(circuitDeviceRows).values({
id: crypto.randomUUID(),
circuitId: input.circuitId,
linkedProjectDeviceId: input.linkedProjectDeviceId ?? null,
legacyConsumerId: input.legacyConsumerId ?? null,
sortOrder: input.sortOrder,
name: input.name,
displayName: input.displayName,
phaseType: input.phaseType ?? null,
connectionKind: input.connectionKind ?? null,
costGroup: input.costGroup ?? null,
category: input.category ?? null,
roomId: input.roomId ?? null,
roomNumberSnapshot: input.roomNumberSnapshot ?? null,
roomNameSnapshot: input.roomNameSnapshot ?? null,
quantity: input.quantity,
powerPerUnit: input.powerPerUnit,
simultaneityFactor: input.simultaneityFactor,
cosPhi: input.cosPhi ?? null,
remark: input.remark ?? null,
overriddenFields: null,
});
}
}
@@ -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,
});
}
}
}
+50
View File
@@ -0,0 +1,50 @@
import crypto from "node:crypto";
import { asc, eq } from "drizzle-orm";
import { db } from "../client.js";
import { circuits } from "../schema/circuits.js";
export class CircuitRepository {
async listByCircuitList(circuitListId: string) {
return db
.select()
.from(circuits)
.where(eq(circuits.circuitListId, circuitListId))
.orderBy(asc(circuits.sortOrder), asc(circuits.equipmentIdentifier));
}
async create(input: {
circuitListId: string;
sectionId: string;
equipmentIdentifier: string;
displayName?: string;
sortOrder: number;
protectionType?: string;
protectionRatedCurrent?: number;
protectionCharacteristic?: string;
cableType?: string;
cableCrossSection?: string;
cableLength?: number;
voltage?: number;
remark?: string;
}) {
const id = crypto.randomUUID();
await db.insert(circuits).values({
id,
circuitListId: input.circuitListId,
sectionId: input.sectionId,
equipmentIdentifier: input.equipmentIdentifier,
displayName: input.displayName ?? null,
sortOrder: input.sortOrder,
protectionType: input.protectionType ?? null,
protectionRatedCurrent: input.protectionRatedCurrent ?? null,
protectionCharacteristic: input.protectionCharacteristic ?? null,
cableType: input.cableType ?? null,
cableCrossSection: input.cableCrossSection ?? null,
cableLength: input.cableLength ?? null,
voltage: input.voltage ?? null,
remark: input.remark ?? null,
});
return id;
}
}
@@ -12,6 +12,10 @@ export class ConsumerRepository {
return db.select().from(consumers).where(eq(consumers.projectId, projectId));
}
async listByCircuitList(circuitListId: string) {
return db.select().from(consumers).where(eq(consumers.circuitListId, circuitListId));
}
async create(input: CreateConsumerInput) {
const id = crypto.randomUUID();
const normalizedName = input.name?.trim() || "Unbenannter Eintrag";
+35
View File
@@ -0,0 +1,35 @@
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { circuits } from "./circuits.js";
import { projectDevices } from "./project-devices.js";
import { rooms } from "./rooms.js";
export const circuitDeviceRows = sqliteTable("circuit_device_rows", {
id: text("id").primaryKey(),
circuitId: text("circuit_id")
.notNull()
.references(() => circuits.id, { onDelete: "cascade" }),
linkedProjectDeviceId: text("linked_project_device_id").references(() => projectDevices.id, {
onDelete: "set null",
}),
legacyConsumerId: text("legacy_consumer_id"),
sortOrder: integer("sort_order").notNull().default(0),
name: text("name").notNull(),
displayName: text("display_name").notNull(),
phaseType: text("phase_type"),
connectionKind: text("connection_kind"),
costGroup: text("cost_group"),
category: text("category"),
level: text("level"),
roomId: text("room_id").references(() => rooms.id, {
onDelete: "set null",
}),
roomNumberSnapshot: text("room_number_snapshot"),
roomNameSnapshot: text("room_name_snapshot"),
quantity: integer("quantity").notNull(),
powerPerUnit: real("power_per_unit").notNull(),
simultaneityFactor: real("simultaneity_factor").notNull(),
cosPhi: real("cos_phi"),
remark: text("remark"),
overriddenFields: text("overridden_fields"),
});
+21
View File
@@ -0,0 +1,21 @@
import { integer, sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
import { circuitLists } from "./circuit-lists.js";
export const circuitSections = sqliteTable(
"circuit_sections",
{
id: text("id").primaryKey(),
circuitListId: text("circuit_list_id")
.notNull()
.references(() => circuitLists.id, { onDelete: "cascade" }),
key: text("key").notNull(),
displayName: text("display_name").notNull(),
prefix: text("prefix").notNull(),
sortOrder: integer("sort_order").notNull().default(0),
},
(table) => [
unique("circuit_sections_list_key_unique").on(table.circuitListId, table.key),
unique("circuit_sections_list_prefix_unique").on(table.circuitListId, table.prefix),
]
);
+33
View File
@@ -0,0 +1,33 @@
import { integer, real, sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
import { circuitLists } from "./circuit-lists.js";
import { circuitSections } from "./circuit-sections.js";
export const circuits = sqliteTable(
"circuits",
{
id: text("id").primaryKey(),
circuitListId: text("circuit_list_id")
.notNull()
.references(() => circuitLists.id, { onDelete: "cascade" }),
sectionId: text("section_id")
.notNull()
.references(() => circuitSections.id, { onDelete: "cascade" }),
equipmentIdentifier: text("equipment_identifier").notNull(),
displayName: text("display_name"),
sortOrder: integer("sort_order").notNull().default(0),
protectionType: text("protection_type"),
protectionRatedCurrent: real("protection_rated_current"),
protectionCharacteristic: text("protection_characteristic"),
cableType: text("cable_type"),
cableCrossSection: text("cable_cross_section"),
cableLength: real("cable_length"),
rcdAssignment: text("rcd_assignment"),
terminalDesignation: text("terminal_designation"),
voltage: real("voltage"),
status: text("status"),
isReserve: integer("is_reserve").notNull().default(0),
remark: text("remark"),
},
(table) => [unique("circuits_list_equipment_identifier_unique").on(table.circuitListId, table.equipmentIdentifier)]
);
@@ -0,0 +1,19 @@
import { sqliteTable, text } from "drizzle-orm/sqlite-core";
import { circuitDeviceRows } from "./circuit-device-rows.js";
import { circuitLists } from "./circuit-lists.js";
import { circuits } from "./circuits.js";
export const legacyConsumerCircuitMigrations = sqliteTable("legacy_consumer_circuit_migrations", {
consumerId: text("consumer_id").primaryKey(),
circuitId: text("circuit_id")
.notNull()
.references(() => circuits.id, { onDelete: "cascade" }),
circuitDeviceRowId: text("circuit_device_row_id")
.notNull()
.references(() => circuitDeviceRows.id, { onDelete: "cascade" }),
circuitListId: text("circuit_list_id")
.notNull()
.references(() => circuitLists.id, { onDelete: "cascade" }),
createdAtIso: text("created_at_iso").notNull(),
});
@@ -0,0 +1,24 @@
import { integer, sqliteTable, text, unique } from "drizzle-orm/sqlite-core";
import { circuitLists } from "./circuit-lists.js";
export const legacyConsumerMigrationReports = sqliteTable(
"legacy_consumer_migration_reports",
{
id: text("id").primaryKey(),
circuitListId: text("circuit_list_id")
.notNull()
.references(() => circuitLists.id, { onDelete: "cascade" }),
legacyConsumerCount: integer("legacy_consumer_count").notNull(),
createdCircuitCount: integer("created_circuit_count").notNull(),
createdDeviceRowCount: integer("created_device_row_count").notNull(),
duplicateGroupedCount: integer("duplicate_grouped_count").notNull(),
generatedIdentifierCount: integer("generated_identifier_count").notNull(),
unassignedRowCount: integer("unassigned_row_count").notNull(),
warningsJson: text("warnings_json").notNull(),
generatedIdentifiersJson: text("generated_identifiers_json").notNull(),
duplicateGroupsJson: text("duplicate_groups_json").notNull(),
createdAtIso: text("created_at_iso").notNull(),
},
(table) => [unique("legacy_consumer_migration_reports_list_unique").on(table.circuitListId)]
);