Skip to content

Commit d2c7e34

Browse files
authored
Merge pull request #745 from chaynHQ/develop
Merge Develop onto Main
2 parents f708deb + 8746e54 commit d2c7e34

File tree

3 files changed

+73
-1
lines changed

3 files changed

+73
-1
lines changed

src/api/mailchimp/mailchimp-api.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,36 @@ export const batchCreateMailchimpProfiles = async (
6666
}
6767
};
6868

69+
export const batchUpdateMailchimpProfiles = async (
70+
userProfiles: Partial<UpdateListMemberRequest>[],
71+
) => {
72+
try {
73+
const operations = [];
74+
75+
userProfiles.forEach((userProfile, index) => {
76+
operations.push({
77+
method: 'PATCH',
78+
path: `/lists/${mailchimpAudienceId}/members/${getEmailMD5Hash(userProfile.email_address)}`,
79+
operation_id: String(index),
80+
body: JSON.stringify(userProfile),
81+
});
82+
});
83+
84+
const batchRequest = await mailchimp.batches.start({
85+
operations: operations,
86+
});
87+
logger.log(`Mailchimp batch request: ${batchRequest}`);
88+
logger.log('Wait 2 minutes before calling response...');
89+
90+
setTimeout(async () => {
91+
const batchResponse = await mailchimp.batches.status(batchRequest.id);
92+
logger.log(`Mailchimp batch response: ${batchResponse}`);
93+
}, 120000);
94+
} catch (error) {
95+
throw new Error(`Batch update mailchimp profiles API call failed: ${JSON.stringify(error)}`);
96+
}
97+
};
98+
6999
// Note getMailchimpProfile is not currently used
70100
export const getMailchimpProfile = async (email: string): Promise<ListMember> => {
71101
try {

src/service-user-profiles/service-user-profiles.service.ts

Lines changed: 35 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { Injectable, Logger } from '@nestjs/common';
22
import { InjectRepository } from '@nestjs/typeorm';
33
import {
44
batchCreateMailchimpProfiles,
5+
batchUpdateMailchimpProfiles,
56
createMailchimpMergeField,
67
createMailchimpProfile,
78
updateMailchimpProfile,
@@ -234,7 +235,6 @@ export class ServiceUserProfilesService {
234235
}
235236

236237
// Static bulk upload function to be used in specific cases e.g. bug prevented a subset of new users from being created
237-
// Currently no endpoint for this function
238238
// UPDATE THE FILTERS to the current requirements
239239
public async bulkUploadMailchimpProfiles() {
240240
try {
@@ -265,6 +265,40 @@ export class ServiceUserProfilesService {
265265
throw new Error(`Bulk upload mailchimp profiles API call failed: ${error}`);
266266
}
267267
}
268+
// Static bulk update function to be used in specific cases e.g. bug prevented a subset of users from being updated
269+
// UPDATE THE FILTERS to the current requirements
270+
public async bulkUpdateMailchimpProfiles() {
271+
try {
272+
const filterStartDate = '2024-10-29'; // UPDATE
273+
const filterEndDate = '2025-01-06'; // UPDATE
274+
const users = await this.userRepository.find({
275+
where: {
276+
// UPDATE TO ANY FILTERS
277+
updatedAt: And(
278+
Raw((alias) => `${alias} >= :filterStartDate`, { filterStartDate: filterStartDate }),
279+
Raw((alias) => `${alias} < :filterEndDate`, { filterEndDate: filterEndDate }),
280+
),
281+
createdAt: Raw((alias) => `${alias} < :filterStartDate`, {
282+
filterStartDate: filterStartDate,
283+
}),
284+
},
285+
relations: {
286+
partnerAccess: { partner: true, therapySession: true },
287+
courseUser: { course: true, sessionUser: { session: true } },
288+
},
289+
});
290+
const mailchimpUserProfiles = users.map((user) =>
291+
this.createCompleteMailchimpUserProfile(user),
292+
);
293+
294+
await batchUpdateMailchimpProfiles(mailchimpUserProfiles);
295+
logger.log(
296+
`Updated batch mailchimp profiles for ${users.length} users, updated before ${filterStartDate}`,
297+
);
298+
} catch (error) {
299+
throw new Error(`Bulk update mailchimp profiles API call failed: ${error}`);
300+
}
301+
}
268302

269303
serializePartnersString(partnerAccesses: PartnerAccessEntity[]) {
270304
const partnersNames = partnerAccesses?.map((pa) => pa.partner.name.toLowerCase());

src/user/user.controller.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -117,4 +117,12 @@ export class UserController {
117117
await this.serviceUserProfilesService.bulkUploadMailchimpProfiles();
118118
return 'ok';
119119
}
120+
121+
@ApiBearerAuth()
122+
@Get('/bulk-update-mailchimp-profiles')
123+
@UseGuards(SuperAdminAuthGuard)
124+
async bulkUpdateMailchimpProfiles() {
125+
await this.serviceUserProfilesService.bulkUpdateMailchimpProfiles();
126+
return 'ok';
127+
}
120128
}

0 commit comments

Comments
 (0)