Skip to content

Commit

Permalink
Merge pull request #745 from chaynHQ/develop
Browse files Browse the repository at this point in the history
Merge Develop onto Main
  • Loading branch information
annarhughes authored Jan 6, 2025
2 parents f708deb + 8746e54 commit d2c7e34
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 1 deletion.
30 changes: 30 additions & 0 deletions src/api/mailchimp/mailchimp-api.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,36 @@ export const batchCreateMailchimpProfiles = async (
}
};

export const batchUpdateMailchimpProfiles = async (
userProfiles: Partial<UpdateListMemberRequest>[],
) => {
try {
const operations = [];

userProfiles.forEach((userProfile, index) => {
operations.push({
method: 'PATCH',
path: `/lists/${mailchimpAudienceId}/members/${getEmailMD5Hash(userProfile.email_address)}`,
operation_id: String(index),
body: JSON.stringify(userProfile),
});
});

const batchRequest = await mailchimp.batches.start({
operations: operations,
});
logger.log(`Mailchimp batch request: ${batchRequest}`);
logger.log('Wait 2 minutes before calling response...');

setTimeout(async () => {
const batchResponse = await mailchimp.batches.status(batchRequest.id);
logger.log(`Mailchimp batch response: ${batchResponse}`);
}, 120000);
} catch (error) {
throw new Error(`Batch update mailchimp profiles API call failed: ${JSON.stringify(error)}`);
}
};

// Note getMailchimpProfile is not currently used
export const getMailchimpProfile = async (email: string): Promise<ListMember> => {
try {
Expand Down
36 changes: 35 additions & 1 deletion src/service-user-profiles/service-user-profiles.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
import { InjectRepository } from '@nestjs/typeorm';
import {
batchCreateMailchimpProfiles,
batchUpdateMailchimpProfiles,
createMailchimpMergeField,
createMailchimpProfile,
updateMailchimpProfile,
Expand Down Expand Up @@ -234,7 +235,6 @@ export class ServiceUserProfilesService {
}

// Static bulk upload function to be used in specific cases e.g. bug prevented a subset of new users from being created
// Currently no endpoint for this function
// UPDATE THE FILTERS to the current requirements
public async bulkUploadMailchimpProfiles() {
try {
Expand Down Expand Up @@ -265,6 +265,40 @@ export class ServiceUserProfilesService {
throw new Error(`Bulk upload mailchimp profiles API call failed: ${error}`);
}
}
// Static bulk update function to be used in specific cases e.g. bug prevented a subset of users from being updated
// UPDATE THE FILTERS to the current requirements
public async bulkUpdateMailchimpProfiles() {
try {
const filterStartDate = '2024-10-29'; // UPDATE
const filterEndDate = '2025-01-06'; // UPDATE
const users = await this.userRepository.find({
where: {
// UPDATE TO ANY FILTERS
updatedAt: And(
Raw((alias) => `${alias} >= :filterStartDate`, { filterStartDate: filterStartDate }),
Raw((alias) => `${alias} < :filterEndDate`, { filterEndDate: filterEndDate }),
),
createdAt: Raw((alias) => `${alias} < :filterStartDate`, {
filterStartDate: filterStartDate,
}),
},
relations: {
partnerAccess: { partner: true, therapySession: true },
courseUser: { course: true, sessionUser: { session: true } },
},
});
const mailchimpUserProfiles = users.map((user) =>
this.createCompleteMailchimpUserProfile(user),
);

await batchUpdateMailchimpProfiles(mailchimpUserProfiles);
logger.log(
`Updated batch mailchimp profiles for ${users.length} users, updated before ${filterStartDate}`,
);
} catch (error) {
throw new Error(`Bulk update mailchimp profiles API call failed: ${error}`);
}
}

serializePartnersString(partnerAccesses: PartnerAccessEntity[]) {
const partnersNames = partnerAccesses?.map((pa) => pa.partner.name.toLowerCase());
Expand Down
8 changes: 8 additions & 0 deletions src/user/user.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -117,4 +117,12 @@ export class UserController {
await this.serviceUserProfilesService.bulkUploadMailchimpProfiles();
return 'ok';
}

@ApiBearerAuth()
@Get('/bulk-update-mailchimp-profiles')
@UseGuards(SuperAdminAuthGuard)
async bulkUpdateMailchimpProfiles() {
await this.serviceUserProfilesService.bulkUpdateMailchimpProfiles();
return 'ok';
}
}

0 comments on commit d2c7e34

Please sign in to comment.