125 lines
3.8 KiB
TypeScript
125 lines
3.8 KiB
TypeScript
import crypto from "node:crypto";
|
|
import { and, asc, eq, ne } from "drizzle-orm";
|
|
import { db } from "../client.js";
|
|
import { circuits } from "../schema/circuits.js";
|
|
import {
|
|
toCircuitCreateValues,
|
|
toCircuitPatchValues,
|
|
type CircuitCreatePersistenceInput,
|
|
type CircuitPatchPersistenceInput,
|
|
} from "./circuit.persistence.js";
|
|
|
|
export type {
|
|
CircuitCreatePersistenceInput,
|
|
CircuitPatchPersistenceInput,
|
|
} from "./circuit.persistence.js";
|
|
export {
|
|
toCircuitCreateValues,
|
|
toCircuitPatchValues,
|
|
} from "./circuit.persistence.js";
|
|
|
|
export class CircuitRepository {
|
|
async findById(circuitId: string) {
|
|
const [row] = await db.select().from(circuits).where(eq(circuits.id, circuitId)).limit(1);
|
|
return row ?? null;
|
|
}
|
|
|
|
async listByCircuitList(circuitListId: string) {
|
|
return db
|
|
.select()
|
|
.from(circuits)
|
|
.where(eq(circuits.circuitListId, circuitListId))
|
|
.orderBy(asc(circuits.sortOrder), asc(circuits.equipmentIdentifier));
|
|
}
|
|
|
|
async create(input: CircuitCreatePersistenceInput) {
|
|
const id = crypto.randomUUID();
|
|
await db.insert(circuits).values(toCircuitCreateValues(id, input));
|
|
return id;
|
|
}
|
|
|
|
async update(
|
|
circuitId: string,
|
|
input: {
|
|
sectionId: string;
|
|
equipmentIdentifier: string;
|
|
displayName?: string;
|
|
sortOrder: number;
|
|
protectionType?: string;
|
|
protectionRatedCurrent?: number;
|
|
protectionCharacteristic?: string;
|
|
cableType?: string;
|
|
cableCrossSection?: string;
|
|
cableLength?: number;
|
|
rcdAssignment?: string;
|
|
terminalDesignation?: string;
|
|
voltage?: number;
|
|
controlRequirement?: string;
|
|
status?: string;
|
|
isReserve: boolean;
|
|
remark?: string;
|
|
}
|
|
) {
|
|
await db
|
|
.update(circuits)
|
|
.set({
|
|
sectionId: input.sectionId,
|
|
equipmentIdentifier: input.equipmentIdentifier,
|
|
displayName: input.displayName ?? null,
|
|
sortOrder: input.sortOrder,
|
|
protectionType: input.protectionType ?? null,
|
|
protectionRatedCurrent: input.protectionRatedCurrent ?? null,
|
|
protectionCharacteristic: input.protectionCharacteristic ?? null,
|
|
cableType: input.cableType ?? null,
|
|
cableCrossSection: input.cableCrossSection ?? null,
|
|
cableLength: input.cableLength ?? null,
|
|
rcdAssignment: input.rcdAssignment ?? null,
|
|
terminalDesignation: input.terminalDesignation ?? null,
|
|
voltage: input.voltage ?? null,
|
|
controlRequirement: input.controlRequirement ?? null,
|
|
status: input.status ?? null,
|
|
isReserve: input.isReserve ? 1 : 0,
|
|
remark: input.remark ?? null,
|
|
})
|
|
.where(eq(circuits.id, circuitId));
|
|
}
|
|
|
|
async updateFields(circuitId: string, input: CircuitPatchPersistenceInput) {
|
|
const values = toCircuitPatchValues(input);
|
|
if (Object.keys(values).length === 0) {
|
|
return;
|
|
}
|
|
await db.update(circuits).set(values).where(eq(circuits.id, circuitId));
|
|
}
|
|
|
|
async delete(circuitId: string) {
|
|
await db.delete(circuits).where(eq(circuits.id, circuitId));
|
|
}
|
|
|
|
async existsByEquipmentIdentifier(circuitListId: string, equipmentIdentifier: string, excludeCircuitId?: string) {
|
|
const rows = await db
|
|
.select({ id: circuits.id })
|
|
.from(circuits)
|
|
.where(
|
|
excludeCircuitId
|
|
? and(
|
|
eq(circuits.circuitListId, circuitListId),
|
|
eq(circuits.equipmentIdentifier, equipmentIdentifier),
|
|
ne(circuits.id, excludeCircuitId)
|
|
)
|
|
: and(eq(circuits.circuitListId, circuitListId), eq(circuits.equipmentIdentifier, equipmentIdentifier))
|
|
)
|
|
.limit(1);
|
|
return Boolean(rows.length);
|
|
}
|
|
|
|
async listBySection(sectionId: string) {
|
|
return db
|
|
.select()
|
|
.from(circuits)
|
|
.where(eq(circuits.sectionId, sectionId))
|
|
.orderBy(asc(circuits.sortOrder), asc(circuits.equipmentIdentifier));
|
|
}
|
|
|
|
}
|