-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
COM-687 Add automatic deletion process (#59)
* add job for deleting blacklistedContacts * add changeset * fix prettier error * refactor findNonMainTargetGroups to findMainTargetGroups including where parameter * add where parameter to brevo-contacts-services functions * use findMainTargetGroups function to get scopes for deleting contacts * fix prettier problems * fix prettier problems * add @CreateRequestContext to execute function * fix pipeline * correct scope by using dynamic values * rename variable numberOfBlacklistedContacts to hasMoreContacts * correct condition to check if more contacts are avaiable * rename findMainTargetGroups to findTargetGroups * correct type of where in findTargetGroups * use scope directly * fix lint error * adapt changeset to only include information that is relevant for the application * remove scope from findTargetGroups and pass it in the prop instead * correct hasMoreContacts * rename DeleteUnsubscribedContactsConsole to DeleteUnsubscribedBrevoContactsConsole --------- Co-authored-by: Julia Wegmayr <[email protected]>
- Loading branch information
1 parent
e56d830
commit d21db92
Showing
6 changed files
with
88 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
--- | ||
"@comet/brevo-api": minor | ||
--- | ||
|
||
Add `DeleteUnsubscribedBrevoContactsConsole` job to enable the deletion of blocklisted contacts. This job can be utilized as a cronjob to periodically clean up the blocklisted contacts. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,49 @@ | ||
import { CreateRequestContext, MikroORM } from "@mikro-orm/core"; | ||
import { Injectable } from "@nestjs/common"; | ||
import { Command, Console } from "nestjs-console"; | ||
|
||
import { BrevoApiContactsService } from "../brevo-api/brevo-api-contact.service"; | ||
import { TargetGroupsService } from "../target-group/target-groups.service"; | ||
|
||
@Injectable() | ||
@Console() | ||
export class DeleteUnsubscribedBrevoContactsConsole { | ||
constructor( | ||
private readonly brevoApiContactsService: BrevoApiContactsService, | ||
private readonly targetGroupsService: TargetGroupsService, | ||
private readonly orm: MikroORM, | ||
) {} | ||
|
||
@Command({ | ||
command: "delete-unsubscribed-brevo-contacts", | ||
description: "deletes unsubscribed contacts", | ||
}) | ||
@CreateRequestContext() | ||
async execute(): Promise<void> { | ||
const offset = 0; | ||
const limit = 50; | ||
const where = { isMainList: true }; | ||
|
||
const [targetGroups] = await this.targetGroupsService.findTargetGroups({ offset, limit, where }); | ||
|
||
for (const targetGroup of targetGroups) { | ||
let hasMoreContacts = false; | ||
let offset = 0; | ||
|
||
do { | ||
const contacts = await this.brevoApiContactsService.findContacts(limit, offset, { | ||
scope: targetGroup.scope, | ||
}); | ||
|
||
const blacklistedContacts = contacts.filter((contact) => contact.emailBlacklisted === true); | ||
|
||
if (blacklistedContacts.length > 0) { | ||
await this.brevoApiContactsService.deleteContacts(blacklistedContacts, { scope: targetGroup.scope }); | ||
} | ||
|
||
hasMoreContacts = !(contacts.length < limit); | ||
offset += limit; | ||
} while (hasMoreContacts); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters