Skip to content

Commit

Permalink
fix: delete cypress mailchimp users (#711)
Browse files Browse the repository at this point in the history
  • Loading branch information
annarhughes authored Dec 2, 2024
1 parent c8870ad commit ad8f635
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
34 changes: 24 additions & 10 deletions src/api/mailchimp/mailchimp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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) => {
Expand Down
8 changes: 5 additions & 3 deletions src/crisp/crisp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ export class CrispService {

async deleteCypressCrispProfiles() {
try {
const profiles = CrispClient.website.listPeopleProfiles(
const profiles = await CrispClient.website.listPeopleProfiles(
crispWebsiteId,
undefined,
undefined,
Expand All @@ -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}`);
Expand Down
8 changes: 7 additions & 1 deletion src/user/user.service.ts
Original file line number Diff line number Diff line change
@@ -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';
Expand Down Expand Up @@ -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();
}
Expand Down

0 comments on commit ad8f635

Please sign in to comment.