From ad8f63532681fd1198d5bf2e80e0c70f612efbda Mon Sep 17 00:00:00 2001 From: Anna Hughes Date: Mon, 2 Dec 2024 17:42:12 +0000 Subject: [PATCH] fix: delete cypress mailchimp users (#711) --- src/api/mailchimp/mailchimp-api.ts | 34 +++++++++++++++++++++--------- src/crisp/crisp.service.ts | 8 ++++--- src/user/user.service.ts | 8 ++++++- 3 files changed, 36 insertions(+), 14 deletions(-) diff --git a/src/api/mailchimp/mailchimp-api.ts b/src/api/mailchimp/mailchimp-api.ts index d4aa2bac..06937b29 100644 --- a/src/api/mailchimp/mailchimp-api.ts +++ b/src/api/mailchimp/mailchimp-api.ts @@ -109,25 +109,39 @@ export const createMailchimpMergeField = async ( export const deleteMailchimpProfile = async (email: string) => { try { - return await mailchimp.lists.deleteListMember(mailchimpAudienceId, getEmailMD5Hash(email)); + return await mailchimp.lists.deleteListMemberPermanent( + mailchimpAudienceId, + getEmailMD5Hash(email), + ); } catch (error) { logger.warn(`Delete mailchimp profile API call failed: ${error}`); } }; export const deleteCypressMailchimpProfiles = async () => { - try { - const cypressProfiles = (await mailchimp.lists.getSegmentMembersList( - mailchimpAudienceId, - '5101590', - )) as { members: ListMember[] }; + let cypressProfiles: { members: ListMember[] }; - cypressProfiles.members.forEach(async (profile: ListMember) => { - deleteMailchimpProfile(profile.email_address); - }); + try { + cypressProfiles = (await mailchimp.lists.getSegmentMembersList(mailchimpAudienceId, '5046292', { + include_cleaned: true, + include_unsubscribed: true, + count: 200, + })) as { + members: ListMember[]; + }; } catch (error) { - throw new Error(`Delete cypress mailchimp profiles API call failed: ${error}`); + throw new Error(`Delete cypress mailchimp profiles API call failed to get users: ${error}`); } + + logger.log(`Deleting ${cypressProfiles.members.length} mailchimp profiles`); + + cypressProfiles.members.forEach(async (profile: ListMember) => { + try { + await deleteMailchimpProfile(profile.email_address); + } catch (error) { + throw new Error(`Delete cypress mailchimp profiles API call failed: ${error}`); + } + }); }; export const sendMailchimpUserEvent = async (email: string, event: MAILCHIMP_CUSTOM_EVENTS) => { diff --git a/src/crisp/crisp.service.ts b/src/crisp/crisp.service.ts index 4b03b4de..fa4ba7dc 100644 --- a/src/crisp/crisp.service.ts +++ b/src/crisp/crisp.service.ts @@ -122,7 +122,7 @@ export class CrispService { async deleteCypressCrispProfiles() { try { - const profiles = CrispClient.website.listPeopleProfiles( + const profiles = await CrispClient.website.listPeopleProfiles( crispWebsiteId, undefined, undefined, @@ -132,8 +132,10 @@ export class CrispService { 'cypresstestemail+', ); - profiles.data.data.forEach(async (profile) => { - CrispClient.website.removePeopleProfile(crispWebsiteId, profile.email); + console.log(`Deleting ${profiles.length} crisp profiles`); + + profiles?.forEach(async (profile) => { + await CrispClient.website.removePeopleProfile(crispWebsiteId, profile.email); }); } catch (error) { throw new Error(`Delete cypress crisp profiles API call failed: ${error}`); diff --git a/src/user/user.service.ts b/src/user/user.service.ts index 41e184ff..95ba8f76 100644 --- a/src/user/user.service.ts +++ b/src/user/user.service.ts @@ -1,6 +1,9 @@ import { HttpException, HttpStatus, Injectable } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; -import { deleteMailchimpProfile } from 'src/api/mailchimp/mailchimp-api'; +import { + deleteCypressMailchimpProfiles, + deleteMailchimpProfile, +} from 'src/api/mailchimp/mailchimp-api'; import { CrispService } from 'src/crisp/crisp.service'; import { PartnerAccessEntity } from 'src/entities/partner-access.entity'; import { PartnerEntity } from 'src/entities/partner.entity'; @@ -366,6 +369,9 @@ export class UserService { // Delete all remaining cypress firebase users (e.g. from failed user creations) await this.authService.deleteCypressFirebaseUsers(); + // Delete all remaining crisp accounts + await deleteCypressMailchimpProfiles(); + // Delete all remaining crisp accounts await this.crispService.deleteCypressCrispProfiles(); }