Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merge Develop onto Main #745

Merged
merged 1 commit into from
Jan 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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';
}
}
Loading