From 496a60f2df39d50701a5bd31bb7bacd0146cb5ac Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Wed, 21 May 2025 10:33:05 +0200 Subject: [PATCH] enhance: enable deletion of protocol content --- .../protocol/protocolAgendaCommandHandler.ts | 18 +++++++++++++++-- .../protocolDecisionCommandHandler.ts | 20 +++++++++++++++++-- .../protocol/protocolVotingCommandHandler.ts | 17 ++++++++++++++-- .../admin/club/protocolController.ts | 6 +++--- 4 files changed, 52 insertions(+), 9 deletions(-) diff --git a/src/command/club/protocol/protocolAgendaCommandHandler.ts b/src/command/club/protocol/protocolAgendaCommandHandler.ts index 73ce349..190f288 100644 --- a/src/command/club/protocol/protocolAgendaCommandHandler.ts +++ b/src/command/club/protocol/protocolAgendaCommandHandler.ts @@ -34,18 +34,32 @@ export default abstract class ProtocolAgendaCommandHandler { /** * @description sync protocolAgenda + * @param {number} protocolId * @param {Array} syncProtocolAgenda * @returns {Promise} */ - static async sync(syncProtocolAgenda: Array): Promise { + static async sync(protocolId: number, syncProtocolAgenda: Array): Promise { + let currentAgenda = await ProtocolAgendaService.getAll(protocolId); return await dataSource .transaction(async (transactionalEntityManager) => { + let removed = currentAgenda.filter((ca) => !syncProtocolAgenda.some((spa) => spa.id == ca.id)); + for (const agenda of syncProtocolAgenda) { await transactionalEntityManager .createQueryBuilder() .update(protocolAgenda) .set(agenda) - .where({ id: agenda.id }) + .where({ id: agenda.id, protocolId }) + .execute(); + } + + if (removed.length != 0) { + await transactionalEntityManager + .createQueryBuilder() + .delete() + .from(protocolAgenda) + .where("id IN (:...ids)", { ids: removed.map((m) => m.id) }) + .andWhere({ protocolId }) .execute(); } }) diff --git a/src/command/club/protocol/protocolDecisionCommandHandler.ts b/src/command/club/protocol/protocolDecisionCommandHandler.ts index c466188..6e8b51c 100644 --- a/src/command/club/protocol/protocolDecisionCommandHandler.ts +++ b/src/command/club/protocol/protocolDecisionCommandHandler.ts @@ -33,12 +33,19 @@ export default abstract class ProtocolDecisionCommandHandler { } /** * @description sync protocolDecision + * @param {number} protocolId * @param {Array} syncProtocolDecisions * @returns {Promise} */ - static async sync(syncProtocolDecisions: Array): Promise { + static async sync( + protocolId: number, + syncProtocolDecisions: Array + ): Promise { + let currentDecision = await ProtocolDecisionService.getAll(protocolId); return await dataSource .transaction(async (transactionalEntityManager) => { + let removed = currentDecision.filter((ca) => !syncProtocolDecisions.some((spa) => spa.id == ca.id)); + for (const decision of syncProtocolDecisions) { await transactionalEntityManager .createQueryBuilder() @@ -47,8 +54,17 @@ export default abstract class ProtocolDecisionCommandHandler { .where({ id: decision.id }) .execute(); } + + if (removed.length != 0) { + await transactionalEntityManager + .createQueryBuilder() + .delete() + .from(protocolDecision) + .where("id IN (:...ids)", { ids: removed.map((m) => m.id) }) + .andWhere({ protocolId }) + .execute(); + } }) - .then(() => {}) .catch((err) => { throw new DatabaseActionException("SYNC", "protocolDecision", err); }); diff --git a/src/command/club/protocol/protocolVotingCommandHandler.ts b/src/command/club/protocol/protocolVotingCommandHandler.ts index 7a27cb6..a788ae5 100644 --- a/src/command/club/protocol/protocolVotingCommandHandler.ts +++ b/src/command/club/protocol/protocolVotingCommandHandler.ts @@ -33,12 +33,16 @@ export default abstract class ProtocolVotingCommandHandler { } /** * @description sync protocolVoting + * @param {number} protocolId * @param {Array} syncProtocolVotings * @returns {Promise} */ - static async sync(syncProtocolVotings: Array): Promise { + static async sync(protocolId: number, syncProtocolVotings: Array): Promise { + let currentVoting = await ProtocolVotingService.getAll(protocolId); return await dataSource .transaction(async (transactionalEntityManager) => { + let removed = currentVoting.filter((ca) => !syncProtocolVotings.some((spa) => spa.id == ca.id)); + for (const voting of syncProtocolVotings) { await transactionalEntityManager .createQueryBuilder() @@ -47,8 +51,17 @@ export default abstract class ProtocolVotingCommandHandler { .where({ id: voting.id }) .execute(); } + + if (removed.length != 0) { + await transactionalEntityManager + .createQueryBuilder() + .delete() + .from(protocolVoting) + .where("id IN (:...ids)", { ids: removed.map((m) => m.id) }) + .andWhere({ protocolId }) + .execute(); + } }) - .then(() => {}) .catch((err) => { throw new DatabaseActionException("SYNC", "protocolVoting", err); }); diff --git a/src/controller/admin/club/protocolController.ts b/src/controller/admin/club/protocolController.ts index 98396c8..02a1bab 100644 --- a/src/controller/admin/club/protocolController.ts +++ b/src/controller/admin/club/protocolController.ts @@ -324,7 +324,7 @@ export async function synchronizeProtocolAgendaById(req: Request, res: Response) protocolId, }) ); - await ProtocolAgendaCommandHandler.sync(syncAgenda); + await ProtocolAgendaCommandHandler.sync(protocolId, syncAgenda); res.sendStatus(204); } @@ -348,7 +348,7 @@ export async function synchronizeProtocolDecisonsById(req: Request, res: Respons protocolId, }) ); - await ProtocolDecisionCommandHandler.sync(syncDecision); + await ProtocolDecisionCommandHandler.sync(protocolId, syncDecision); res.sendStatus(204); } @@ -375,7 +375,7 @@ export async function synchronizeProtocolVotingsById(req: Request, res: Response protocolId, }) ); - await ProtocolVotingCommandHandler.sync(syncVoting); + await ProtocolVotingCommandHandler.sync(protocolId, syncVoting); res.sendStatus(204); }