import { Request, Response } from "express"; import ProtocolService from "../../../service/club/protocol/protocolService"; import ProtocolFactory from "../../../factory/admin/club/protocol/protocol"; import ProtocolAgendaService from "../../../service/club/protocol/protocolAgendaService"; import ProtocolAgendaFactory from "../../../factory/admin/club/protocol/protocolAgenda"; import ProtocolDecisionService from "../../../service/club/protocol/protocolDecisionService"; import ProtocolDecisionFactory from "../../../factory/admin/club/protocol/protocolDecision"; import ProtocolPresenceService from "../../../service/club/protocol/protocolPrecenseService"; import ProtocolPresenceFactory from "../../../factory/admin/club/protocol/protocolPresence"; import ProtocolVotingService from "../../../service/club/protocol/protocolVotingService"; import ProtocolVotingFactory from "../../../factory/admin/club/protocol/protocolVoting"; import { CreateProtocolCommand, SynchronizeProtocolCommand } from "../../../command/club/protocol/protocolCommand"; import ProtocolCommandHandler from "../../../command/club/protocol/protocolCommandHandler"; import { SynchronizeProtocolAgendaCommand } from "../../../command/club/protocol/protocolAgendaCommand"; import ProtocolAgendaCommandHandler from "../../../command/club/protocol/protocolAgendaCommandHandler"; import { ProtocolAgendaViewModel } from "../../../viewmodel/admin/club/protocol/protocolAgenda.models"; import ProtocolDecisionCommandHandler from "../../../command/club/protocol/protocolDecisionCommandHandler"; import { ProtocolDecisionViewModel } from "../../../viewmodel/admin/club/protocol/protocolDecision.models"; import ProtocolPresenceCommandHandler from "../../../command/club/protocol/protocolPresenceCommandHandler"; import { SynchronizeProtocolPresenceCommand } from "../../../command/club/protocol/protocolPresenceCommand"; import { SynchronizeProtocolDecisionCommand } from "../../../command/club/protocol/protocolDecisionCommand"; import { SynchronizeProtocolVotingCommand } from "../../../command/club/protocol/protocolVotingCommand"; import { ProtocolVotingViewModel } from "../../../viewmodel/admin/club/protocol/protocolVoting.models"; import ProtocolVotingCommandHandler from "../../../command/club/protocol/protocolVotingCommandHandler"; import { PdfExport } from "../../../helpers/pdfExport"; import ProtocolPrintoutService from "../../../service/club/protocol/protocolPrintoutService"; import ProtocolPrintoutFactory from "../../../factory/admin/club/protocol/protocolPrintout"; import { CreateProtocolPrintoutCommand } from "../../../command/club/protocol/protocolPrintoutCommand"; import ProtocolPrintoutCommandHandler from "../../../command/club/protocol/protocolPrintoutCommandHandler"; import { FileSystemHelper } from "../../../helpers/fileSystemHelper"; import { ProtocolPresenceViewModel } from "../../../viewmodel/admin/club/protocol/protocolPresence.models"; /** * @description get all protocols * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getAllProtocols(req: Request, res: Response): Promise { let offset = parseInt((req.query.offset as string) ?? "0"); let count = parseInt((req.query.count as string) ?? "25"); let [protocols, total] = await ProtocolService.getAll(offset, count); res.json({ protocols: ProtocolFactory.mapToBase(protocols), total: total, offset: offset, count: count, }); } /** * @description get protocol by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolById(req: Request, res: Response): Promise { let id = parseInt(req.params.id); let protocol = await ProtocolService.getById(id); res.json(ProtocolFactory.mapToSingle(protocol)); } /** * @description get protocol agenda by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolAgendaById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let agenda = await ProtocolAgendaService.getAll(protocolId); res.json(ProtocolAgendaFactory.mapToBase(agenda)); } /** * @description get protocol decisions by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolDecisonsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let decisions = await ProtocolDecisionService.getAll(protocolId); res.json(ProtocolDecisionFactory.mapToBase(decisions)); } /** * @description get protocol precense by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolPrecenseById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let presence = await ProtocolPresenceService.getAll(protocolId); res.json(ProtocolPresenceFactory.mapToBase(presence)); } /** * @description get protocol votings by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolVotingsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let votings = await ProtocolVotingService.getAll(protocolId); res.json(ProtocolVotingFactory.mapToBase(votings)); } /** * @description get protocol printouts by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolPrintoutsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let printouts = await ProtocolPrintoutService.getAll(protocolId); res.json(ProtocolPrintoutFactory.mapToBase(printouts)); } /** * @description get protocol printout by id and print * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function getProtocolPrintoutByIdAndPrint(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let printoutId = parseInt(req.params.printoutId); let printout = await ProtocolPrintoutService.getById(printoutId, protocolId); let filepath = FileSystemHelper.formatPath("protocol", printout.filename); res.sendFile(filepath, { headers: { "Content-Type": "application/pdf", }, }); } /** * @description create protocol * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function createProtocol(req: Request, res: Response): Promise { let title = req.body.title; let date = req.body.date; let createProtocol: CreateProtocolCommand = { title, date, }; let id = await ProtocolCommandHandler.create(createProtocol); res.send(id); } /** * @description create protocol agenda by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function createProtocolAgendaById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let agenda = await ProtocolAgendaCommandHandler.create(protocolId); res.send(agenda); } /** * @description create protocol decisions by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function createProtocolDecisonsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let decision = await ProtocolDecisionCommandHandler.create(protocolId); res.send(decision); } /** * @description create protocol votings by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function createProtocolVotingsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let voting = await ProtocolVotingCommandHandler.create(protocolId); res.send(voting); } /** * @description create protocol printout by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function createProtocolPrintoutById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let protocol = await ProtocolService.getById(protocolId); let agenda = await ProtocolAgendaService.getAll(protocolId); let decisions = await ProtocolDecisionService.getAll(protocolId); let presence = await ProtocolPresenceService.getAll(protocolId); let votings = await ProtocolVotingService.getAll(protocolId); let iteration = await ProtocolPrintoutService.getCount(protocolId); let title = `${protocol.title} - ${new Date(protocol.date).toLocaleDateString("de-DE", { day: "2-digit", month: "long", year: "numeric", })}`; let filename = `${new Date().toISOString().split("T")[0]}_${iteration + 1}_Protokoll_${protocol.title.replace( /[^a-zA-Z0-9]/g, "" )}`; await PdfExport.renderFile({ template: "protocol", title, filename, folder: "protocol", data: { title: protocol.title, summary: protocol.summary, iteration: iteration + 1, date: new Date(protocol.date).toLocaleDateString("de-DE", { weekday: "long", day: "2-digit", month: "2-digit", year: "numeric", }), start: protocol.starttime, end: protocol.endtime, agenda, decisions, presence: presence.filter((p) => !p.absent).map((p) => p.member), absent: presence.filter((p) => p.absent).map((p) => ({ ...p.member, excused: p.excused })), excused_absent: presence.filter((p) => p.absent && p.excused).map((p) => p.member), unexcused_absent: presence.filter((p) => p.absent && !p.excused).map((p) => p.member), votings, }, }); let printout: CreateProtocolPrintoutCommand = { title, iteration: iteration + 1, filename: `${filename}.pdf`, protocolId, }; await ProtocolPrintoutCommandHandler.create(printout); res.sendStatus(204); } /** * @description synchronize protocol by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function synchronizeProtocolById(req: Request, res: Response): Promise { let id = parseInt(req.params.id); let title = req.body.title; let date = req.body.date; let starttime = req.body.starttime; let endtime = req.body.endtime; let summary = req.body.summary; let syncProtocol: SynchronizeProtocolCommand = { id, title, date, starttime, endtime, summary, }; await ProtocolCommandHandler.sync(syncProtocol); res.sendStatus(204); } /** * @description synchronize protocol agenda by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function synchronizeProtocolAgendaById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let agenda = req.body.agenda as Array; let syncAgenda: Array = agenda.map( (a: ProtocolAgendaViewModel): SynchronizeProtocolAgendaCommand => ({ id: a.id ?? null, topic: a.topic, context: a.context, protocolId, }) ); await ProtocolAgendaCommandHandler.sync(syncAgenda); res.sendStatus(204); } /** * @description synchronize protocol decisions by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function synchronizeProtocolDecisonsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let decisions = req.body.decisions as Array; let syncDecision: Array = decisions.map( (d: ProtocolDecisionViewModel): SynchronizeProtocolDecisionCommand => ({ id: d.id ?? null, topic: d.topic, context: d.context, protocolId, }) ); await ProtocolDecisionCommandHandler.sync(syncDecision); res.sendStatus(204); } /** * @description synchronize protocol votings by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function synchronizeProtocolVotingsById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let votings = req.body.votings as Array; let syncVoting: Array = votings.map( (d: ProtocolVotingViewModel): SynchronizeProtocolVotingCommand => ({ id: d.id ?? null, topic: d.topic, context: d.context, favour: d.favour, abstain: d.abstain, against: d.abstain, protocolId, }) ); await ProtocolVotingCommandHandler.sync(syncVoting); res.sendStatus(204); } /** * @description synchronize protocol precense by id * @param req {Request} Express req object * @param res {Response} Express res object * @returns {Promise<*>} */ export async function synchronizeProtocolPrecenseById(req: Request, res: Response): Promise { let protocolId = parseInt(req.params.protocolId); let presence = req.body.presence as Array; let syncPresence: SynchronizeProtocolPresenceCommand = { members: presence.map((p) => ({ memberId: p.memberId, absent: p.absent, excused: p.excused, })), protocolId, }; await ProtocolPresenceCommandHandler.sync(syncPresence); res.sendStatus(204); }