#6-messages #24

Merged
jkeffects merged 16 commits from #6-messages into main 2024-12-31 13:25:12 +00:00
9 changed files with 16 additions and 49 deletions
Showing only changes of commit 6dcd650cad - Show all commits

View file

@ -1,9 +1,4 @@
export interface SynchronizeNewsletterRecipientsCommand {
newsletterId: number;
recipients: Array<NewsletterRecipientCommand>;
}
export interface NewsletterRecipientCommand {
memberId: number;
addedManually: boolean;
recipients: Array<number>;
}

View file

@ -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<void>}
*/
static async sync(syncNewsletterRecipients: SynchronizeNewsletterRecipientsCommand): Promise<void> {
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<NewsletterRecipientCommand>
recipients: Array<number>
): Promise<InsertResult> {
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<UpdateResult> {
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<NewsletterRecipientCommand>
recipients: Array<number>
): Promise<DeleteResult> {
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();
}

View file

@ -215,14 +215,11 @@ export async function synchronizeNewsletterDatesById(req: Request, res: Response
*/
export async function synchronizeNewsletterRecipientsById(req: Request, res: Response): Promise<any> {
let newsletterId = parseInt(req.params.newsletterId);
let recipients = req.body.recipients as Array<NewsletterRecipientsViewModel>;
let recipients = req.body.recipients as Array<number>;
let syncRecipients: SynchronizeNewsletterRecipientsCommand = {
newsletterId,
recipients: recipients.map((r) => ({
memberId: r.memberId,
addedManually: r.addedManually,
})),
recipients: recipients,
};
await NewsletterRecipientsCommandHandler.sync(syncRecipients);

View file

@ -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",

View file

@ -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,
};
}

View file

@ -12,7 +12,6 @@ export default abstract class NewsletterRecipientsFactory {
return {
newsletterId: record.newsletterId,
memberId: record.memberId,
addedManually: record.addedManually,
member: MemberFactory.mapToSingle(record.member),
};
}

View file

@ -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

View file

@ -8,5 +8,6 @@ export interface NewsletterViewModel {
newsletterText: string;
newsletterSignatur: string;
isSent: boolean;
recipientsByQueryId?: number;
recipientsByQuery?: QueryStoreViewModel;
}

View file

@ -3,6 +3,5 @@ import { MemberViewModel } from "./member.models";
export interface NewsletterRecipientsViewModel {
newsletterId: number;
memberId: number;
addedManually: boolean;
member: MemberViewModel;
}