Files
leistungsbilanz-ts/src/db/repositories/consumer.repository.ts
T
2026-05-03 21:16:52 +02:00

148 lines
5.3 KiB
TypeScript

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)
)
);
}
}