All first todos completed
This commit is contained in:
@@ -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)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user