Rewrite frontend, added rooms, voltage selection per project, startet with todos
This commit is contained in:
@@ -0,0 +1,14 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { CircuitListRepository } from "../../db/repositories/circuit-list.repository.js";
|
||||
|
||||
const circuitListRepository = new CircuitListRepository();
|
||||
|
||||
export async function listCircuitListsByProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const result = await circuitListRepository.listByProject(projectId);
|
||||
return res.json(result);
|
||||
}
|
||||
@@ -1,16 +1,54 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { CircuitListRepository } from "../../db/repositories/circuit-list.repository.js";
|
||||
import { ConsumerRepository } from "../../db/repositories/consumer.repository.js";
|
||||
import { DistributionBoardRepository } from "../../db/repositories/distribution-board.repository.js";
|
||||
import { FloorRepository } from "../../db/repositories/floor.repository.js";
|
||||
import { ProjectRepository } from "../../db/repositories/project.repository.js";
|
||||
import { RoomRepository } from "../../db/repositories/room.repository.js";
|
||||
import type { Consumer } from "../../domain/models/consumer.model.js";
|
||||
import { PowerBalanceService } from "../../domain/services/power-balance.service.js";
|
||||
import {
|
||||
createConsumerSchema,
|
||||
updateConsumerSchema,
|
||||
} from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const circuitListRepository = new CircuitListRepository();
|
||||
const consumerRepository = new ConsumerRepository();
|
||||
const distributionBoardRepository = new DistributionBoardRepository();
|
||||
const floorRepository = new FloorRepository();
|
||||
const projectRepository = new ProjectRepository();
|
||||
const roomRepository = new RoomRepository();
|
||||
const powerBalanceService = new PowerBalanceService();
|
||||
|
||||
type ConsumerRow = {
|
||||
id: string;
|
||||
projectId: string;
|
||||
distributionBoardId: string | null;
|
||||
circuitListId: string | null;
|
||||
roomId: string | null;
|
||||
circuitNumber: string | null;
|
||||
description: string | null;
|
||||
name: string;
|
||||
category: string | null;
|
||||
deviceType: string | null;
|
||||
phaseType: string | null;
|
||||
tradeOrCostGroup: string | null;
|
||||
group: string | null;
|
||||
protectionType: string | null;
|
||||
protectionRatedCurrent: number | null;
|
||||
protectionCharacteristic: string | null;
|
||||
cableType: string | null;
|
||||
cableCrossSection: string | null;
|
||||
comment: string | null;
|
||||
quantity: number;
|
||||
installedPowerPerUnitKw: number;
|
||||
demandFactor: number;
|
||||
voltageV: number | null;
|
||||
phaseCount: number | null;
|
||||
powerFactor: number | null;
|
||||
note: string | null;
|
||||
};
|
||||
|
||||
async function validateDistributionBoardOwnership(
|
||||
projectId: string,
|
||||
distributionBoardId: string | undefined
|
||||
@@ -21,29 +59,134 @@ async function validateDistributionBoardOwnership(
|
||||
return distributionBoardRepository.existsInProject(projectId, distributionBoardId);
|
||||
}
|
||||
|
||||
async function validateRoomOwnership(projectId: string, roomId: string | undefined) {
|
||||
if (!roomId) {
|
||||
return true;
|
||||
}
|
||||
return roomRepository.existsInProject(projectId, roomId);
|
||||
}
|
||||
|
||||
async function resolveCircuitScope(input: {
|
||||
projectId: string;
|
||||
distributionBoardId?: string;
|
||||
circuitListId?: string;
|
||||
}) {
|
||||
let distributionBoardId = input.distributionBoardId;
|
||||
let circuitListId = input.circuitListId;
|
||||
|
||||
if (distributionBoardId) {
|
||||
const linkedList = await circuitListRepository.findByDistributionBoardId(
|
||||
input.projectId,
|
||||
distributionBoardId
|
||||
);
|
||||
if (!linkedList) {
|
||||
return { ok: false as const, error: "No circuit list found for the provided distribution board." };
|
||||
}
|
||||
if (circuitListId && circuitListId !== linkedList.id) {
|
||||
return {
|
||||
ok: false as const,
|
||||
error: "Circuit list does not match the provided distribution board.",
|
||||
};
|
||||
}
|
||||
circuitListId = linkedList.id;
|
||||
}
|
||||
|
||||
if (circuitListId) {
|
||||
const list = await circuitListRepository.findById(input.projectId, circuitListId);
|
||||
if (!list) {
|
||||
return { ok: false as const, error: "Circuit list does not belong to the provided project." };
|
||||
}
|
||||
if (distributionBoardId && distributionBoardId !== list.distributionBoardId) {
|
||||
return {
|
||||
ok: false as const,
|
||||
error: "Circuit list does not match the provided distribution board.",
|
||||
};
|
||||
}
|
||||
distributionBoardId = list.distributionBoardId;
|
||||
}
|
||||
|
||||
return {
|
||||
ok: true as const,
|
||||
distributionBoardId,
|
||||
circuitListId,
|
||||
};
|
||||
}
|
||||
|
||||
function buildConsumerFromRow(
|
||||
row: ConsumerRow,
|
||||
roomById: Map<string, { floorId: string | null; roomName: string; roomNumber: string }>,
|
||||
floorById: Map<string, { name: string }>
|
||||
): Consumer {
|
||||
const room = row.roomId ? roomById.get(row.roomId) : undefined;
|
||||
const floor = room?.floorId ? floorById.get(room.floorId) : undefined;
|
||||
|
||||
return {
|
||||
id: row.id,
|
||||
projectId: row.projectId,
|
||||
distributionBoardId: row.distributionBoardId ?? undefined,
|
||||
circuitListId: row.circuitListId ?? undefined,
|
||||
roomId: row.roomId ?? undefined,
|
||||
roomNumber: room?.roomNumber,
|
||||
roomName: room?.roomName,
|
||||
floorId: room?.floorId ?? undefined,
|
||||
floorName: floor?.name,
|
||||
circuitNumber: row.circuitNumber ?? undefined,
|
||||
description: row.description ?? undefined,
|
||||
name: row.name,
|
||||
category: row.category ?? undefined,
|
||||
deviceType: row.deviceType ?? undefined,
|
||||
phaseType: row.phaseType ?? undefined,
|
||||
tradeOrCostGroup: row.tradeOrCostGroup ?? undefined,
|
||||
group: row.group ?? undefined,
|
||||
protectionType: row.protectionType ?? undefined,
|
||||
protectionRatedCurrent: row.protectionRatedCurrent ?? undefined,
|
||||
protectionCharacteristic: row.protectionCharacteristic ?? undefined,
|
||||
cableType: row.cableType ?? undefined,
|
||||
cableCrossSection: row.cableCrossSection ?? undefined,
|
||||
comment: row.comment ?? 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,
|
||||
};
|
||||
}
|
||||
|
||||
export async function listConsumersByProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
const rows = await consumerRepository.listByProject(projectId);
|
||||
const enriched = rows.map((row) =>
|
||||
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,
|
||||
})
|
||||
|
||||
const [rows, project, floors, rooms] = await Promise.all([
|
||||
consumerRepository.listByProject(projectId),
|
||||
projectRepository.findById(projectId),
|
||||
floorRepository.listByProject(projectId),
|
||||
roomRepository.listByProject(projectId),
|
||||
]);
|
||||
|
||||
const roomById = new Map(
|
||||
rooms.map((room) => [room.id, { floorId: room.floorId, roomName: room.roomName, roomNumber: room.roomNumber }])
|
||||
);
|
||||
res.json(enriched);
|
||||
const floorById = new Map(floors.map((floor) => [floor.id, { name: floor.name }]));
|
||||
|
||||
const projectVoltageDefaults = project
|
||||
? {
|
||||
singlePhaseVoltageV: project.singlePhaseVoltageV,
|
||||
threePhaseVoltageV: project.threePhaseVoltageV,
|
||||
}
|
||||
: undefined;
|
||||
|
||||
const enriched = rows.map((row) =>
|
||||
powerBalanceService.enrichConsumer(
|
||||
buildConsumerFromRow(row as ConsumerRow, roomById, floorById),
|
||||
projectVoltageDefaults
|
||||
)
|
||||
);
|
||||
|
||||
return res.json(enriched);
|
||||
}
|
||||
|
||||
export async function createConsumer(req: Request, res: Response) {
|
||||
@@ -52,18 +195,66 @@ export async function createConsumer(req: Request, res: Response) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const hasValidDistributionBoard = await validateDistributionBoardOwnership(
|
||||
parsed.data.projectId,
|
||||
parsed.data.distributionBoardId
|
||||
);
|
||||
const [hasValidDistributionBoard, hasValidRoom] = await Promise.all([
|
||||
validateDistributionBoardOwnership(parsed.data.projectId, parsed.data.distributionBoardId),
|
||||
validateRoomOwnership(parsed.data.projectId, parsed.data.roomId),
|
||||
]);
|
||||
if (!hasValidDistributionBoard) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Distribution board does not belong to the provided project." });
|
||||
}
|
||||
if (!hasValidRoom) {
|
||||
return res.status(400).json({ error: "Room does not belong to the provided project." });
|
||||
}
|
||||
|
||||
const resolvedScope = await resolveCircuitScope({
|
||||
projectId: parsed.data.projectId,
|
||||
distributionBoardId: parsed.data.distributionBoardId,
|
||||
circuitListId: parsed.data.circuitListId,
|
||||
});
|
||||
if (!resolvedScope.ok) {
|
||||
return res.status(400).json({ error: resolvedScope.error });
|
||||
}
|
||||
|
||||
const payload = {
|
||||
...parsed.data,
|
||||
distributionBoardId: resolvedScope.distributionBoardId,
|
||||
circuitListId: resolvedScope.circuitListId,
|
||||
description: parsed.data.description ?? parsed.data.name,
|
||||
};
|
||||
|
||||
const created = await consumerRepository.create(payload);
|
||||
const [project, floors, rooms] = await Promise.all([
|
||||
projectRepository.findById(parsed.data.projectId),
|
||||
floorRepository.listByProject(parsed.data.projectId),
|
||||
roomRepository.listByProject(parsed.data.projectId),
|
||||
]);
|
||||
const roomById = new Map(
|
||||
rooms.map((room) => [room.id, { floorId: room.floorId, roomName: room.roomName, roomNumber: room.roomNumber }])
|
||||
);
|
||||
const floorById = new Map(floors.map((floor) => [floor.id, { name: floor.name }]));
|
||||
|
||||
const enriched = powerBalanceService.enrichConsumer(
|
||||
{
|
||||
...(created as Consumer),
|
||||
description: created.description ?? created.name,
|
||||
roomNumber: created.roomId ? roomById.get(created.roomId)?.roomNumber : undefined,
|
||||
roomName: created.roomId ? roomById.get(created.roomId)?.roomName : undefined,
|
||||
floorId: created.roomId ? roomById.get(created.roomId)?.floorId ?? undefined : undefined,
|
||||
floorName:
|
||||
created.roomId && roomById.get(created.roomId)?.floorId
|
||||
? floorById.get(roomById.get(created.roomId)!.floorId as string)?.name
|
||||
: undefined,
|
||||
},
|
||||
project
|
||||
? {
|
||||
singlePhaseVoltageV: project.singlePhaseVoltageV,
|
||||
threePhaseVoltageV: project.threePhaseVoltageV,
|
||||
}
|
||||
: undefined
|
||||
);
|
||||
|
||||
const created = await consumerRepository.create(parsed.data);
|
||||
const enriched = powerBalanceService.enrichConsumer(created);
|
||||
return res.status(201).json(enriched);
|
||||
}
|
||||
|
||||
@@ -78,36 +269,60 @@ export async function updateConsumer(req: Request, res: Response) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const hasValidDistributionBoard = await validateDistributionBoardOwnership(
|
||||
parsed.data.projectId,
|
||||
parsed.data.distributionBoardId
|
||||
);
|
||||
const [hasValidDistributionBoard, hasValidRoom] = await Promise.all([
|
||||
validateDistributionBoardOwnership(parsed.data.projectId, parsed.data.distributionBoardId),
|
||||
validateRoomOwnership(parsed.data.projectId, parsed.data.roomId),
|
||||
]);
|
||||
if (!hasValidDistributionBoard) {
|
||||
return res
|
||||
.status(400)
|
||||
.json({ error: "Distribution board does not belong to the provided project." });
|
||||
}
|
||||
if (!hasValidRoom) {
|
||||
return res.status(400).json({ error: "Room does not belong to the provided project." });
|
||||
}
|
||||
|
||||
const resolvedScope = await resolveCircuitScope({
|
||||
projectId: parsed.data.projectId,
|
||||
distributionBoardId: parsed.data.distributionBoardId,
|
||||
circuitListId: parsed.data.circuitListId,
|
||||
});
|
||||
if (!resolvedScope.ok) {
|
||||
return res.status(400).json({ error: resolvedScope.error });
|
||||
}
|
||||
|
||||
await consumerRepository.update(consumerId, {
|
||||
...parsed.data,
|
||||
distributionBoardId: resolvedScope.distributionBoardId,
|
||||
circuitListId: resolvedScope.circuitListId,
|
||||
description: parsed.data.description ?? parsed.data.name,
|
||||
});
|
||||
|
||||
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,
|
||||
});
|
||||
const [project, floors, rooms] = await Promise.all([
|
||||
projectRepository.findById(row.projectId),
|
||||
floorRepository.listByProject(row.projectId),
|
||||
roomRepository.listByProject(row.projectId),
|
||||
]);
|
||||
const roomById = new Map(
|
||||
rooms.map((room) => [room.id, { floorId: room.floorId, roomName: room.roomName, roomNumber: room.roomNumber }])
|
||||
);
|
||||
const floorById = new Map(floors.map((floor) => [floor.id, { name: floor.name }]));
|
||||
|
||||
const enriched = powerBalanceService.enrichConsumer(
|
||||
buildConsumerFromRow(row as ConsumerRow, roomById, floorById),
|
||||
project
|
||||
? {
|
||||
singlePhaseVoltageV: project.singlePhaseVoltageV,
|
||||
threePhaseVoltageV: project.threePhaseVoltageV,
|
||||
}
|
||||
: undefined
|
||||
);
|
||||
|
||||
return res.json(enriched);
|
||||
}
|
||||
|
||||
|
||||
@@ -1,7 +1,9 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { CircuitListRepository } from "../../db/repositories/circuit-list.repository.js";
|
||||
import { DistributionBoardRepository } from "../../db/repositories/distribution-board.repository.js";
|
||||
import { createDistributionBoardSchema } from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const circuitListRepository = new CircuitListRepository();
|
||||
const distributionBoardRepository = new DistributionBoardRepository();
|
||||
|
||||
export async function listDistributionBoardsByProject(req: Request, res: Response) {
|
||||
@@ -26,5 +28,10 @@ export async function createDistributionBoard(req: Request, res: Response) {
|
||||
}
|
||||
|
||||
const board = await distributionBoardRepository.create(projectId, parsed.data.name);
|
||||
await circuitListRepository.createForDistributionBoard({
|
||||
projectId,
|
||||
distributionBoardId: board.id,
|
||||
name: `${board.name} Stromkreisliste`,
|
||||
});
|
||||
return res.status(201).json(board);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,30 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { FloorRepository } from "../../db/repositories/floor.repository.js";
|
||||
import { createFloorSchema } from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const floorRepository = new FloorRepository();
|
||||
|
||||
export async function listFloorsByProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const result = await floorRepository.listByProject(projectId);
|
||||
return res.json(result);
|
||||
}
|
||||
|
||||
export async function createFloor(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const parsed = createFloorSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const floor = await floorRepository.create(projectId, parsed.data.name);
|
||||
return res.status(201).json(floor);
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { GlobalDeviceRepository } from "../../db/repositories/global-device.repository.js";
|
||||
import {
|
||||
createGlobalDeviceSchema,
|
||||
updateGlobalDeviceSchema,
|
||||
} from "../../shared/validation/global-device.schemas.js";
|
||||
|
||||
const globalDeviceRepository = new GlobalDeviceRepository();
|
||||
|
||||
export async function listGlobalDevices(_req: Request, res: Response) {
|
||||
const rows = await globalDeviceRepository.list();
|
||||
return res.json(rows);
|
||||
}
|
||||
|
||||
export async function createGlobalDevice(req: Request, res: Response) {
|
||||
const parsed = createGlobalDeviceSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
const created = await globalDeviceRepository.create(parsed.data);
|
||||
return res.status(201).json(created);
|
||||
}
|
||||
|
||||
export async function updateGlobalDevice(req: Request, res: Response) {
|
||||
const { globalDeviceId } = req.params;
|
||||
if (typeof globalDeviceId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid globalDeviceId" });
|
||||
}
|
||||
|
||||
const parsed = updateGlobalDeviceSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
await globalDeviceRepository.update(globalDeviceId, parsed.data);
|
||||
const row = await globalDeviceRepository.findById(globalDeviceId);
|
||||
if (!row) {
|
||||
return res.status(404).json({ error: "Global device not found" });
|
||||
}
|
||||
return res.json(row);
|
||||
}
|
||||
|
||||
export async function deleteGlobalDevice(req: Request, res: Response) {
|
||||
const { globalDeviceId } = req.params;
|
||||
if (typeof globalDeviceId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid globalDeviceId" });
|
||||
}
|
||||
|
||||
await globalDeviceRepository.delete(globalDeviceId);
|
||||
return res.status(204).send();
|
||||
}
|
||||
@@ -0,0 +1,59 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { ProjectDeviceRepository } from "../../db/repositories/project-device.repository.js";
|
||||
import {
|
||||
createProjectDeviceSchema,
|
||||
updateProjectDeviceSchema,
|
||||
} from "../../shared/validation/project-device.schemas.js";
|
||||
|
||||
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);
|
||||
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();
|
||||
}
|
||||
@@ -1,6 +1,9 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { ProjectRepository } from "../../db/repositories/project.repository.js";
|
||||
import { createProjectSchema } from "../../shared/validation/consumer.schemas.js";
|
||||
import {
|
||||
createProjectSchema,
|
||||
updateProjectSettingsSchema,
|
||||
} from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const projectRepository = new ProjectRepository();
|
||||
|
||||
@@ -14,7 +17,37 @@ export async function createProject(req: Request, res: Response) {
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
const project = await projectRepository.create(parsed.data.name);
|
||||
const project = await projectRepository.create(parsed.data);
|
||||
return res.status(201).json(project);
|
||||
}
|
||||
|
||||
export async function getProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
const row = await projectRepository.findById(projectId);
|
||||
if (!row) {
|
||||
return res.status(404).json({ error: "Project not found" });
|
||||
}
|
||||
return res.json(row);
|
||||
}
|
||||
|
||||
export async function updateProjectSettings(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const parsed = updateProjectSettingsSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
await projectRepository.updateSettings(projectId, parsed.data);
|
||||
const row = await projectRepository.findById(projectId);
|
||||
if (!row) {
|
||||
return res.status(404).json({ error: "Project not found" });
|
||||
}
|
||||
return res.json(row);
|
||||
}
|
||||
|
||||
@@ -0,0 +1,39 @@
|
||||
import type { Request, Response } from "express";
|
||||
import { FloorRepository } from "../../db/repositories/floor.repository.js";
|
||||
import { RoomRepository } from "../../db/repositories/room.repository.js";
|
||||
import { createRoomSchema } from "../../shared/validation/consumer.schemas.js";
|
||||
|
||||
const floorRepository = new FloorRepository();
|
||||
const roomRepository = new RoomRepository();
|
||||
|
||||
export async function listRoomsByProject(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const result = await roomRepository.listByProject(projectId);
|
||||
return res.json(result);
|
||||
}
|
||||
|
||||
export async function createRoom(req: Request, res: Response) {
|
||||
const { projectId } = req.params;
|
||||
if (typeof projectId !== "string") {
|
||||
return res.status(400).json({ error: "Invalid projectId" });
|
||||
}
|
||||
|
||||
const parsed = createRoomSchema.safeParse(req.body);
|
||||
if (!parsed.success) {
|
||||
return res.status(400).json({ error: parsed.error.flatten() });
|
||||
}
|
||||
|
||||
if (parsed.data.floorId) {
|
||||
const hasValidFloor = await floorRepository.existsInProject(projectId, parsed.data.floorId);
|
||||
if (!hasValidFloor) {
|
||||
return res.status(400).json({ error: "Floor does not belong to the provided project." });
|
||||
}
|
||||
}
|
||||
|
||||
const room = await roomRepository.create(projectId, parsed.data);
|
||||
return res.status(201).json(room);
|
||||
}
|
||||
Reference in New Issue
Block a user