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

feat: Add endpoint to send newsletter consent email #645

Merged
merged 12 commits into from
Sep 10, 2024

Conversation

sashko9807
Copy link
Member

Adds an endpoint to send newsletter consent mails.

  • Added a function to export contacts from Sendgrid list
  • Added a function to chunk sendgrid contacts list
  • Added a function to send mass emails which are scheduled in a minute difference.

Copy link

github-actions bot commented Jun 18, 2024

✅ Tests will run for this PR. Once they succeed it can be merged.

@sashko9807 sashko9807 added the run tests Allows running the tests workflows for forked repos label Jun 19, 2024
@github-actions github-actions bot removed the run tests Allows running the tests workflows for forked repos label Jun 19, 2024
}

private updateMailListMap(
regUser: Person[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

regUser -> regUsers

regUser: Person[],
contacts: ContactsMap,
skipAfterDate: Date,
unregisteredConsent: UnregisteredNotificationConsent[],
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

unregisteredConsent -> unregisteredConsents

// Remove email if it belongs to user created after the change has been deployed, as they had already decided
// whether to give consent or not.
if (contacts.get(registeredUser.email as string) && createdAt > skipAfterDate) {
Logger.debug(`Removing email ${registeredUser.email} from list`)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we add a bit more in the log:

removing email XXX from the list because of date

and same later below:

removing email XXX because the user don't want them.


do {
Logger.debug('Waiting export to be finished')
await new Promise((r) => setTimeout(r, SENDGRID_EXPORT_TIMEOUT))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't understand how this works... so we wait 10 seconds every time?
Seems very strange

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sendgrid doesn't have API to extract emails from list as of now, thus we need first to create export file, which takes approx. ~10-15 seconds.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I see

`Couldn't export contacts within the limit. Try again later.`,
)
}
const exportUrl = exportStatusResponse.urls[0]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can we check if the urls list is non empty?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will do, though if export status is succeeded it always returns an url.

const currentDate = new Date()
contactsMap.forEach((contacts, index) => {
//Schedule batches in a minute difference
currentDate.setMinutes(currentDate.getMinutes() + index)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This will result in current date with the following values (say it is now 11:00):

11:00
11:01
11:03
11:06
11:10
11:15
11:21
...

If I am reading this correctly.
It will probably work, I am just not sure this was the intent, especially if we expect this list to be big...

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Will have to revisit whether the scheduling part works as intended , but the idea is to schedule each call in a minute difference, due to Sendgrid Rate limiting. eg:
First batch of emails: 10:00
Second batch of emails: 10:01
Third batch of emails: 10:02

Big lists are not a problem since those emails are sent in bulk. What the implementation does is the following:
Get contacts data from list -> Split contact's data into chunks, with each chunk having up to 1000 emails -> Schedule mail send for the emails within each chunk.

Though thinking about it we should probably have a way to cancel the mail sent at some point.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Lets test this in staging/prod environment? We already have a private contact list, to test this internally first?

@sashko9807 sashko9807 added the run tests Allows running the tests workflows for forked repos label Jul 28, 2024
@github-actions github-actions bot removed the run tests Allows running the tests workflows for forked repos label Jul 28, 2024
@sashko9807 sashko9807 added the run tests Allows running the tests workflows for forked repos label Sep 2, 2024
@github-actions github-actions bot removed the run tests Allows running the tests workflows for forked repos label Sep 2, 2024
@sashko9807 sashko9807 merged commit c814b46 into podkrepi-bg:master Sep 10, 2024
10 of 11 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants