protcol data commands

This commit is contained in:
Julian Krauser 2024-10-13 15:48:01 +02:00
parent 475a13ce36
commit b9b258a1f6
16 changed files with 335 additions and 10 deletions

View file

@ -0,0 +1,6 @@
export interface SynchronizeProtocolAgendaCommand {
id?: number;
topic: string;
context: string;
protocolId: number;
}

View file

@ -0,0 +1,25 @@
import { dataSource } from "../data-source";
import { protocolAgenda } from "../entity/protocolAgenda";
import InternalException from "../exceptions/internalException";
import { SynchronizeProtocolAgendaCommand } from "./protocolAgendaCommand";
export default abstract class ProtocolAgendaCommandHandler {
/**
* @description sync protocolAgenda
* @param {Array<SynchronizeProtocolAgendaCommand>}
* @returns {Promise<void>}
*/
static async sync(syncProtocolAgenda: Array<SynchronizeProtocolAgendaCommand>): Promise<void> {
return await dataSource
.createQueryBuilder()
.insert()
.into(protocolAgenda)
.values(syncProtocolAgenda)
.orUpdate(["topic", "context"], ["id"])
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed creating protocol", err);
});
}
}

View file

@ -0,0 +1,13 @@
export interface CreateProtocolCommand {
title: string;
date: Date;
}
export interface SynchronizeProtocolCommand {
id: number;
title: string;
date: Date;
starttime: Date;
endtime: Date;
summary: string;
}

View file

@ -0,0 +1,53 @@
import { dataSource } from "../data-source";
import { protocol } from "../entity/protocol";
import InternalException from "../exceptions/internalException";
import { CreateProtocolCommand, SynchronizeProtocolCommand } from "./protocolCommand";
export default abstract class ProtocolCommandHandler {
/**
* @description create protocol
* @param CreateProtocolCommand
* @returns {Promise<number>}
*/
static async create(createProtocol: CreateProtocolCommand): Promise<number> {
return await dataSource
.createQueryBuilder()
.insert()
.into(protocol)
.values({
title: createProtocol.title,
date: createProtocol.date,
})
.execute()
.then((result) => {
return result.identifiers[0].id;
})
.catch((err) => {
throw new InternalException("Failed creating protocol", err);
});
}
/**
* @description sync protocol
* @param SynchronizeProtocolCommand
* @returns {Promise<void>}
*/
static async sync(syncProtocol: SynchronizeProtocolCommand): Promise<void> {
return await dataSource
.createQueryBuilder()
.update(protocol)
.set({
title: syncProtocol.title,
date: syncProtocol.date,
starttime: syncProtocol.starttime,
endtime: syncProtocol.endtime,
summary: syncProtocol.summary,
})
.where("id = :id", { id: syncProtocol.id })
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed creating protocol", err);
});
}
}

View file

@ -0,0 +1,6 @@
export interface SynchronizeProtocolDecisionCommand {
id?: number;
topic: string;
context: string;
protocolId: number;
}

View file

@ -0,0 +1,25 @@
import { dataSource } from "../data-source";
import { protocolDecision } from "../entity/protocolDecision";
import InternalException from "../exceptions/internalException";
import { SynchronizeProtocolDecisionCommand } from "./protocolDecisionCommand";
export default abstract class ProtocolDecisionCommandHandler {
/**
* @description sync protocolDecision
* @param {Array<SynchronizeProtocolDecisionCommand>}
* @returns {Promise<void>}
*/
static async sync(syncProtocolDecisions: Array<SynchronizeProtocolDecisionCommand>): Promise<void> {
return await dataSource
.createQueryBuilder()
.insert()
.into(protocolDecision)
.values(syncProtocolDecisions)
.orUpdate(["topic", "context"], ["id"])
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed creating protocol", err);
});
}
}

View file

@ -0,0 +1,4 @@
export interface SynchronizeProtocolPresenceCommand {
memberIds: Array<number>;
protocolId: number;
}

View file

@ -0,0 +1,65 @@
import { DeleteResult, EntityManager, InsertResult } from "typeorm";
import { dataSource } from "../data-source";
import { protocolPresence } from "../entity/protocolPresence";
import InternalException from "../exceptions/internalException";
import ProtocolPresenceService from "../service/protocolPrecenseService";
import { SynchronizeProtocolPresenceCommand } from "./protocolPresenceCommand";
export default abstract class ProtocolPresenceCommandHandler {
/**
* @description sync protocolPresence
* @param {SynchronizeProtocolPresenceCommand}
* @returns {Promise<void>}
*/
static async sync(syncProtocolPresences: SynchronizeProtocolPresenceCommand): Promise<void> {
let currentPresence = (await ProtocolPresenceService.getAll(syncProtocolPresences.protocolId)).map(
(r) => r.memberId
);
return await dataSource.manager
.transaction(async (manager) => {
let newMembers = syncProtocolPresences.memberIds.filter((r) => !currentPresence.includes(r));
let removeMembers = currentPresence.filter((r) => !syncProtocolPresences.memberIds.includes(r));
await this.syncPresenceAdd(manager, syncProtocolPresences.protocolId, newMembers);
await this.syncPresenceRemove(manager, syncProtocolPresences.protocolId, removeMembers);
})
.then(() => {})
.catch((err) => {
throw new InternalException("Failed saving user roles", err);
});
}
private static async syncPresenceAdd(
manager: EntityManager,
protocolId: number,
memberIds: Array<number>
): Promise<InsertResult> {
return await manager
.createQueryBuilder()
.insert()
.into(protocolPresence)
.values(
memberIds.map((m) => ({
protocolId,
memberIds: m,
}))
)
.execute();
}
private static async syncPresenceRemove(
manager: EntityManager,
protocolId: number,
memberIds: Array<number>
): Promise<DeleteResult> {
return await manager
.createQueryBuilder()
.delete()
.from(protocolPresence)
.where("memberId IN (:...ids)", { ids: memberIds })
.andWhere("protcolId = :protocolId", { protocolId })
.execute();
}
}

View file

@ -0,0 +1,9 @@
export interface SynchronizeProtocolVotingCommand {
id: number;
topic: string;
context: string;
favour: number;
abstain: number;
against: number;
protocolId: number;
}

View file

@ -0,0 +1,25 @@
import { dataSource } from "../data-source";
import { protocolVoting } from "../entity/protocolVoting";
import InternalException from "../exceptions/internalException";
import { SynchronizeProtocolVotingCommand } from "./protocolVotingCommand";
export default abstract class ProtocolVotingCommandHandler {
/**
* @description sync protocolVoting
* @param {Array<SynchronizeProtocolVotingCommand>}
* @returns {Promise<void>}
*/
static async sync(syncProtocolVotings: Array<SynchronizeProtocolVotingCommand>): Promise<void> {
return await dataSource
.createQueryBuilder()
.insert()
.into(protocolVoting)
.values(syncProtocolVotings)
.orUpdate(["topic", "context", "favour", "abstain", "against"], ["id"])
.execute()
.then(() => {})
.catch((err) => {
throw new InternalException("Failed creating protocol", err);
});
}
}

View file

@ -9,6 +9,18 @@ import ProtocolPresenceService from "../../service/protocolPrecenseService";
import ProtocolPresenceFactory from "../../factory/admin/protocolPresence"; import ProtocolPresenceFactory from "../../factory/admin/protocolPresence";
import ProtocolVotingService from "../../service/protocolVotingService"; import ProtocolVotingService from "../../service/protocolVotingService";
import ProtocolVotingFactory from "../../factory/admin/protocolVoting"; import ProtocolVotingFactory from "../../factory/admin/protocolVoting";
import { CreateProtocolCommand, SynchronizeProtocolCommand } from "../../command/protocolCommand";
import ProtocolCommandHandler from "../../command/protocolCommandHandler";
import { SynchronizeProtocolAgendaCommand } from "../../command/protocolAgendaCommand";
import ProtocolAgendaCommandHandler from "../../command/protocolAgendaCommandHandler";
import { ProtocolAgendaViewModel } from "../../viewmodel/admin/protocolAgenda.models";
import ProtocolDecisionCommandHandler from "../../command/protocolDecisionCommandHandler";
import { ProtocolDecisionViewModel } from "../../viewmodel/admin/protocolDecision.models";
import ProtocolPresenceCommandHandler from "../../command/protocolPresenceCommandHandler";
import { SynchronizeProtocolPresenceCommand } from "../../command/protocolPresenceCommand";
import { SynchronizeProtocolDecisionCommand } from "../../command/protocolDecisionCommand";
import { SynchronizeProtocolVotingCommand } from "../../command/protocolVotingCommand";
import { ProtocolVotingViewModel } from "../../viewmodel/admin/protocolVoting.models";
/** /**
* @description get all protocols * @description get all protocols
@ -98,6 +110,25 @@ export async function getProtocolVotingsById(req: Request, res: Response): Promi
res.json(ProtocolVotingFactory.mapToBase(votings)); res.json(ProtocolVotingFactory.mapToBase(votings));
} }
/**
* @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<any> {
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 synchronize protocol by id * @description synchronize protocol by id
* @param req {Request} Express req object * @param req {Request} Express req object
@ -106,6 +137,21 @@ export async function getProtocolVotingsById(req: Request, res: Response): Promi
*/ */
export async function synchronizeProtocolById(req: Request, res: Response): Promise<any> { export async function synchronizeProtocolById(req: Request, res: Response): Promise<any> {
let id = parseInt(req.params.id); 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); res.sendStatus(204);
} }
@ -118,6 +164,17 @@ export async function synchronizeProtocolById(req: Request, res: Response): Prom
*/ */
export async function synchronizeProtocolAgendaById(req: Request, res: Response): Promise<any> { export async function synchronizeProtocolAgendaById(req: Request, res: Response): Promise<any> {
let protocolId = parseInt(req.params.protocolId); let protocolId = parseInt(req.params.protocolId);
let agenda = req.body.agenda as Array<ProtocolAgendaViewModel>;
let syncAgenda: Array<SynchronizeProtocolAgendaCommand> = agenda.map(
(a: ProtocolAgendaViewModel): SynchronizeProtocolAgendaCommand => ({
id: a.id ?? null,
topic: a.topic,
context: a.context,
protocolId,
})
);
await ProtocolAgendaCommandHandler.sync(syncAgenda);
res.sendStatus(204); res.sendStatus(204);
} }
@ -130,6 +187,17 @@ export async function synchronizeProtocolAgendaById(req: Request, res: Response)
*/ */
export async function synchronizeProtocolDecisonsById(req: Request, res: Response): Promise<any> { export async function synchronizeProtocolDecisonsById(req: Request, res: Response): Promise<any> {
let protocolId = parseInt(req.params.protocolId); let protocolId = parseInt(req.params.protocolId);
let decisions = req.body.decisions as Array<ProtocolDecisionViewModel>;
let syncDecision: Array<SynchronizeProtocolDecisionCommand> = decisions.map(
(d: ProtocolDecisionViewModel): SynchronizeProtocolDecisionCommand => ({
id: d.id ?? null,
topic: d.topic,
context: d.context,
protocolId,
})
);
await ProtocolDecisionCommandHandler.sync(syncDecision);
res.sendStatus(204); res.sendStatus(204);
} }
@ -142,6 +210,13 @@ export async function synchronizeProtocolDecisonsById(req: Request, res: Respons
*/ */
export async function synchronizeProtocolPrecenseById(req: Request, res: Response): Promise<any> { export async function synchronizeProtocolPrecenseById(req: Request, res: Response): Promise<any> {
let protocolId = parseInt(req.params.protocolId); let protocolId = parseInt(req.params.protocolId);
let presence = req.body.precense as Array<number>;
let syncPresence: SynchronizeProtocolPresenceCommand = {
memberIds: presence,
protocolId,
};
await ProtocolPresenceCommandHandler.sync(syncPresence);
res.sendStatus(204); res.sendStatus(204);
} }
@ -154,6 +229,20 @@ export async function synchronizeProtocolPrecenseById(req: Request, res: Respons
*/ */
export async function synchronizeProtocolVotingsById(req: Request, res: Response): Promise<any> { export async function synchronizeProtocolVotingsById(req: Request, res: Response): Promise<any> {
let protocolId = parseInt(req.params.protocolId); let protocolId = parseInt(req.params.protocolId);
let decisions = req.body.decisions as Array<ProtocolVotingViewModel>;
let syncDecision: Array<SynchronizeProtocolVotingCommand> = decisions.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 ProtocolDecisionCommandHandler.sync(syncDecision);
res.sendStatus(204); res.sendStatus(204);
} }

View file

@ -5,10 +5,10 @@ import { member } from "./member";
@Entity() @Entity()
export class protocolPresence { export class protocolPresence {
@PrimaryColumn() @PrimaryColumn()
memberId: string; memberId: number;
@PrimaryColumn() @PrimaryColumn()
protocolId: string; protocolId: number;
@ManyToOne(() => member, { @ManyToOne(() => member, {
nullable: false, nullable: false,

View file

@ -1,5 +1,6 @@
import express, { Request, Response } from "express"; import express, { Request, Response } from "express";
import { import {
createProtocol,
getAllProtocols, getAllProtocols,
getProtocolAgendaById, getProtocolAgendaById,
getProtocolById, getProtocolById,
@ -39,23 +40,27 @@ router.get("/:protocolId/votings", async (req: Request, res: Response) => {
await getProtocolVotingsById(req, res); await getProtocolVotingsById(req, res);
}); });
router.get("/:id/synchronize", async (req: Request, res: Response) => { router.post("/", async (req: Request, res: Response) => {
await createProtocol(req, res);
});
router.put("/:id/synchronize", async (req: Request, res: Response) => {
await synchronizeProtocolById(req, res); await synchronizeProtocolById(req, res);
}); });
router.get("/:protocolId/synchronize/agenda", async (req: Request, res: Response) => { router.put("/:protocolId/synchronize/agenda", async (req: Request, res: Response) => {
await synchronizeProtocolAgendaById(req, res); await synchronizeProtocolAgendaById(req, res);
}); });
router.get("/:protocolId/synchronize/decisions", async (req: Request, res: Response) => { router.put("/:protocolId/synchronize/decisions", async (req: Request, res: Response) => {
await synchronizeProtocolDecisonsById(req, res); await synchronizeProtocolDecisonsById(req, res);
}); });
router.get("/:protocolId/synchronize/presence", async (req: Request, res: Response) => { router.put("/:protocolId/synchronize/presence", async (req: Request, res: Response) => {
await synchronizeProtocolPrecenseById(req, res); await synchronizeProtocolPrecenseById(req, res);
}); });
router.get("/:protocolId/synchronize/votings", async (req: Request, res: Response) => { router.put("/:protocolId/synchronize/votings", async (req: Request, res: Response) => {
await synchronizeProtocolVotingsById(req, res); await synchronizeProtocolVotingsById(req, res);
}); });

View file

@ -11,7 +11,7 @@ export default abstract class ProtocolDecisionService {
return await dataSource return await dataSource
.getRepository(protocolDecision) .getRepository(protocolDecision)
.createQueryBuilder("protocolDecisions") .createQueryBuilder("protocolDecisions")
.where("protocolAgenda.protocolId = :protocolId", { protocolId }) .where("protocolDecisions.protocolId = :protocolId", { protocolId })
.getMany() .getMany()
.then((res) => { .then((res) => {
return res; return res;

View file

@ -11,7 +11,7 @@ export default abstract class ProtocolPresenceService {
return await dataSource return await dataSource
.getRepository(protocolPresence) .getRepository(protocolPresence)
.createQueryBuilder("protocolPresence") .createQueryBuilder("protocolPresence")
.where("protocolAgenda.protocolId = :protocolId", { protocolId }) .where("protocolPresence.protocolId = :protocolId", { protocolId })
.getMany() .getMany()
.then((res) => { .then((res) => {
return res; return res;

View file

@ -11,7 +11,7 @@ export default abstract class ProtocolVotingService {
return await dataSource return await dataSource
.getRepository(protocolVoting) .getRepository(protocolVoting)
.createQueryBuilder("protocolVotings") .createQueryBuilder("protocolVotings")
.where("protocolAgenda.protocolId = :protocolId", { protocolId }) .where("protocolVotings.protocolId = :protocolId", { protocolId })
.getMany() .getMany()
.then((res) => { .then((res) => {
return res; return res;