import type { Request, Response } from "express"; import { createDistributionBoardInsertProjectCommand, createDistributionBoardStructureSnapshot, } from "../../domain/models/distribution-board-structure-project-command.model.js"; import { createDistributionBoardSchema } from "../../shared/validation/project-structure.schemas.js"; import { projectCommandService } from "../composition/project-command-stores.js"; import { distributionBoardRepository } from "../composition/application-repositories.js"; import { respondWithProjectCommandError } from "./project-command.controller.js"; export async function listDistributionBoardsByProject(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const result = await distributionBoardRepository.listByProject(projectId); return res.json(result); } export async function createDistributionBoard(req: Request, res: Response) { const { projectId } = req.params; if (typeof projectId !== "string") { return res.status(400).json({ error: "Invalid projectId" }); } const parsed = createDistributionBoardSchema.safeParse(req.body); if (!parsed.success) { return res.status(400).json({ error: parsed.error.flatten() }); } const structure = createDistributionBoardStructureSnapshot( projectId, parsed.data.name ); try { const result = projectCommandService.executeUser({ projectId, expectedRevision: parsed.data.expectedRevision, description: "Verteilung anlegen", command: createDistributionBoardInsertProjectCommand(structure), }); return res.status(201).json({ ...result, distributionBoard: structure.distributionBoard, }); } catch (error) { return respondWithProjectCommandError(error, res); } }