import type { Request, Response } from "express"; import { GlobalDeviceRepository } from "../../db/repositories/global-device.repository.js"; import { ProjectDeviceRepository } from "../../db/repositories/project-device.repository.js"; import { projectDeviceSyncService } from "../composition/project-device-sync-service.js"; import { createProjectDeviceSchema, disconnectProjectDeviceRowsSchema, reconnectProjectDeviceRowsSchema, restoreProjectDeviceRowsSchema, synchronizeProjectDeviceRowsSchema, updateProjectDeviceSchema, } from "../../shared/validation/project-device.schemas.js"; const globalDeviceRepository = new GlobalDeviceRepository(); const projectDeviceRepository = new ProjectDeviceRepository(); export async function listProjectDevicesByProject(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const rows = await projectDeviceRepository.listByProject(projectId); return res.json(rows); } export async function createProjectDevice(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const parsed = createProjectDeviceSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } const created = await projectDeviceRepository.create(projectId, parsed.data); return res.status(201).json(created); } export async function updateProjectDevice(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = updateProjectDeviceSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } await projectDeviceRepository.update(projectId, projectDeviceId, parsed.data); // Linked rows are synchronized only through the explicit review flow. // Updating a project device must never silently overwrite local circuit-list values. const row = await projectDeviceRepository.findById(projectId, projectDeviceId); if (!row) { return res.status(404).json({ error: "Project device not found" }); } return res.json(row); } export async function deleteProjectDevice(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } await projectDeviceRepository.delete(projectId, projectDeviceId); return res.status(204).send(); } export async function copyGlobalDeviceToProject(req: Request, res: Response) { const { projectId, globalDeviceId } = req.params; if (typeof projectId !== "string" || typeof globalDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const source = await globalDeviceRepository.findById(globalDeviceId); if (!source) { return res.status(404).json({ error: "Global device not found" }); } const created = await projectDeviceRepository.create(projectId, { name: source.name, displayName: source.displayName, phaseType: source.phaseCount === 3 ? "three_phase" : "single_phase", category: source.category ?? undefined, quantity: source.quantity, powerPerUnit: source.installedPowerPerUnitKw, simultaneityFactor: source.demandFactor, cosPhi: source.powerFactor ?? undefined, remark: source.note ?? undefined, voltageV: source.voltageV ?? undefined, }); return res.status(201).json(created); } export async function getProjectDeviceSyncPreview(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } try { const preview = await projectDeviceSyncService.getPreview(projectId, projectDeviceId); return res.json(preview); } catch (error) { return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to load sync preview." }); } } export async function synchronizeProjectDeviceRows(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = synchronizeProjectDeviceRowsSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } try { const result = await projectDeviceSyncService.synchronize( projectId, projectDeviceId, parsed.data.rowIds, parsed.data.fields ); return res.json(result); } catch (error) { return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to synchronize rows." }); } } export async function restoreProjectDeviceRows(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = restoreProjectDeviceRowsSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } try { const preview = await projectDeviceSyncService.restore(projectId, projectDeviceId, parsed.data.rows); return res.json(preview); } catch (error) { return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to restore rows." }); } } export async function disconnectProjectDeviceRows(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = disconnectProjectDeviceRowsSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } try { const result = await projectDeviceSyncService.disconnect(projectId, projectDeviceId, parsed.data.rowIds); return res.json(result); } catch (error) { return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to disconnect rows." }); } } export async function reconnectProjectDeviceRows(req: Request, res: Response) { const { projectId, projectDeviceId } = req.params; if (typeof projectId !== "string" || typeof projectDeviceId !== "string") { return res.status(400).json({ error: "Invalid parameters" }); } const parsed = reconnectProjectDeviceRowsSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } try { const preview = await projectDeviceSyncService.reconnect(projectId, projectDeviceId, parsed.data.rowIds); return res.json(preview); } catch (error) { return res.status(400).json({ error: error instanceof Error ? error.message : "Failed to reconnect rows." }); } }