Create circuits with rows atomically
This commit is contained in:
@@ -6,6 +6,7 @@ import { ProjectDeviceRepository } from "../../db/repositories/project-device.re
|
||||
import type {
|
||||
CreateCircuitDeviceRowInput,
|
||||
CreateCircuitInput,
|
||||
CreateCircuitWithDeviceRowsInput,
|
||||
MoveCircuitDeviceRowInput,
|
||||
MoveCircuitDeviceRowsBulkInput,
|
||||
ReorderSectionCircuitsInput,
|
||||
@@ -73,6 +74,19 @@ export class CircuitWriteService {
|
||||
}
|
||||
|
||||
// Validates linked project-device id against owning project of the circuit list.
|
||||
private async assertValidLinkedProjectDeviceForProject(
|
||||
projectId: string,
|
||||
linkedProjectDeviceId?: string
|
||||
) {
|
||||
if (!linkedProjectDeviceId) {
|
||||
return;
|
||||
}
|
||||
const device = await this.projectDeviceRepository.findById(projectId, linkedProjectDeviceId);
|
||||
if (!device) {
|
||||
throw new Error("Invalid linked project device id.");
|
||||
}
|
||||
}
|
||||
|
||||
private async assertValidLinkedProjectDevice(circuitId: string, linkedProjectDeviceId?: string) {
|
||||
if (!linkedProjectDeviceId) {
|
||||
return;
|
||||
@@ -85,10 +99,7 @@ export class CircuitWriteService {
|
||||
if (!list) {
|
||||
throw new Error("Circuit list not found.");
|
||||
}
|
||||
const device = await this.projectDeviceRepository.findById(list.projectId, linkedProjectDeviceId);
|
||||
if (!device) {
|
||||
throw new Error("Invalid linked project device id.");
|
||||
}
|
||||
await this.assertValidLinkedProjectDeviceForProject(list.projectId, linkedProjectDeviceId);
|
||||
}
|
||||
|
||||
async createCircuit(projectId: string, circuitListId: string, input: CreateCircuitInput) {
|
||||
@@ -121,6 +132,48 @@ export class CircuitWriteService {
|
||||
return this.circuitRepository.findById(id);
|
||||
}
|
||||
|
||||
async createCircuitWithDeviceRows(
|
||||
projectId: string,
|
||||
circuitListId: string,
|
||||
input: CreateCircuitWithDeviceRowsInput
|
||||
) {
|
||||
const list = await this.circuitListRepository.findById(projectId, circuitListId);
|
||||
if (!list) {
|
||||
throw new Error("Circuit list not found in project.");
|
||||
}
|
||||
await this.assertSectionInList(input.circuit.sectionId, circuitListId);
|
||||
await this.assertUniqueEquipmentIdentifier(
|
||||
circuitListId,
|
||||
input.circuit.equipmentIdentifier
|
||||
);
|
||||
for (const row of input.deviceRows) {
|
||||
await this.assertValidLinkedProjectDeviceForProject(
|
||||
list.projectId,
|
||||
row.linkedProjectDeviceId
|
||||
);
|
||||
}
|
||||
|
||||
const created = this.deviceRowRepository.createCircuitWithDeviceRowsTransactional({
|
||||
circuit: {
|
||||
circuitListId,
|
||||
...input.circuit,
|
||||
isReserve: false,
|
||||
},
|
||||
deviceRows: input.deviceRows,
|
||||
});
|
||||
const circuit = await this.circuitRepository.findById(created.circuitId);
|
||||
const deviceRows = await Promise.all(
|
||||
created.rowIds.map((rowId) => this.deviceRowRepository.findById(rowId))
|
||||
);
|
||||
if (!circuit || deviceRows.some((row) => !row)) {
|
||||
throw new Error("Der angelegte Stromkreis konnte nicht geladen werden.");
|
||||
}
|
||||
return {
|
||||
circuit,
|
||||
deviceRows,
|
||||
};
|
||||
}
|
||||
|
||||
async updateCircuit(circuitId: string, input: UpdateCircuitInput) {
|
||||
const current = await this.circuitRepository.findById(circuitId);
|
||||
if (!current) {
|
||||
|
||||
Reference in New Issue
Block a user