diff --git a/src/api/mailchimp/mailchimp-api.ts b/src/api/mailchimp/mailchimp-api.ts index cd3c9dbc..2d7e97b3 100644 --- a/src/api/mailchimp/mailchimp-api.ts +++ b/src/api/mailchimp/mailchimp-api.ts @@ -66,6 +66,36 @@ export const batchCreateMailchimpProfiles = async ( } }; +export const batchUpdateMailchimpProfiles = async ( + userProfiles: Partial[], +) => { + 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 => { try { diff --git a/src/service-user-profiles/service-user-profiles.service.ts b/src/service-user-profiles/service-user-profiles.service.ts index 0fa62a31..2952a02d 100644 --- a/src/service-user-profiles/service-user-profiles.service.ts +++ b/src/service-user-profiles/service-user-profiles.service.ts @@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common'; import { InjectRepository } from '@nestjs/typeorm'; import { batchCreateMailchimpProfiles, + batchUpdateMailchimpProfiles, createMailchimpMergeField, createMailchimpProfile, updateMailchimpProfile, @@ -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 { @@ -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()); diff --git a/src/user/user.controller.ts b/src/user/user.controller.ts index 30713ccb..32c5f565 100644 --- a/src/user/user.controller.ts +++ b/src/user/user.controller.ts @@ -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'; + } }