From 6dcd650cade61c97e49617dba213cba5bf809802 Mon Sep 17 00:00:00 2001 From: Julian Krauser Date: Thu, 26 Dec 2024 12:34:31 +0100 Subject: [PATCH] update newsletter syncing commands and controller --- src/command/newsletterRecipientsCommand.ts | 7 +-- .../newsletterRecipientsCommandHandler.ts | 43 +++++-------------- src/controller/admin/newsletterController.ts | 7 +-- src/entity/newsletterRecipients.ts | 3 -- src/factory/admin/newsletter.ts | 1 + src/factory/admin/newsletterRecipients.ts | 1 - src/migrations/1735118780511-newsletter.ts | 1 - src/viewmodel/admin/newsletter.models.ts | 1 + .../admin/newsletterRecipients.models.ts | 1 - 9 files changed, 16 insertions(+), 49 deletions(-) diff --git a/src/command/newsletterRecipientsCommand.ts b/src/command/newsletterRecipientsCommand.ts index fdd8928..ec69c35 100644 --- a/src/command/newsletterRecipientsCommand.ts +++ b/src/command/newsletterRecipientsCommand.ts @@ -1,9 +1,4 @@ export interface SynchronizeNewsletterRecipientsCommand { newsletterId: number; - recipients: Array; -} - -export interface NewsletterRecipientCommand { - memberId: number; - addedManually: boolean; + recipients: Array; } diff --git a/src/command/newsletterRecipientsCommandHandler.ts b/src/command/newsletterRecipientsCommandHandler.ts index 0421c41..b321c71 100644 --- a/src/command/newsletterRecipientsCommandHandler.ts +++ b/src/command/newsletterRecipientsCommandHandler.ts @@ -2,7 +2,7 @@ import { DeleteResult, EntityManager, InsertResult, UpdateResult } from "typeorm import { dataSource } from "../data-source"; import InternalException from "../exceptions/internalException"; import NewsletterRecipientsService from "../service/newsletterRecipientsService"; -import { NewsletterRecipientCommand, SynchronizeNewsletterRecipientsCommand } from "./newsletterRecipientsCommand"; +import { SynchronizeNewsletterRecipientsCommand } from "./newsletterRecipientsCommand"; import { newsletterRecipients } from "../entity/newsletterRecipients"; export default abstract class NewsletterRecipientsCommandHandler { @@ -12,18 +12,17 @@ export default abstract class NewsletterRecipientsCommandHandler { * @returns {Promise} */ static async sync(syncNewsletterRecipients: SynchronizeNewsletterRecipientsCommand): Promise { - let currentRecipients = await NewsletterRecipientsService.getAll(syncNewsletterRecipients.newsletterId); + let currentRecipients = (await NewsletterRecipientsService.getAll(syncNewsletterRecipients.newsletterId)).map( + (r) => r.memberId + ); return await dataSource.manager .transaction(async (manager) => { let newRecipients = syncNewsletterRecipients.recipients.filter( - (r) => !currentRecipients.map((np) => np.memberId).includes(r.memberId) + (r) => !currentRecipients.map((np) => np).includes(r) ); let removeRecipients = currentRecipients.filter( - (r) => !syncNewsletterRecipients.recipients.map((np) => np.memberId).includes(r.memberId) - ); - let keptRecipients = currentRecipients.filter((r) => - syncNewsletterRecipients.recipients.map((np) => np.memberId).includes(r.memberId) + (r) => !syncNewsletterRecipients.recipients.map((np) => np).includes(r) ); if (newRecipients.length != 0) { @@ -33,10 +32,6 @@ export default abstract class NewsletterRecipientsCommandHandler { if (removeRecipients.length != 0) { await this.syncPresenceRemove(manager, syncNewsletterRecipients.newsletterId, removeRecipients); } - - for (const recipient of keptRecipients) { - await this.syncPresenceUpdate(manager, syncNewsletterRecipients.newsletterId, recipient); - } }) .then(() => {}) .catch((err) => { @@ -47,47 +42,31 @@ export default abstract class NewsletterRecipientsCommandHandler { private static async syncPresenceAdd( manager: EntityManager, newsletterId: number, - recipients: Array + recipients: Array ): Promise { return await manager .createQueryBuilder() .insert() .into(newsletterRecipients) .values( - recipients.map((d) => ({ - ...d, + recipients.map((r) => ({ + memberId: r, newsletterId: newsletterId, })) ) .execute(); } - private static async syncPresenceUpdate( - manager: EntityManager, - newsletterId: number, - recipient: NewsletterRecipientCommand - ): Promise { - return await manager - .createQueryBuilder() - .update(newsletterRecipients) - .set({ - addedManually: recipient.addedManually, - }) - .where("memberId = :memberId", { memberId: recipient.memberId }) - .andWhere("newsletterId = :newsletterId", { newsletterId }) - .execute(); - } - private static async syncPresenceRemove( manager: EntityManager, newsletterId: number, - recipients: Array + recipients: Array ): Promise { return await manager .createQueryBuilder() .delete() .from(newsletterRecipients) - .where("memberId IN (:...ids)", { ids: recipients.map((d) => d.memberId) }) + .where("memberId IN (:...ids)", { ids: recipients }) .andWhere("newsletterId = :newsletterId", { newsletterId }) .execute(); } diff --git a/src/controller/admin/newsletterController.ts b/src/controller/admin/newsletterController.ts index 11bf8c2..fa9f481 100644 --- a/src/controller/admin/newsletterController.ts +++ b/src/controller/admin/newsletterController.ts @@ -215,14 +215,11 @@ export async function synchronizeNewsletterDatesById(req: Request, res: Response */ export async function synchronizeNewsletterRecipientsById(req: Request, res: Response): Promise { let newsletterId = parseInt(req.params.newsletterId); - let recipients = req.body.recipients as Array; + let recipients = req.body.recipients as Array; let syncRecipients: SynchronizeNewsletterRecipientsCommand = { newsletterId, - recipients: recipients.map((r) => ({ - memberId: r.memberId, - addedManually: r.addedManually, - })), + recipients: recipients, }; await NewsletterRecipientsCommandHandler.sync(syncRecipients); diff --git a/src/entity/newsletterRecipients.ts b/src/entity/newsletterRecipients.ts index 1f62a5a..b57bce4 100644 --- a/src/entity/newsletterRecipients.ts +++ b/src/entity/newsletterRecipients.ts @@ -10,9 +10,6 @@ export class newsletterRecipients { @PrimaryColumn({ type: "int" }) memberId: number; - @Column({ type: "boolean", default: false }) - addedManually: boolean; - @ManyToOne(() => newsletter, (newsletter) => newsletter.recipients, { nullable: false, onDelete: "CASCADE", diff --git a/src/factory/admin/newsletter.ts b/src/factory/admin/newsletter.ts index 3dca651..d734564 100644 --- a/src/factory/admin/newsletter.ts +++ b/src/factory/admin/newsletter.ts @@ -17,6 +17,7 @@ export default abstract class NewsletterFactory { newsletterText: record.newsletterText, newsletterSignatur: record.newsletterSignatur, isSent: record.isSent, + recipientsByQueryId: record?.recipientsByQuery ? record.recipientsByQuery.id : null, recipientsByQuery: record?.recipientsByQuery ? QueryStoreFactory.mapToSingle(record.recipientsByQuery) : null, }; } diff --git a/src/factory/admin/newsletterRecipients.ts b/src/factory/admin/newsletterRecipients.ts index f9373ef..9a1e332 100644 --- a/src/factory/admin/newsletterRecipients.ts +++ b/src/factory/admin/newsletterRecipients.ts @@ -12,7 +12,6 @@ export default abstract class NewsletterRecipientsFactory { return { newsletterId: record.newsletterId, memberId: record.memberId, - addedManually: record.addedManually, member: MemberFactory.mapToSingle(record.member), }; } diff --git a/src/migrations/1735118780511-newsletter.ts b/src/migrations/1735118780511-newsletter.ts index 6308425..e9f79ea 100644 --- a/src/migrations/1735118780511-newsletter.ts +++ b/src/migrations/1735118780511-newsletter.ts @@ -26,7 +26,6 @@ export class Newsletter1735118780511 implements MigrationInterface { columns: [ { name: "newsletterId", type: variableType_int, isPrimary: true }, { name: "memberId", type: variableType_int, isPrimary: true }, - { name: "addedManually", type: "tinyint", default: "0" }, ], }), true diff --git a/src/viewmodel/admin/newsletter.models.ts b/src/viewmodel/admin/newsletter.models.ts index b68814f..cfd8b42 100644 --- a/src/viewmodel/admin/newsletter.models.ts +++ b/src/viewmodel/admin/newsletter.models.ts @@ -8,5 +8,6 @@ export interface NewsletterViewModel { newsletterText: string; newsletterSignatur: string; isSent: boolean; + recipientsByQueryId?: number; recipientsByQuery?: QueryStoreViewModel; } diff --git a/src/viewmodel/admin/newsletterRecipients.models.ts b/src/viewmodel/admin/newsletterRecipients.models.ts index 55160f9..a1030a7 100644 --- a/src/viewmodel/admin/newsletterRecipients.models.ts +++ b/src/viewmodel/admin/newsletterRecipients.models.ts @@ -3,6 +3,5 @@ import { MemberViewModel } from "./member.models"; export interface NewsletterRecipientsViewModel { newsletterId: number; memberId: number; - addedManually: boolean; member: MemberViewModel; }