All first todos completed

This commit is contained in:
2026-05-01 17:58:14 +02:00
parent 65819900b1
commit 18a4fdd893
29 changed files with 1263 additions and 160 deletions
@@ -0,0 +1,7 @@
ALTER TABLE `global_devices` ADD COLUMN `display_name` text;
--> statement-breakpoint
UPDATE `global_devices` SET `display_name` = `name` WHERE `display_name` IS NULL;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `display_name` text;
--> statement-breakpoint
UPDATE `project_devices` SET `display_name` = `name` WHERE `display_name` IS NULL;
@@ -0,0 +1,3 @@
ALTER TABLE `consumers` ADD COLUMN `project_device_id` text REFERENCES `project_devices`(`id`) ON UPDATE no action ON DELETE set null;
--> statement-breakpoint
ALTER TABLE `consumers` ADD COLUMN `is_linked_to_device` integer NOT NULL DEFAULT 0;
+14
View File
@@ -43,6 +43,20 @@
"when": 1777597000000,
"tag": "0005_project_devices",
"breakpoints": true
},
{
"idx": 6,
"version": "6",
"when": 1777652000000,
"tag": "0006_device_display_name",
"breakpoints": true
},
{
"idx": 7,
"version": "6",
"when": 1777680000000,
"tag": "0007_consumer_device_link",
"breakpoints": true
}
]
}
+64 -10
View File
@@ -1,5 +1,5 @@
import crypto from "node:crypto";
import { eq } from "drizzle-orm";
import { and, eq } from "drizzle-orm";
import { db } from "../client.js";
import { consumers } from "../schema/consumers.js";
import type {
@@ -14,15 +14,21 @@ export class ConsumerRepository {
async create(input: CreateConsumerInput) {
const id = crypto.randomUUID();
const normalizedName = input.name?.trim() || "Unbenannter Eintrag";
const normalizedQuantity = input.quantity ?? 0;
const normalizedInstalledPowerPerUnitKw = input.installedPowerPerUnitKw ?? 0;
const normalizedDemandFactor = input.demandFactor ?? 1;
await db.insert(consumers).values({
id,
projectId: input.projectId,
distributionBoardId: input.distributionBoardId ?? null,
circuitListId: input.circuitListId ?? null,
projectDeviceId: input.projectDeviceId ?? null,
isLinkedToDevice: input.isLinkedToDevice ? 1 : 0,
roomId: input.roomId ?? null,
circuitNumber: input.circuitNumber ?? null,
description: input.description ?? null,
name: input.name,
name: normalizedName,
category: input.category ?? null,
deviceType: input.deviceType ?? null,
phaseType: input.phaseType ?? null,
@@ -34,28 +40,41 @@ export class ConsumerRepository {
cableType: input.cableType ?? null,
cableCrossSection: input.cableCrossSection ?? null,
comment: input.comment ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
quantity: normalizedQuantity,
installedPowerPerUnitKw: normalizedInstalledPowerPerUnitKw,
demandFactor: normalizedDemandFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
note: input.note ?? null,
});
return { id, ...input };
return {
id,
...input,
name: normalizedName,
quantity: normalizedQuantity,
installedPowerPerUnitKw: normalizedInstalledPowerPerUnitKw,
demandFactor: normalizedDemandFactor,
};
}
async update(consumerId: string, input: UpdateConsumerInput) {
const normalizedName = input.name?.trim() || "Unbenannter Eintrag";
const normalizedQuantity = input.quantity ?? 0;
const normalizedInstalledPowerPerUnitKw = input.installedPowerPerUnitKw ?? 0;
const normalizedDemandFactor = input.demandFactor ?? 1;
await db
.update(consumers)
.set({
projectId: input.projectId,
distributionBoardId: input.distributionBoardId ?? null,
circuitListId: input.circuitListId ?? null,
projectDeviceId: input.projectDeviceId ?? null,
isLinkedToDevice: input.isLinkedToDevice ? 1 : 0,
roomId: input.roomId ?? null,
circuitNumber: input.circuitNumber ?? null,
description: input.description ?? null,
name: input.name,
name: normalizedName,
category: input.category ?? null,
deviceType: input.deviceType ?? null,
phaseType: input.phaseType ?? null,
@@ -67,9 +86,9 @@ export class ConsumerRepository {
cableType: input.cableType ?? null,
cableCrossSection: input.cableCrossSection ?? null,
comment: input.comment ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
quantity: normalizedQuantity,
installedPowerPerUnitKw: normalizedInstalledPowerPerUnitKw,
demandFactor: normalizedDemandFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
@@ -86,4 +105,39 @@ export class ConsumerRepository {
async delete(consumerId: string) {
await db.delete(consumers).where(eq(consumers.id, consumerId));
}
async syncLinkedConsumersFromProjectDevice(
projectId: string,
projectDeviceId: string,
data: {
displayName: string;
category?: string;
quantity: number;
installedPowerPerUnitKw: number;
demandFactor: number;
phaseCount?: 1 | 3;
powerFactor?: number;
note?: string;
}
) {
await db
.update(consumers)
.set({
name: data.displayName,
category: data.category ?? null,
quantity: data.quantity,
installedPowerPerUnitKw: data.installedPowerPerUnitKw,
demandFactor: data.demandFactor,
phaseCount: data.phaseCount ?? null,
powerFactor: data.powerFactor ?? null,
note: data.note ?? null,
})
.where(
and(
eq(consumers.projectId, projectId),
eq(consumers.projectDeviceId, projectDeviceId),
eq(consumers.isLinkedToDevice, 1)
)
);
}
}
@@ -17,6 +17,7 @@ export class GlobalDeviceRepository {
await db.insert(globalDevices).values({
id,
name: input.name,
displayName: input.displayName,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
@@ -34,6 +35,7 @@ export class GlobalDeviceRepository {
.update(globalDevices)
.set({
name: input.name,
displayName: input.displayName,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
@@ -18,6 +18,7 @@ export class ProjectDeviceRepository {
id,
projectId,
name: input.name,
displayName: input.displayName,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
@@ -46,6 +47,7 @@ export class ProjectDeviceRepository {
.update(projectDevices)
.set({
name: input.name,
displayName: input.displayName,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
+5
View File
@@ -1,6 +1,7 @@
import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
import { circuitLists } from "./circuit-lists.js";
import { distributionBoards } from "./distribution-boards.js";
import { projectDevices } from "./project-devices.js";
import { projects } from "./projects.js";
import { rooms } from "./rooms.js";
@@ -15,6 +16,10 @@ export const consumers = sqliteTable("consumers", {
circuitListId: text("circuit_list_id").references(() => circuitLists.id, {
onDelete: "set null",
}),
projectDeviceId: text("project_device_id").references(() => projectDevices.id, {
onDelete: "set null",
}),
isLinkedToDevice: integer("is_linked_to_device").notNull().default(0),
roomId: text("room_id").references(() => rooms.id, {
onDelete: "set null",
}),
+1
View File
@@ -3,6 +3,7 @@ import { integer, real, sqliteTable, text } from "drizzle-orm/sqlite-core";
export const globalDevices = sqliteTable("global_devices", {
id: text("id").primaryKey(),
name: text("name").notNull(),
displayName: text("display_name").notNull(),
category: text("category"),
quantity: integer("quantity").notNull(),
installedPowerPerUnitKw: real("installed_power_per_unit_kw").notNull(),
+1
View File
@@ -7,6 +7,7 @@ export const projectDevices = sqliteTable("project_devices", {
.notNull()
.references(() => projects.id, { onDelete: "cascade" }),
name: text("name").notNull(),
displayName: text("display_name").notNull(),
category: text("category"),
quantity: integer("quantity").notNull(),
installedPowerPerUnitKw: real("installed_power_per_unit_kw").notNull(),