Remove legacy consumer API

This commit is contained in:
2026-07-23 20:00:48 +02:00
parent 04c299e3f2
commit 3fbf3ac622
30 changed files with 76 additions and 1089 deletions
-147
View File
@@ -1,147 +0,0 @@
import crypto from "node:crypto";
import { and, eq } from "drizzle-orm";
import { db } from "../client.js";
import { consumers } from "../schema/consumers.js";
import type {
CreateConsumerInput,
UpdateConsumerInput,
} from "../../shared/validation/consumer.schemas.js";
export class ConsumerRepository {
async listByProject(projectId: string) {
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";
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: normalizedName,
category: input.category ?? null,
deviceType: input.deviceType ?? null,
phaseType: input.phaseType ?? null,
tradeOrCostGroup: input.tradeOrCostGroup ?? null,
group: input.group ?? null,
protectionType: input.protectionType ?? null,
protectionRatedCurrent: input.protectionRatedCurrent ?? null,
protectionCharacteristic: input.protectionCharacteristic ?? null,
cableType: input.cableType ?? null,
cableCrossSection: input.cableCrossSection ?? null,
comment: input.comment ?? null,
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,
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: normalizedName,
category: input.category ?? null,
deviceType: input.deviceType ?? null,
phaseType: input.phaseType ?? null,
tradeOrCostGroup: input.tradeOrCostGroup ?? null,
group: input.group ?? null,
protectionType: input.protectionType ?? null,
protectionRatedCurrent: input.protectionRatedCurrent ?? null,
protectionCharacteristic: input.protectionCharacteristic ?? null,
cableType: input.cableType ?? null,
cableCrossSection: input.cableCrossSection ?? null,
comment: input.comment ?? null,
quantity: normalizedQuantity,
installedPowerPerUnitKw: normalizedInstalledPowerPerUnitKw,
demandFactor: normalizedDemandFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
note: input.note ?? null,
})
.where(eq(consumers.id, consumerId));
}
async findById(consumerId: string) {
const [row] = await db.select().from(consumers).where(eq(consumers.id, consumerId)).limit(1);
return row ?? null;
}
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)
)
);
}
}
@@ -37,6 +37,13 @@ export interface LegacyMigrationReportPersistenceInput {
export class LegacyConsumerMigrationRepository {
constructor(private readonly database: AppDatabase) {}
async listSourceConsumersByCircuitList(circuitListId: string) {
return this.database
.select()
.from(consumers)
.where(eq(consumers.circuitListId, circuitListId));
}
async listUnmigratedConsumers() {
return this.database
.select({
+1 -1
View File
@@ -5,7 +5,7 @@ import { projects } from "../schema/projects.js";
import type {
CreateProjectInput,
UpdateProjectSettingsInput,
} from "../../shared/validation/consumer.schemas.js";
} from "../../shared/validation/project-structure.schemas.js";
export class ProjectRepository {
async list() {
+1 -1
View File
@@ -2,7 +2,7 @@ import crypto from "node:crypto";
import { and, asc, eq } from "drizzle-orm";
import { db } from "../client.js";
import { rooms } from "../schema/rooms.js";
import type { CreateRoomInput } from "../../shared/validation/consumer.schemas.js";
import type { CreateRoomInput } from "../../shared/validation/project-structure.schemas.js";
export class RoomRepository {
async listByProject(projectId: string) {
+1 -1
View File
@@ -17,7 +17,7 @@ export const projectDevices = sqliteTable("project_devices", {
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.
// Transitional mirror columns retained until a dedicated schema cleanup migration removes them.
installedPowerPerUnitKw: real("installed_power_per_unit_kw").notNull(),
demandFactor: real("demand_factor").notNull(),
voltageV: real("voltage_v"),