Added power sum per distributionboard
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { ConsumerRepository } from "../../db/repositories/consumer.repository.js";
|
||||
import { DistributionBoardRepository } from "../../db/repositories/distribution-board.repository.js";
|
||||
import { PowerBalanceService } from "../../domain/services/power-balance.service.js";
|
||||
import { createConsumerSchema } from "../../shared/validation/consumer.schemas.js";
|
||||
import {
|
||||
createConsumerSchema,
|
||||
updateConsumerSchema,
|
||||
} from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const consumerRepository = new ConsumerRepository();
|
||||
const distributionBoardRepository = new DistributionBoardRepository();
|
||||
const powerBalanceService = new PowerBalanceService();
|
||||
|
||||
async function validateDistributionBoardOwnership(
|
||||
projectId: string,
|
||||
distributionBoardId: string | undefined
|
||||
) {
|
||||
if (!distributionBoardId) {
|
||||
return true;
|
||||
}
|
||||
return distributionBoardRepository.existsInProject(projectId, distributionBoardId);
|
||||
}
|
||||
|
||||
export async function listConsumersByProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
@@ -36,7 +51,72 @@ export async function createConsumer(req: Request, res: Response) {
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const hasValidDistributionBoard = await validateDistributionBoardOwnership(
|
||||
parsed.data.projectId,
|
||||
parsed.data.distributionBoardId
|
||||
);
|
||||
if (!hasValidDistributionBoard) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Distribution board does not belong to the provided project." });
|
||||
}
|
||||
|
||||
const created = await consumerRepository.create(parsed.data);
|
||||
const enriched = powerBalanceService.enrichConsumer(created);
|
||||
return res.status(201).json(enriched);
|
||||
}
|
||||
|
||||
export async function updateConsumer(req: Request, res: Response) {
|
||||
const { consumerId } = req.params;
|
||||
if (typeof consumerId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid consumerId" });
|
||||
}
|
||||
|
||||
const parsed = updateConsumerSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const hasValidDistributionBoard = await validateDistributionBoardOwnership(
|
||||
parsed.data.projectId,
|
||||
parsed.data.distributionBoardId
|
||||
);
|
||||
if (!hasValidDistributionBoard) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Distribution board does not belong to the provided project." });
|
||||
}
|
||||
|
||||
await consumerRepository.update(consumerId, parsed.data);
|
||||
const row = await consumerRepository.findById(consumerId);
|
||||
if (!row) {
|
||||
return res.status(404).json({ error: "Consumer not found" });
|
||||
}
|
||||
|
||||
const enriched = powerBalanceService.enrichConsumer({
|
||||
id: row.id,
|
||||
projectId: row.projectId,
|
||||
distributionBoardId: row.distributionBoardId ?? undefined,
|
||||
name: row.name,
|
||||
category: row.category ?? undefined,
|
||||
quantity: row.quantity,
|
||||
installedPowerPerUnitKw: row.installedPowerPerUnitKw,
|
||||
demandFactor: row.demandFactor,
|
||||
voltageV: row.voltageV ?? undefined,
|
||||
phaseCount: row.phaseCount === 1 || row.phaseCount === 3 ? row.phaseCount : undefined,
|
||||
powerFactor: row.powerFactor ?? undefined,
|
||||
note: row.note ?? undefined,
|
||||
});
|
||||
return res.json(enriched);
|
||||
}
|
||||
|
||||
export async function deleteConsumer(req: Request, res: Response) {
|
||||
const { consumerId } = req.params;
|
||||
if (typeof consumerId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid consumerId" });
|
||||
}
|
||||
|
||||
await consumerRepository.delete(consumerId);
|
||||
return res.status(204).send();
|
||||
}
|
||||
|
||||
@@ -1,8 +1,14 @@
|
||||
import { Router } from "express";
|
||||
import { createConsumer, listConsumersByProject } from "../controllers/consumer.controller.js";
|
||||
import {
|
||||
createConsumer,
|
||||
deleteConsumer,
|
||||
listConsumersByProject,
|
||||
updateConsumer,
|
||||
} from "../controllers/consumer.controller.js";
|
||||
|
||||
export const consumerRouter = Router();
|
||||
|
||||
consumerRouter.get("/projects/:projectId", listConsumersByProject);
|
||||
consumerRouter.post("/", createConsumer);
|
||||
|
||||
consumerRouter.put("/:consumerId", updateConsumer);
|
||||
consumerRouter.delete("/:consumerId", deleteConsumer);
|
||||
|
||||
Reference in New Issue
Block a user