Skip to content

Commit

Permalink
COM-1135: Fix brevo error handling (#108)
Browse files Browse the repository at this point in the history
## Description

Fix import failing due to error in brevo handeling. 
The import was causing an error because of nested try catch blocks and
adapting the brevo error message. Now the `findContact` function returns
`undefined` if no brevo contact was fround instead of an error.

## Changeset

[x ] I have verified if my change requires a changeset

## Related tasks and documents
COM-1135

---------

Co-authored-by: Denise Buder <[email protected]>
  • Loading branch information
RainbowBunchie and RainbowBunchie authored Oct 11, 2024
1 parent d865b3f commit 3c5b342
Show file tree
Hide file tree
Showing 5 changed files with 37 additions and 19 deletions.
7 changes: 7 additions & 0 deletions .changeset/large-mice-sort.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@comet/brevo-api": patch
---

Fix brevo error handling error

Fix brevo error handling causing contact import to fail if contact does not exists in brevo yet
22 changes: 17 additions & 5 deletions packages/api/src/brevo-api/brevo-api-contact.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,14 @@ export class BrevoApiContactsService {
try {
const idAsString = id.toString(); // brevo expects a string, because it can be an email or the id, so we have to transform the id to string
await this.getContactsApi(scope).updateContact(idAsString, { emailBlacklisted: blocked, attributes, listIds, unlinkListIds });
return this.findContact(id, scope);

const brevoContact = await this.findContact(id, scope);

if (!brevoContact) {
throw new Error(`The brevo contact with the id ${id} not found`);
}

return brevoContact;
} catch (error) {
handleBrevoError(error);
}
Expand All @@ -101,27 +108,32 @@ export class BrevoApiContactsService {
}
}

public async findContact(idOrEmail: string | number, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface> {
public async findContact(idOrEmail: string | number, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface | null> {
try {
const idAsString = String(idOrEmail); // brevo expects a string, because it can be an email or the id
const { body } = await this.getContactsApi(scope).getContactInfo(idAsString);

return body;
} catch (error) {
// Brevo returns a 404 error if no contact is found and a 400 error if an invalid email is provided.
if (isErrorFromBrevo(error) && (error.response.statusCode === 404 || error.response.statusCode === 400)) {
return null;
}

handleBrevoError(error);
}
}

public async getContactInfoByEmail(email: string, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface | undefined> {
public async getContactInfoByEmail(email: string, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface | null> {
try {
const data = await this.getContactsApi(scope).getContactInfo(email);
const contact = data.body;
if (!contact) return undefined;
if (!contact) return null;
return contact;
} catch (error) {
// Brevo returns a 404 error if no contact is found and a 400 error if an invalid email is provided.
if (isErrorFromBrevo(error) && (error.response.statusCode === 404 || error.response.statusCode === 400)) {
return undefined;
return null;
}
handleBrevoError(error);
}
Expand Down
15 changes: 2 additions & 13 deletions packages/api/src/brevo-contact/brevo-contact-import.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@ import isEqual from "lodash.isequal";
import { TargetGroupInterface } from "src/target-group/entity/target-group-entity.factory";
import { Readable } from "stream";

import { isErrorFromBrevo } from "../brevo-api/brevo-api.utils";
import { BrevoApiContactsService, CreateDoubleOptInContactData } from "../brevo-api/brevo-api-contact.service";
import { BrevoContactsService } from "../brevo-contact/brevo-contacts.service";
import { BrevoModuleConfig } from "../config/brevo-module.config";
Expand Down Expand Up @@ -129,18 +128,8 @@ export class BrevoContactImportService {
targetGroupBrevoIds: number[],
): Promise<"created" | "updated" | "error"> {
try {
let brevoContact;
try {
brevoContact = await this.brevoApiContactsService.findContact(contact.email, scope);
} catch (error) {
if (!isErrorFromBrevo(error)) {
throw error;
}
// Brevo throws 404 error if no contact was found
if (error.response.statusCode !== 404) {
throw error;
}
}
const brevoContact = await this.brevoApiContactsService.findContact(contact.email, scope);

const mainTargetGroupForScope = await this.targetGroupsService.createIfNotExistMainTargetGroupForScope(scope);
if (brevoContact && !brevoContact.emailBlacklisted) {
const updatedBrevoContact = await this.brevoApiContactsService.updateContact(
Expand Down
8 changes: 7 additions & 1 deletion packages/api/src/brevo-contact/brevo-contact.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,13 @@ export function createBrevoContactResolver({
@Args("id", { type: () => Int }) id: number,
@Args("scope", { type: () => Scope }, new DynamicDtoValidationPipe(Scope)) scope: typeof Scope,
): Promise<BrevoContactInterface> {
return this.brevoContactsApiService.findContact(id, scope);
const brevoContact = await this.brevoContactsApiService.findContact(id, scope);

if (!brevoContact) {
throw new Error(`Brevo contact with id ${id} not found`);
}

return brevoContact;
}

@Query(() => PaginatedBrevoContacts)
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/target-group/target-group.resolver.ts
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,10 @@ export function createTargetGroupsResolver({
throw new Error("No assigned contacts target group found");
}

if (!brevoContact) {
throw new Error(`Brevo contact with id ${input.brevoContactId} not found`);
}

const contactIsInTargetGroupByAttributes = this.targetGroupsService.checkIfContactIsInTargetGroup(
brevoContact.attributes,
targetGroup.filters,
Expand Down

0 comments on commit 3c5b342

Please sign in to comment.