From 475a13ce36915cc2a36bc917f817bfbe048537dd Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Fri, 11 Oct 2024 14:44:09 +0200 Subject: [PATCH] protcol table services --- src/controller/admin/protocolController.ts | 124 ++++++++++++++++++ src/data-source.ts | 10 +- src/entity/protocolAgenda.ts | 2 +- ...otocolDecisions.ts => protocolDecision.ts} | 4 +- .../{protocolVotings.ts => protocolVoting.ts} | 4 +- src/factory/admin/protocolAgenda.ts | 27 ++++ src/factory/admin/protocolDecision.ts | 27 ++++ src/factory/admin/protocolPresence.ts | 27 ++++ src/factory/admin/protocolVoting.ts | 30 +++++ .../1728645611919-protocolTableRename.ts | 15 +++ src/routes/admin/protocol.ts | 50 ++++++- src/service/protocolAgendaService.ts | 41 ++++++ src/service/protocolDecisionService.ts | 41 ++++++ src/service/protocolPrecenseService.ts | 41 ++++++ src/service/protocolVotingService.ts | 41 ++++++ src/viewmodel/admin/protocolAgenda.models.ts | 6 + .../admin/protocolDecision.models.ts | 6 + .../admin/protocolPresence.models.ts | 7 + src/viewmodel/admin/protocolVoting.models.ts | 9 ++ 19 files changed, 502 insertions(+), 10 deletions(-) rename src/entity/{protocolDecisions.ts => protocolDecision.ts} (90%) rename src/entity/{protocolVotings.ts => protocolVoting.ts} (93%) create mode 100644 src/factory/admin/protocolAgenda.ts create mode 100644 src/factory/admin/protocolDecision.ts create mode 100644 src/factory/admin/protocolPresence.ts create mode 100644 src/factory/admin/protocolVoting.ts create mode 100644 src/migrations/1728645611919-protocolTableRename.ts create mode 100644 src/service/protocolAgendaService.ts create mode 100644 src/service/protocolDecisionService.ts create mode 100644 src/service/protocolPrecenseService.ts create mode 100644 src/service/protocolVotingService.ts create mode 100644 src/viewmodel/admin/protocolAgenda.models.ts create mode 100644 src/viewmodel/admin/protocolDecision.models.ts create mode 100644 src/viewmodel/admin/protocolPresence.models.ts create mode 100644 src/viewmodel/admin/protocolVoting.models.ts diff --git a/src/controller/admin/protocolController.ts b/src/controller/admin/protocolController.ts index b94ac1f..53444a5 100644 --- a/src/controller/admin/protocolController.ts +++ b/src/controller/admin/protocolController.ts @@ -1,6 +1,14 @@ import { Request, Response } from "express"; import ProtocolService from "../../service/protocolService"; import ProtocolFactory from "../../factory/admin/protocol"; +import ProtocolAgendaService from "../../service/protocolAgendaService"; +import ProtocolAgendaFactory from "../../factory/admin/protocolAgenda"; +import ProtocolDecisionService from "../../service/protocolDecisionService"; +import ProtocolDecisionFactory from "../../factory/admin/protocolDecision"; +import ProtocolPresenceService from "../../service/protocolPrecenseService"; +import ProtocolPresenceFactory from "../../factory/admin/protocolPresence"; +import ProtocolVotingService from "../../service/protocolVotingService"; +import ProtocolVotingFactory from "../../factory/admin/protocolVoting"; /** * @description get all protocols @@ -33,3 +41,119 @@ export async function getProtocolById(req: Request, res: Response): Promise 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 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); + + 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); + + 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); + + 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); + + 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); + + res.sendStatus(204); +} diff --git a/src/data-source.ts b/src/data-source.ts index f3f08a0..f08af33 100644 --- a/src/data-source.ts +++ b/src/data-source.ts @@ -33,10 +33,11 @@ import { protocol } from "./entity/protocol"; import { ProtocolInit1727953803404 } from "./migrations/1727953803404-protocol-init"; import { ProtocolBase1728037129072 } from "./migrations/1728037129072-protocolBase"; import { protocolAgenda } from "./entity/protocolAgenda"; -import { protocolDecisions } from "./entity/protocolDecisions"; +import { protocolDecision } from "./entity/protocolDecision"; import { protocolPresence } from "./entity/protocolPresence"; -import { protocolVotings } from "./entity/protocolVotings"; +import { protocolVoting } from "./entity/protocolVoting"; import { ProtocolTables1728563204766 } from "./migrations/1728563204766-protocolTables"; +import { ProtocolTableRename1728645611919 } from "./migrations/1728645611919-protocolTableRename"; const dataSource = new DataSource({ type: DB_TYPE as any, @@ -68,9 +69,9 @@ const dataSource = new DataSource({ membership, protocol, protocolAgenda, - protocolDecisions, + protocolDecision, protocolPresence, - protocolVotings, + protocolVoting, ], migrations: [ Initial1724317398939, @@ -84,6 +85,7 @@ const dataSource = new DataSource({ ProtocolInit1727953803404, ProtocolBase1728037129072, ProtocolTables1728563204766, + ProtocolTableRename1728645611919, ], migrationsRun: true, migrationsTransactionMode: "each", diff --git a/src/entity/protocolAgenda.ts b/src/entity/protocolAgenda.ts index c3396c7..dcfbde4 100644 --- a/src/entity/protocolAgenda.ts +++ b/src/entity/protocolAgenda.ts @@ -4,7 +4,7 @@ import { protocol } from "./protocol"; @Entity() export class protocolAgenda { @PrimaryGeneratedColumn("increment") - id: string; + id: number; @Column({ type: "varchar", length: 255 }) topic: string; diff --git a/src/entity/protocolDecisions.ts b/src/entity/protocolDecision.ts similarity index 90% rename from src/entity/protocolDecisions.ts rename to src/entity/protocolDecision.ts index fed4e2e..b40d3aa 100644 --- a/src/entity/protocolDecisions.ts +++ b/src/entity/protocolDecision.ts @@ -2,9 +2,9 @@ import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; import { protocol } from "./protocol"; @Entity() -export class protocolDecisions { +export class protocolDecision { @PrimaryGeneratedColumn("increment") - id: string; + id: number; @Column({ type: "varchar", length: 255 }) topic: string; diff --git a/src/entity/protocolVotings.ts b/src/entity/protocolVoting.ts similarity index 93% rename from src/entity/protocolVotings.ts rename to src/entity/protocolVoting.ts index b2a03c7..4f3b461 100644 --- a/src/entity/protocolVotings.ts +++ b/src/entity/protocolVoting.ts @@ -2,9 +2,9 @@ import { Column, Entity, ManyToOne, PrimaryGeneratedColumn } from "typeorm"; import { protocol } from "./protocol"; @Entity() -export class protocolVotings { +export class protocolVoting { @PrimaryGeneratedColumn("increment") - id: string; + id: number; @Column({ type: "varchar", length: 255 }) topic: string; diff --git a/src/factory/admin/protocolAgenda.ts b/src/factory/admin/protocolAgenda.ts new file mode 100644 index 0000000..d48c0ef --- /dev/null +++ b/src/factory/admin/protocolAgenda.ts @@ -0,0 +1,27 @@ +import { protocolAgenda } from "../../entity/protocolAgenda"; +import { ProtocolAgendaViewModel } from "../../viewmodel/admin/protocolAgenda.models"; + +export default abstract class ProtocolAgendaFactory { + /** + * @description map record to protocolAgenda + * @param {protocol} record + * @returns {ProtocolAgendaViewModel} + */ + public static mapToSingle(record: protocolAgenda): ProtocolAgendaViewModel { + return { + id: record.id, + topic: record.topic, + context: record.context, + protocolId: record.protocol.id, + }; + } + + /** + * @description map records to protocolAgenda + * @param {Array} records + * @returns {Array} + */ + public static mapToBase(records: Array): Array { + return records.map((r) => this.mapToSingle(r)); + } +} diff --git a/src/factory/admin/protocolDecision.ts b/src/factory/admin/protocolDecision.ts new file mode 100644 index 0000000..a23f524 --- /dev/null +++ b/src/factory/admin/protocolDecision.ts @@ -0,0 +1,27 @@ +import { protocolDecision } from "../../entity/protocolDecision"; +import { ProtocolDecisionViewModel } from "../../viewmodel/admin/protocolDecision.models"; + +export default abstract class ProtocolDecisionFactory { + /** + * @description map record to protocolDecision + * @param {protocol} record + * @returns {ProtocolDecisionViewModel} + */ + public static mapToSingle(record: protocolDecision): ProtocolDecisionViewModel { + return { + id: record.id, + topic: record.topic, + context: record.context, + protocolId: record.protocol.id, + }; + } + + /** + * @description map records to protocolDecision + * @param {Array} records + * @returns {Array} + */ + public static mapToBase(records: Array): Array { + return records.map((r) => this.mapToSingle(r)); + } +} diff --git a/src/factory/admin/protocolPresence.ts b/src/factory/admin/protocolPresence.ts new file mode 100644 index 0000000..3772be8 --- /dev/null +++ b/src/factory/admin/protocolPresence.ts @@ -0,0 +1,27 @@ +import { protocolPresence } from "../../entity/protocolPresence"; +import { ProtocolPresenceViewModel } from "../../viewmodel/admin/protocolPresence.models"; +import MemberFactory from "./member"; + +export default abstract class ProtocolPresenceFactory { + /** + * @description map record to protocolPresence + * @param {protocol} record + * @returns {ProtocolPresenceViewModel} + */ + public static mapToSingle(record: protocolPresence): ProtocolPresenceViewModel { + return { + memberId: record.member.id, + member: MemberFactory.mapToSingle(record.member), + protocolId: record.protocol.id, + }; + } + + /** + * @description map records to protocolPresence + * @param {Array} records + * @returns {Array} + */ + public static mapToBase(records: Array): Array { + return records.map((r) => this.mapToSingle(r)); + } +} diff --git a/src/factory/admin/protocolVoting.ts b/src/factory/admin/protocolVoting.ts new file mode 100644 index 0000000..076fe59 --- /dev/null +++ b/src/factory/admin/protocolVoting.ts @@ -0,0 +1,30 @@ +import { protocolVoting } from "../../entity/protocolVoting"; +import { ProtocolVotingViewModel } from "../../viewmodel/admin/protocolVoting.models"; + +export default abstract class ProtocolVotingFactory { + /** + * @description map record to protocolVoting + * @param {protocol} record + * @returns {ProtocolVotingViewModel} + */ + public static mapToSingle(record: protocolVoting): ProtocolVotingViewModel { + return { + id: record.id, + topic: record.topic, + context: record.context, + favour: record.favour, + abstain: record.abstain, + against: record.against, + protocolId: record.protocol.id, + }; + } + + /** + * @description map records to protocolVoting + * @param {Array} records + * @returns {Array} + */ + public static mapToBase(records: Array): Array { + return records.map((r) => this.mapToSingle(r)); + } +} diff --git a/src/migrations/1728645611919-protocolTableRename.ts b/src/migrations/1728645611919-protocolTableRename.ts new file mode 100644 index 0000000..4a79c51 --- /dev/null +++ b/src/migrations/1728645611919-protocolTableRename.ts @@ -0,0 +1,15 @@ +import { MigrationInterface, QueryRunner } from "typeorm"; + +export class ProtocolTableRename1728645611919 implements MigrationInterface { + name = "ProtocolTableRename1728645611919"; + + public async up(queryRunner: QueryRunner): Promise { + await queryRunner.renameTable("protocol_decisions", "protocol_decision"); + await queryRunner.renameTable("protocol_votings", "protocol_voting"); + } + + public async down(queryRunner: QueryRunner): Promise { + await queryRunner.renameTable("protocol_decision", "protocol_decisions"); + await queryRunner.renameTable("protocol_voting", "protocol_votings"); + } +} diff --git a/src/routes/admin/protocol.ts b/src/routes/admin/protocol.ts index e353582..1870d50 100644 --- a/src/routes/admin/protocol.ts +++ b/src/routes/admin/protocol.ts @@ -1,5 +1,17 @@ import express, { Request, Response } from "express"; -import { getAllProtocols, getProtocolById } from "../../controller/admin/protocolController"; +import { + getAllProtocols, + getProtocolAgendaById, + getProtocolById, + getProtocolDecisonsById, + getProtocolPrecenseById, + getProtocolVotingsById, + synchronizeProtocolAgendaById, + synchronizeProtocolById, + synchronizeProtocolDecisonsById, + synchronizeProtocolPrecenseById, + synchronizeProtocolVotingsById, +} from "../../controller/admin/protocolController"; var router = express.Router({ mergeParams: true }); @@ -11,4 +23,40 @@ router.get("/:id", async (req: Request, res: Response) => { await getProtocolById(req, res); }); +router.get("/:protocolId/agenda", async (req: Request, res: Response) => { + await getProtocolAgendaById(req, res); +}); + +router.get("/:protocolId/decisions", async (req: Request, res: Response) => { + await getProtocolDecisonsById(req, res); +}); + +router.get("/:protocolId/presence", async (req: Request, res: Response) => { + await getProtocolPrecenseById(req, res); +}); + +router.get("/:protocolId/votings", async (req: Request, res: Response) => { + await getProtocolVotingsById(req, res); +}); + +router.get("/:id/synchronize", async (req: Request, res: Response) => { + await synchronizeProtocolById(req, res); +}); + +router.get("/:protocolId/synchronize/agenda", async (req: Request, res: Response) => { + await synchronizeProtocolAgendaById(req, res); +}); + +router.get("/:protocolId/synchronize/decisions", async (req: Request, res: Response) => { + await synchronizeProtocolDecisonsById(req, res); +}); + +router.get("/:protocolId/synchronize/presence", async (req: Request, res: Response) => { + await synchronizeProtocolPrecenseById(req, res); +}); + +router.get("/:protocolId/synchronize/votings", async (req: Request, res: Response) => { + await synchronizeProtocolVotingsById(req, res); +}); + export default router; diff --git a/src/service/protocolAgendaService.ts b/src/service/protocolAgendaService.ts new file mode 100644 index 0000000..4409d05 --- /dev/null +++ b/src/service/protocolAgendaService.ts @@ -0,0 +1,41 @@ +import { dataSource } from "../data-source"; +import { protocolAgenda } from "../entity/protocolAgenda"; +import InternalException from "../exceptions/internalException"; + +export default abstract class ProtocolAgendaService { + /** + * @description get all protocolAgendas + * @returns {Promise>} + */ + static async getAll(protocolId: number): Promise> { + return await dataSource + .getRepository(protocolAgenda) + .createQueryBuilder("protocolAgenda") + .where("protocolAgenda.protocolId = :protocolId", { protocolId }) + .getMany() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolAgendas not found", err); + }); + } + + /** + * @description get protocolAgenda by id + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(protocolAgenda) + .createQueryBuilder("protocolAgenda") + .where("protocolAgenda.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolAgenda not found by id", err); + }); + } +} diff --git a/src/service/protocolDecisionService.ts b/src/service/protocolDecisionService.ts new file mode 100644 index 0000000..b7a9051 --- /dev/null +++ b/src/service/protocolDecisionService.ts @@ -0,0 +1,41 @@ +import { dataSource } from "../data-source"; +import { protocolDecision } from "../entity/protocolDecision"; +import InternalException from "../exceptions/internalException"; + +export default abstract class ProtocolDecisionService { + /** + * @description get all protocolDecisionss + * @returns {Promise>} + */ + static async getAll(protocolId: number): Promise> { + return await dataSource + .getRepository(protocolDecision) + .createQueryBuilder("protocolDecisions") + .where("protocolAgenda.protocolId = :protocolId", { protocolId }) + .getMany() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolDecisions not found", err); + }); + } + + /** + * @description get protocolDecision by id + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(protocolDecision) + .createQueryBuilder("protocolDecisions") + .where("protocolDecisions.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolDecision not found by id", err); + }); + } +} diff --git a/src/service/protocolPrecenseService.ts b/src/service/protocolPrecenseService.ts new file mode 100644 index 0000000..bc32993 --- /dev/null +++ b/src/service/protocolPrecenseService.ts @@ -0,0 +1,41 @@ +import { dataSource } from "../data-source"; +import { protocolPresence } from "../entity/protocolPresence"; +import InternalException from "../exceptions/internalException"; + +export default abstract class ProtocolPresenceService { + /** + * @description get all protocolPresences + * @returns {Promise>} + */ + static async getAll(protocolId: number): Promise> { + return await dataSource + .getRepository(protocolPresence) + .createQueryBuilder("protocolPresence") + .where("protocolAgenda.protocolId = :protocolId", { protocolId }) + .getMany() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolPresence not found", err); + }); + } + + /** + * @description get protocolDecision by id + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(protocolPresence) + .createQueryBuilder("protocolPresence") + .where("protocolPresence.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolDecision not found by id", err); + }); + } +} diff --git a/src/service/protocolVotingService.ts b/src/service/protocolVotingService.ts new file mode 100644 index 0000000..4f806d9 --- /dev/null +++ b/src/service/protocolVotingService.ts @@ -0,0 +1,41 @@ +import { dataSource } from "../data-source"; +import { protocolVoting } from "../entity/protocolVoting"; +import InternalException from "../exceptions/internalException"; + +export default abstract class ProtocolVotingService { + /** + * @description get all protocolVotingss + * @returns {Promise>} + */ + static async getAll(protocolId: number): Promise> { + return await dataSource + .getRepository(protocolVoting) + .createQueryBuilder("protocolVotings") + .where("protocolAgenda.protocolId = :protocolId", { protocolId }) + .getMany() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolVotings not found", err); + }); + } + + /** + * @description get protocolVoting by id + * @returns {Promise} + */ + static async getById(id: number): Promise { + return await dataSource + .getRepository(protocolVoting) + .createQueryBuilder("protocolVotings") + .where("protocolVotings.id = :id", { id: id }) + .getOneOrFail() + .then((res) => { + return res; + }) + .catch((err) => { + throw new InternalException("protocolVoting not found by id", err); + }); + } +} diff --git a/src/viewmodel/admin/protocolAgenda.models.ts b/src/viewmodel/admin/protocolAgenda.models.ts new file mode 100644 index 0000000..3a59327 --- /dev/null +++ b/src/viewmodel/admin/protocolAgenda.models.ts @@ -0,0 +1,6 @@ +export interface ProtocolAgendaViewModel { + id: number; + topic: string; + context: string; + protocolId: number; +} diff --git a/src/viewmodel/admin/protocolDecision.models.ts b/src/viewmodel/admin/protocolDecision.models.ts new file mode 100644 index 0000000..4a7212c --- /dev/null +++ b/src/viewmodel/admin/protocolDecision.models.ts @@ -0,0 +1,6 @@ +export interface ProtocolDecisionViewModel { + id: number; + topic: string; + context: string; + protocolId: number; +} diff --git a/src/viewmodel/admin/protocolPresence.models.ts b/src/viewmodel/admin/protocolPresence.models.ts new file mode 100644 index 0000000..9c10f59 --- /dev/null +++ b/src/viewmodel/admin/protocolPresence.models.ts @@ -0,0 +1,7 @@ +import { MemberViewModel } from "./member.models"; + +export interface ProtocolPresenceViewModel { + memberId: number; + member: MemberViewModel; + protocolId: number; +} diff --git a/src/viewmodel/admin/protocolVoting.models.ts b/src/viewmodel/admin/protocolVoting.models.ts new file mode 100644 index 0000000..686f423 --- /dev/null +++ b/src/viewmodel/admin/protocolVoting.models.ts @@ -0,0 +1,9 @@ +export interface ProtocolVotingViewModel { + id: number; + topic: string; + context: string; + favour: number; + abstain: number; + against: number; + protocolId: number; +}