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

fix: tags not applied #83

Merged
merged 1 commit into from
Jul 20, 2023
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
2 changes: 1 addition & 1 deletion apps/cdd-backend/src/mailchimp/mailchimp.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ describe('MailchimpService', () => {
});

it('should return true when isEnabled is true and Mailchimp returns a successful response', async () => {
mockMailchimpClient.ping.get.mockResolvedValue({});
mockMailchimpClient.ping.get.mockResolvedValue({ health_status: "Everything's Chimpy!"});

const result = await service.ping();
expect(result).toBe(true);
Expand Down
38 changes: 6 additions & 32 deletions apps/cdd-backend/src/mailchimp/mailchimp.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HttpStatus, Inject, Injectable } from '@nestjs/common';
import { Inject, Injectable } from '@nestjs/common';
import { ConfigService } from '@nestjs/config';
import { WINSTON_MODULE_PROVIDER } from 'nest-winston';
import { Logger } from 'winston';
Expand All @@ -13,7 +13,7 @@ type MarketingPermission = {
enabled: boolean;
};

type MailchimpPingResponse = client.ping.APIHealthStatus | ErrorResponse
type MailchimpPingResponse = client.ping.APIHealthStatus
type MailchimpListResponse = ErrorResponse | client.lists.MembersSuccessResponse
type ListMemberTag = { name: string, status: 'active' | "inactive" }

Expand Down Expand Up @@ -61,7 +61,7 @@ export class MailchimpService {
throw new Error('Mailchimp Error');
}

if(result && this.isMailchimpErrorResponse(result)){
if(result && !result.health_status){
this.logger.error('Mailchimp Error', { error: result });

throw new Error('Mailchimp Error');
Expand All @@ -82,8 +82,6 @@ export class MailchimpService {
}




private async addSubscriberToList(
email: string,
listId: string,
Expand Down Expand Up @@ -113,8 +111,8 @@ export class MailchimpService {
return;
}

if (result && this.isMailchimpErrorResponse(result)) {
this.logger.error('Mailchimp Error', { error: result });
sansan marked this conversation as resolved.
Show resolved Hide resolved
if (result && result.status !== 'subscribed') {
this.logger.error('Mailchimp Error Adding Subscriber', result);

return;
}
Expand All @@ -129,34 +127,10 @@ export class MailchimpService {
tags.push({ name: this.devUpdatesTagName, status: 'active' });
}

const [tagsError, tagsResult] = await to<object>(this.mailchimpClient.lists.updateListMemberTags(listId, subscriberHash, { tags }))
const [tagsError] = await to<object>(this.mailchimpClient.lists.updateListMemberTags(listId, subscriberHash, { tags }))

if(tagsError){
this.logger.error('Mailchimp Error', { tagsError });
}

if (tagsResult && this.isMailchimpErrorResponse(tagsResult)) {
this.logger.error('Mailchimp Error', { error: tagsResult });
}

return;
}

private isMailchimpErrorResponse(
obj:
| client.ping.APIHealthStatus
| client.lists.MembersSuccessResponse
| ErrorResponse
| object
): obj is ErrorResponse {
return (
(obj as ErrorResponse).status !== undefined &&
![
HttpStatus.OK,
HttpStatus.CREATED,
HttpStatus.ACCEPTED,
HttpStatus.NO_CONTENT,
].includes((obj as ErrorResponse).status)
);
}
}
Loading