Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[FIX] mail service checks #486

Merged
merged 1 commit into from
Jan 27, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion backend/src/mail/mail.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ class Context {
[key: string]: unknown;
}

class Contexts {
export class Contexts {
@ApiProperty({ required: false, default: 0 })
@IsNumber()
@IsOptional()
Expand Down
93 changes: 48 additions & 45 deletions backend/src/mail/mail.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import {
envs,
TemplateType,
} from './constants';
import { MailDto } from './mail.dto';
import { Contexts, MailDto } from './mail.dto';
import { MailRO } from './mail.ro';
import { Program } from '../auth/interface';
import { MailBatchEntity } from '../database/entities/mail-batch.entity';
Expand Down Expand Up @@ -174,6 +174,45 @@ export class MailService {
});
}

async checkForSentMail(contexts: Contexts[], templateType?: EmailTags) {
if (
[EmailTags.MEMBER_FOLLOW_UP, EmailTags.SUPERVISOR_REMINDER].includes(
templateType,
)
) {
this.logger.log(
'Filtering existing emails for automated notifications in the last 6 days',
);
const mailQB = this.mailRepository
.createQueryBuilder('mail')
.leftJoinAndSelect('mail.tx', 'tx');
mailQB
.where('mail.email IN (:...emails)', {
emails: contexts.map((itm) => itm.to[0]),
})
.andWhere('tx.template = :template', { template: templateType })
.andWhere('mail.date > :date', {
date: new Date(new Date().setDate(new Date().getDate() - 6)),
});

const existingMail = await mailQB.getMany();

const existingEmails = existingMail?.map((itm) => itm.email);

this.logger.log(
`Total existing emails to filter: ${existingEmails?.length}`,
);

const filteredContexts = contexts.filter(
(itm) => !existingEmails?.includes(itm.to[0]),
);

this.logger.log(
`Total emails to be sent (after filter existing): ${filteredContexts?.length}`,
);
return filteredContexts;
}
}
/**
* Send email, passing in the email data with body template and values
* @param mail MailDto
Expand Down Expand Up @@ -216,52 +255,16 @@ export class MailService {

this.logger.log(`Total invalid emails filtered: ${invalidEmails?.length}`);

if (
[EmailTags.MEMBER_FOLLOW_UP, EmailTags.SUPERVISOR_REMINDER].includes(
templateType,
)
) {
this.logger.log(
'Filtering existing emails for automated notifications in the last 6 days',
);
const mailQB = this.mailRepository
.createQueryBuilder('mail')
.leftJoinAndSelect('mail.tx', 'tx');
mailQB
.where('mail.email IN (:...emails)', {
emails: mail.contexts.map((itm) => itm.to[0]),
})
.andWhere('tx.tag = :tag', { tag: mail.contexts[0].tag })
.andWhere('mail.date > :date', {
date: new Date(new Date().setDate(new Date().getDate() - 6)),
});

const existingMail = await mailQB.getMany();

if (existingMail?.length > 0) {
const existingEmails = existingMail.map((itm) => itm.email);

this.logger.log(
`Total existing emails to filter: ${existingEmails?.length}`,
);

const filteredContexts = mail.contexts.filter(
(itm) => !existingEmails?.includes(itm.to[0]),
);

if (filteredContexts?.length > 0) {
mail.contexts = filteredContexts;
}
const alreadySentMail = await this.checkForSentMail(
mail.contexts,
templateType,
);

this.logger.log(
`Total emails to be sent (after filter existing): ${mail.contexts?.length}`,
);
}
mail.contexts = alreadySentMail;

if (mail.contexts?.length === 0) {
this.logger.log('No valid emails to send');
return;
}
if (mail.contexts?.length === 0) {
this.logger.log('No valid emails to send');
return;
}

try {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/recommitment/recommitment.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -270,7 +270,7 @@ export class RecommitmentService {
* @returns
*/
private chunkEmails(list: unknown[]): RecommitmentRO[][] {
const maxContexts = 30;
const maxContexts = 20;
return Object.values(
list.reduce((splitList, item, index) => {
const groupIndex = Math.floor(index / maxContexts);
Expand Down
Loading