Skip to content

Commit

Permalink
fix: tags not applied
Browse files Browse the repository at this point in the history
  • Loading branch information
sansan committed Jul 20, 2023
1 parent 266e280 commit 3c52cf8
Showing 1 changed file with 5 additions and 31 deletions.
36 changes: 5 additions & 31 deletions apps/cdd-backend/src/mailchimp/mailchimp.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
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)
);
}
}

0 comments on commit 3c52cf8

Please sign in to comment.