59 lines
2.2 KiB
TypeScript
59 lines
2.2 KiB
TypeScript
import type { Request, Response } from "express";
|
|
import { circuitWriteService } from "../composition/circuit-write-service.js";
|
|
import {
|
|
reorderSectionCircuitsSchema,
|
|
updateSectionEquipmentIdentifiersSchema,
|
|
} from "../../shared/validation/circuit.schemas.js";
|
|
|
|
export async function renumberCircuitSection(req: Request, res: Response) {
|
|
const { sectionId } = req.params;
|
|
if (typeof sectionId !== "string") {
|
|
return res.status(400).json({ error: "Invalid sectionId" });
|
|
}
|
|
|
|
try {
|
|
const updatedCircuits = await circuitWriteService.renumberSection(sectionId);
|
|
return res.json({ sectionId, circuits: updatedCircuits });
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to renumber section." });
|
|
}
|
|
}
|
|
|
|
export async function reorderSectionCircuits(req: Request, res: Response) {
|
|
const { sectionId } = req.params;
|
|
if (typeof sectionId !== "string") {
|
|
return res.status(400).json({ error: "Invalid sectionId" });
|
|
}
|
|
|
|
const parsed = reorderSectionCircuitsSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
try {
|
|
const updatedCircuits = await circuitWriteService.reorderCircuitsInSection(sectionId, parsed.data);
|
|
return res.json({ sectionId, circuits: updatedCircuits });
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to reorder circuits." });
|
|
}
|
|
}
|
|
|
|
export async function updateSectionEquipmentIdentifiers(req: Request, res: Response) {
|
|
const { sectionId } = req.params;
|
|
if (typeof sectionId !== "string") {
|
|
return res.status(400).json({ error: "Invalid sectionId" });
|
|
}
|
|
|
|
const parsed = updateSectionEquipmentIdentifiersSchema.safeParse(req.body);
|
|
if (!parsed.success) {
|
|
return res.status(400).json({ error: parsed.error.flatten() });
|
|
}
|
|
|
|
try {
|
|
const updatedCircuits = await circuitWriteService.updateSectionEquipmentIdentifiers(sectionId, parsed.data);
|
|
return res.json({ sectionId, circuits: updatedCircuits });
|
|
} catch (error) {
|
|
return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to update section identifiers." });
|
|
}
|
|
}
|