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

COM-1135: Fix brevo error handling #108

Merged
merged 4 commits into from
Oct 11, 2024
Merged
Show file tree
Hide file tree
Changes from 3 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
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
16 changes: 14 additions & 2 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,13 +108,18 @@ export class BrevoApiContactsService {
}
}

public async findContact(idOrEmail: string | number, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface> {
public async findContact(idOrEmail: string | number, scope: EmailCampaignScopeInterface): Promise<BrevoContactInterface | undefined> {
raphaelblum marked this conversation as resolved.
Show resolved Hide resolved
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 undefined;
}

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
Loading