diff --git a/apps/cdd-backend/src/mailchimp/mailchimp.service.ts b/apps/cdd-backend/src/mailchimp/mailchimp.service.ts index 498cf5e..b7f14ab 100644 --- a/apps/cdd-backend/src/mailchimp/mailchimp.service.ts +++ b/apps/cdd-backend/src/mailchimp/mailchimp.service.ts @@ -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" } @@ -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'); @@ -82,8 +82,6 @@ export class MailchimpService { } - - private async addSubscriberToList( email: string, listId: string, @@ -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; } @@ -129,34 +127,10 @@ export class MailchimpService { tags.push({ name: this.devUpdatesTagName, status: 'active' }); } - const [tagsError, tagsResult] = await to(this.mailchimpClient.lists.updateListMemberTags(listId, subscriberHash, { tags })) + const [tagsError] = await to(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) - ); } }