From eb4db01b27f326d7e3c87956a34edbc0d003b48d Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Mon, 24 Mar 2025 09:19:11 +0100 Subject: [PATCH] hotfix: insert or update with postgres --- .../club/protocol/protocolAgendaCommand.ts | 4 ++-- .../protocol/protocolAgendaCommandHandler.ts | 17 ++++++++++------- .../club/protocol/protocolDecisionCommand.ts | 4 ++-- .../protocol/protocolDecisionCommandHandler.ts | 16 ++++++++++------ .../club/protocol/protocolVotingCommand.ts | 2 +- .../protocol/protocolVotingCommandHandler.ts | 16 ++++++++++------ .../newsletterConfigCommandHandler.ts | 2 +- src/controller/admin/club/protocolController.ts | 6 +++--- src/helpers/backupHelper.ts | 2 +- src/migrations/1738166167472-CreateSchema.ts | 2 +- .../1742549956787-templatesAndProtocolSort.ts | 4 ++-- 11 files changed, 43 insertions(+), 32 deletions(-) diff --git a/src/command/club/protocol/protocolAgendaCommand.ts b/src/command/club/protocol/protocolAgendaCommand.ts index 2889122..3654c47 100644 --- a/src/command/club/protocol/protocolAgendaCommand.ts +++ b/src/command/club/protocol/protocolAgendaCommand.ts @@ -1,7 +1,7 @@ export interface SynchronizeProtocolAgendaCommand { - id?: number; + id: number; topic: string; context: string; - sort?: number; + sort: number; protocolId: number; } diff --git a/src/command/club/protocol/protocolAgendaCommandHandler.ts b/src/command/club/protocol/protocolAgendaCommandHandler.ts index 96dbc08..73ce349 100644 --- a/src/command/club/protocol/protocolAgendaCommandHandler.ts +++ b/src/command/club/protocol/protocolAgendaCommandHandler.ts @@ -39,13 +39,16 @@ export default abstract class ProtocolAgendaCommandHandler { */ static async sync(syncProtocolAgenda: Array): Promise { return await dataSource - .createQueryBuilder() - .insert() - .into(protocolAgenda) - .values(syncProtocolAgenda) - .orUpdate(["topic", "context", "sort"], ["id"]) - .execute() - .then(() => {}) + .transaction(async (transactionalEntityManager) => { + for (const agenda of syncProtocolAgenda) { + await transactionalEntityManager + .createQueryBuilder() + .update(protocolAgenda) + .set(agenda) + .where({ id: agenda.id }) + .execute(); + } + }) .catch((err) => { throw new DatabaseActionException("SYNC", "protocolAgenda", err); }); diff --git a/src/command/club/protocol/protocolDecisionCommand.ts b/src/command/club/protocol/protocolDecisionCommand.ts index aeb2f24..254bff8 100644 --- a/src/command/club/protocol/protocolDecisionCommand.ts +++ b/src/command/club/protocol/protocolDecisionCommand.ts @@ -1,7 +1,7 @@ export interface SynchronizeProtocolDecisionCommand { - id?: number; + id: number; topic: string; context: string; - sort?: number; + sort: number; protocolId: number; } diff --git a/src/command/club/protocol/protocolDecisionCommandHandler.ts b/src/command/club/protocol/protocolDecisionCommandHandler.ts index 9425d38..c466188 100644 --- a/src/command/club/protocol/protocolDecisionCommandHandler.ts +++ b/src/command/club/protocol/protocolDecisionCommandHandler.ts @@ -38,12 +38,16 @@ export default abstract class ProtocolDecisionCommandHandler { */ static async sync(syncProtocolDecisions: Array): Promise { return await dataSource - .createQueryBuilder() - .insert() - .into(protocolDecision) - .values(syncProtocolDecisions) - .orUpdate(["topic", "context", "sort"], ["id"]) - .execute() + .transaction(async (transactionalEntityManager) => { + for (const decision of syncProtocolDecisions) { + await transactionalEntityManager + .createQueryBuilder() + .update(protocolDecision) + .set(decision) + .where({ id: decision.id }) + .execute(); + } + }) .then(() => {}) .catch((err) => { throw new DatabaseActionException("SYNC", "protocolDecision", err); diff --git a/src/command/club/protocol/protocolVotingCommand.ts b/src/command/club/protocol/protocolVotingCommand.ts index 617a560..111b0a7 100644 --- a/src/command/club/protocol/protocolVotingCommand.ts +++ b/src/command/club/protocol/protocolVotingCommand.ts @@ -5,6 +5,6 @@ export interface SynchronizeProtocolVotingCommand { favour: number; abstain: number; against: number; - sort?: number; + sort: number; protocolId: number; } diff --git a/src/command/club/protocol/protocolVotingCommandHandler.ts b/src/command/club/protocol/protocolVotingCommandHandler.ts index 741c0da..7a27cb6 100644 --- a/src/command/club/protocol/protocolVotingCommandHandler.ts +++ b/src/command/club/protocol/protocolVotingCommandHandler.ts @@ -38,12 +38,16 @@ export default abstract class ProtocolVotingCommandHandler { */ static async sync(syncProtocolVotings: Array): Promise { return await dataSource - .createQueryBuilder() - .insert() - .into(protocolVoting) - .values(syncProtocolVotings) - .orUpdate(["topic", "context", "favour", "abstain", "against", "sort"], ["id"]) - .execute() + .transaction(async (transactionalEntityManager) => { + for (const voting of syncProtocolVotings) { + await transactionalEntityManager + .createQueryBuilder() + .update(protocolVoting) + .set(voting) + .where({ id: voting.id }) + .execute(); + } + }) .then(() => {}) .catch((err) => { throw new DatabaseActionException("SYNC", "protocolVoting", err); diff --git a/src/command/configuration/newsletterConfig/newsletterConfigCommandHandler.ts b/src/command/configuration/newsletterConfig/newsletterConfigCommandHandler.ts index e9d3d2f..259d0cc 100644 --- a/src/command/configuration/newsletterConfig/newsletterConfigCommandHandler.ts +++ b/src/command/configuration/newsletterConfig/newsletterConfigCommandHandler.ts @@ -19,7 +19,7 @@ export default abstract class NewsletterConfigCommandHandler { comTypeId: setNewsletterConfig.comTypeId, config: setNewsletterConfig.config, }) - .orUpdate(["config"], "comTypeId") + .orUpdate(["config"], ["comTypeId"]) .execute() .then((result) => {}) .catch((err) => { diff --git a/src/controller/admin/club/protocolController.ts b/src/controller/admin/club/protocolController.ts index 2a6f5fb..ecae059 100644 --- a/src/controller/admin/club/protocolController.ts +++ b/src/controller/admin/club/protocolController.ts @@ -317,7 +317,7 @@ export async function synchronizeProtocolAgendaById(req: Request, res: Response) let syncAgenda: Array = agenda.map( (a: ProtocolAgendaViewModel): SynchronizeProtocolAgendaCommand => ({ - id: a.id ?? null, + id: a.id, topic: a.topic, context: a.context, sort: a.sort, @@ -341,7 +341,7 @@ export async function synchronizeProtocolDecisonsById(req: Request, res: Respons let syncDecision: Array = decisions.map( (d: ProtocolDecisionViewModel): SynchronizeProtocolDecisionCommand => ({ - id: d.id ?? null, + id: d.id, topic: d.topic, context: d.context, sort: d.sort, @@ -365,7 +365,7 @@ export async function synchronizeProtocolVotingsById(req: Request, res: Response let syncVoting: Array = votings.map( (v: ProtocolVotingViewModel): SynchronizeProtocolVotingCommand => ({ - id: v.id ?? null, + id: v.id, topic: v.topic, context: v.context, favour: v.favour, diff --git a/src/helpers/backupHelper.ts b/src/helpers/backupHelper.ts index 7727527..690a36f 100644 --- a/src/helpers/backupHelper.ts +++ b/src/helpers/backupHelper.ts @@ -748,7 +748,7 @@ export default abstract class BackupHelper { .insert() .into("template_usage") .values(data?.["template_usage"] ?? []) - .orUpdate(["headerId", "bodyId", "footerId", "headerHeight", "footerHeight"], ["scope"]) + .orIgnore() .execute(); } private static async setUser(data: { [key: string]: Array }): Promise { diff --git a/src/migrations/1738166167472-CreateSchema.ts b/src/migrations/1738166167472-CreateSchema.ts index 5dcd9db..df9c6fd 100644 --- a/src/migrations/1738166167472-CreateSchema.ts +++ b/src/migrations/1738166167472-CreateSchema.ts @@ -106,7 +106,7 @@ export class CreateSchema1738166167472 implements MigrationInterface { .insert() .into(templateUsage) .values([{ scope: "newsletter" }, { scope: "protocol" }, { scope: "member.list" }]) - .orUpdate(["headerId", "bodyId", "footerId", "headerHeight", "footerHeight"], ["scope"]) + .orIgnore() .execute(); await queryRunner.createTable(protocol_table, true, true, true); diff --git a/src/migrations/1742549956787-templatesAndProtocolSort.ts b/src/migrations/1742549956787-templatesAndProtocolSort.ts index 5036920..cc86bf8 100644 --- a/src/migrations/1742549956787-templatesAndProtocolSort.ts +++ b/src/migrations/1742549956787-templatesAndProtocolSort.ts @@ -11,7 +11,7 @@ export class TemplatesAndProtocolSort1742549956787 implements MigrationInterface .insert() .into(templateUsage) .values([{ scope: "member" }]) - .orUpdate(["headerId", "bodyId", "footerId", "headerHeight", "footerHeight"], ["scope"]) + .orIgnore() .execute(); await queryRunner.manager @@ -47,7 +47,7 @@ export class TemplatesAndProtocolSort1742549956787 implements MigrationInterface .insert() .into(templateUsage) .values([{ scope: "member.list" }]) - .orUpdate(["headerId", "bodyId", "footerId", "headerHeight", "footerHeight"], ["scope"]) + .orIgnore() .execute(); await queryRunner.manager.createQueryBuilder().delete().from(templateUsage).where({ scope: "member" }).execute();