-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
c15195c
commit 53d8f20
Showing
4 changed files
with
81 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
import * as Brevo from "@getbrevo/brevo"; | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import { EmailCampaignScopeInterface } from "src/types"; | ||
|
||
import { BrevoModuleConfig } from "../config/brevo-module.config"; | ||
import { BREVO_MODULE_CONFIG } from "../config/brevo-module.constants"; | ||
import { handleBrevoError } from "./brevo-api.utils"; | ||
import { BrevoApiFolder } from "./dto/brevo-api-folder"; | ||
|
||
@Injectable() | ||
export class BrevoApiFoldersService { | ||
private readonly contactsApi: Brevo.ContactsApi; | ||
|
||
constructor(@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig) { | ||
this.contactsApi = new Brevo.ContactsApi(); | ||
} | ||
|
||
async *getAllBrevoFolders(scope: EmailCampaignScopeInterface): AsyncGenerator<BrevoApiFolder, void, undefined> { | ||
const apiKey = this.config.brevo.resolveConfig(scope).apiKey; | ||
this.contactsApi.setApiKey(Brevo.ContactsApiApiKeys.apiKey, apiKey); | ||
|
||
// Limit set by Brevo | ||
const limit = 50; | ||
let offset = 0; | ||
|
||
while (true) { | ||
try { | ||
const { response, body } = await this.contactsApi.getFolders(limit, offset); | ||
|
||
if (response.statusCode !== 200) { | ||
throw new Error("Failed to get folders"); | ||
} | ||
|
||
const folders = body.folders ?? []; | ||
if (folders.length === 0) { | ||
break; | ||
} | ||
|
||
yield* folders; | ||
|
||
offset += limit; | ||
} catch (error) { | ||
handleBrevoError(error); | ||
} | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
import { Field, ID, ObjectType } from "@nestjs/graphql"; | ||
|
||
@ObjectType() | ||
export class BrevoApiFolder { | ||
@Field(() => ID) | ||
id: number; | ||
|
||
@Field(() => String) | ||
name: string; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters