All first todos completed
This commit is contained in:
@@ -1,10 +1,14 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { GlobalDeviceRepository } from "../../db/repositories/global-device.repository.js";
|
||||
import { ConsumerRepository } from "../../db/repositories/consumer.repository.js";
|
||||
import { ProjectDeviceRepository } from "../../db/repositories/project-device.repository.js";
|
||||
import {
|
||||
createProjectDeviceSchema,
|
||||
updateProjectDeviceSchema,
|
||||
} from "../../shared/validation/project-device.schemas.js";
|
||||
|
||||
const globalDeviceRepository = new GlobalDeviceRepository();
|
||||
const consumerRepository = new ConsumerRepository();
|
||||
const projectDeviceRepository = new ProjectDeviceRepository();
|
||||
|
||||
export async function listProjectDevicesByProject(req: Request, res: Response) {
|
||||
@@ -41,6 +45,16 @@ export async function updateProjectDevice(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
await projectDeviceRepository.update(projectId, projectDeviceId, parsed.data);
|
||||
await consumerRepository.syncLinkedConsumersFromProjectDevice(projectId, projectDeviceId, {
|
||||
displayName: parsed.data.displayName,
|
||||
category: parsed.data.category,
|
||||
quantity: parsed.data.quantity,
|
||||
installedPowerPerUnitKw: parsed.data.installedPowerPerUnitKw,
|
||||
demandFactor: parsed.data.demandFactor,
|
||||
phaseCount: parsed.data.phaseCount,
|
||||
powerFactor: parsed.data.powerFactor,
|
||||
note: parsed.data.note,
|
||||
});
|
||||
const row = await projectDeviceRepository.findById(projectId, projectDeviceId);
|
||||
if (!row) {
|
||||
return res.status(404).json({ error: "Project device not found" });
|
||||
@@ -57,3 +71,30 @@ export async function deleteProjectDevice(req: Request, res: Response) {
|
||||
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,
|
||||
category: source.category ?? undefined,
|
||||
quantity: source.quantity,
|
||||
installedPowerPerUnitKw: source.installedPowerPerUnitKw,
|
||||
demandFactor: source.demandFactor,
|
||||
voltageV: source.voltageV ?? undefined,
|
||||
phaseCount: source.phaseCount === 1 || source.phaseCount === 3 ? source.phaseCount : undefined,
|
||||
powerFactor: source.powerFactor ?? undefined,
|
||||
note: source.note ?? undefined,
|
||||
});
|
||||
|
||||
return res.status(201).json(created);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user