Align project devices with circuit model

This commit is contained in:
2026-07-22 19:31:08 +02:00
parent 689cfd3c65
commit 9d07ed9856
16 changed files with 301 additions and 106 deletions
@@ -0,0 +1,24 @@
ALTER TABLE `project_devices` ADD COLUMN `phase_type` text NOT NULL DEFAULT 'single_phase';
--> statement-breakpoint
UPDATE `project_devices`
SET `phase_type` = CASE WHEN `phase_count` = 3 THEN 'three_phase' ELSE 'single_phase' END;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `connection_kind` text;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `cost_group` text;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `power_per_unit` real NOT NULL DEFAULT 0;
--> statement-breakpoint
UPDATE `project_devices` SET `power_per_unit` = `installed_power_per_unit_kw`;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `simultaneity_factor` real NOT NULL DEFAULT 1;
--> statement-breakpoint
UPDATE `project_devices` SET `simultaneity_factor` = `demand_factor`;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `cos_phi` real;
--> statement-breakpoint
UPDATE `project_devices` SET `cos_phi` = `power_factor`;
--> statement-breakpoint
ALTER TABLE `project_devices` ADD COLUMN `remark` text;
--> statement-breakpoint
UPDATE `project_devices` SET `remark` = `note`;
+7
View File
@@ -64,6 +64,13 @@
"when": 1777800000000,
"tag": "0008_circuit_first_model",
"breakpoints": true
},
{
"idx": 9,
"version": "6",
"when": 1784743200000,
"tag": "0009_project_device_circuit_fields",
"breakpoints": true
}
]
}
@@ -2,14 +2,23 @@ import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import { db } from "../client.js";
import { projectDevices } from "../schema/project-devices.js";
import { calculateRowTotalPower } from "../../domain/calculations/circuit-power-calculation.js";
import type {
CreateProjectDeviceInput,
UpdateProjectDeviceInput,
} from "../../shared/validation/project-device.schemas.js";
function withTotalPower(row: typeof projectDevices.$inferSelect) {
return {
...row,
totalPower: calculateRowTotalPower(row.quantity, row.powerPerUnit, row.simultaneityFactor),
};
}
export class ProjectDeviceRepository {
async listByProject(projectId: string) {
return db.select().from(projectDevices).where(eq(projectDevices.projectId, projectId));
const rows = await db.select().from(projectDevices).where(eq(projectDevices.projectId, projectId));
return rows.map(withTotalPower);
}
async create(projectId: string, input: CreateProjectDeviceInput) {
@@ -19,16 +28,27 @@ export class ProjectDeviceRepository {
projectId,
name: input.name,
displayName: input.displayName,
phaseType: input.phaseType,
connectionKind: input.connectionKind ?? null,
costGroup: input.costGroup ?? null,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
powerPerUnit: input.powerPerUnit,
simultaneityFactor: input.simultaneityFactor,
cosPhi: input.cosPhi ?? null,
remark: input.remark ?? null,
installedPowerPerUnitKw: input.powerPerUnit,
demandFactor: input.simultaneityFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
note: input.note ?? null,
phaseCount: input.phaseType === "three_phase" ? 3 : 1,
powerFactor: input.cosPhi ?? null,
note: input.remark ?? null,
});
return { id, projectId, ...input };
const created = await this.findById(projectId, id);
if (!created) {
throw new Error("Failed to create project device.");
}
return created;
}
async findById(projectId: string, projectDeviceId: string) {
@@ -39,7 +59,7 @@ export class ProjectDeviceRepository {
and(eq(projectDevices.id, projectDeviceId), eq(projectDevices.projectId, projectId))
)
.limit(1);
return row ?? null;
return row ? withTotalPower(row) : null;
}
async update(projectId: string, projectDeviceId: string, input: UpdateProjectDeviceInput) {
@@ -48,14 +68,21 @@ export class ProjectDeviceRepository {
.set({
name: input.name,
displayName: input.displayName,
phaseType: input.phaseType,
connectionKind: input.connectionKind ?? null,
costGroup: input.costGroup ?? null,
category: input.category ?? null,
quantity: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
powerPerUnit: input.powerPerUnit,
simultaneityFactor: input.simultaneityFactor,
cosPhi: input.cosPhi ?? null,
remark: input.remark ?? null,
installedPowerPerUnitKw: input.powerPerUnit,
demandFactor: input.simultaneityFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
note: input.note ?? null,
phaseCount: input.phaseType === "three_phase" ? 3 : 1,
powerFactor: input.cosPhi ?? null,
note: input.remark ?? null,
})
.where(
and(eq(projectDevices.id, projectDeviceId), eq(projectDevices.projectId, projectId))
+8
View File
@@ -8,8 +8,16 @@ export const projectDevices = sqliteTable("project_devices", {
.references(() => projects.id, { onDelete: "cascade" }),
name: text("name").notNull(),
displayName: text("display_name").notNull(),
phaseType: text("phase_type").notNull().default("single_phase"),
connectionKind: text("connection_kind"),
costGroup: text("cost_group"),
category: text("category"),
quantity: integer("quantity").notNull(),
powerPerUnit: real("power_per_unit").notNull().default(0),
simultaneityFactor: real("simultaneity_factor").notNull().default(1),
cosPhi: real("cos_phi"),
remark: text("remark"),
// Legacy compatibility fields. Keep these synchronized until the Consumer editor is removed.
installedPowerPerUnitKw: real("installed_power_per_unit_kw").notNull(),
demandFactor: real("demand_factor").notNull(),
voltageV: real("voltage_v"),