leistungsbilanz-ts/src/db/repositories/consumer.repository.ts

89 lines
3.3 KiB
TypeScript

import crypto from "node:crypto";
import { 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 create(input: CreateConsumerInput) {
const id = crypto.randomUUID();
await db.insert(consumers).values({
id,
projectId: input.projectId,
distributionBoardId: input.distributionBoardId ?? null,
circuitListId: input.circuitListId ?? null,
roomId: input.roomId ?? null,
circuitNumber: input.circuitNumber ?? null,
description: input.description ?? null,
name: input.name,
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: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
voltageV: input.voltageV ?? null,
phaseCount: input.phaseCount ?? null,
powerFactor: input.powerFactor ?? null,
note: input.note ?? null,
});
return { id, ...input };
}
async update(consumerId: string, input: UpdateConsumerInput) {
await db
.update(consumers)
.set({
projectId: input.projectId,
distributionBoardId: input.distributionBoardId ?? null,
circuitListId: input.circuitListId ?? null,
roomId: input.roomId ?? null,
circuitNumber: input.circuitNumber ?? null,
description: input.description ?? null,
name: input.name,
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: input.quantity,
installedPowerPerUnitKw: input.installedPowerPerUnitKw,
demandFactor: input.demandFactor,
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));
}
}