-
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.
Merge pull request #16 from vivid-planet/add-brevo-campaigns-api-to-m…
…ailings Add brevo campaigns api to email campaign
- Loading branch information
Showing
20 changed files
with
577 additions
and
53 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
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
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
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
118 changes: 118 additions & 0 deletions
118
packages/api/src/brevo-api/brevo-api-campaigns.service.ts
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,118 @@ | ||
import { Inject, Injectable } from "@nestjs/common"; | ||
import * as SibApiV3Sdk from "@sendinblue/client"; | ||
|
||
import { BrevoModuleConfig } from "../config/brevo-module.config"; | ||
import { BREVO_MODULE_CONFIG } from "../config/brevo-module.constants"; | ||
import { EmailCampaignInterface } from "../email-campaign/entities/email-campaign-entity.factory"; | ||
import { SendingState } from "../email-campaign/sending-state.enum"; | ||
import { BrevoApiCampaign } from "./dto/brevo-api-campaign"; | ||
import { BrevoApiCampaignStatistics } from "./dto/brevo-api-campaign-statistics"; | ||
|
||
@Injectable() | ||
export class BrevoApiCampaignsService { | ||
private readonly campaignsApi: SibApiV3Sdk.EmailCampaignsApi; | ||
|
||
constructor(@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig) { | ||
this.campaignsApi = new SibApiV3Sdk.EmailCampaignsApi(); | ||
this.campaignsApi.setApiKey(SibApiV3Sdk.EmailCampaignsApiApiKeys.apiKey, config.brevo.apiKey); | ||
} | ||
|
||
public getSendingInformationFromBrevoCampaign(campaign: BrevoApiCampaign): SendingState { | ||
if (campaign.status === "sent") { | ||
return SendingState.SENT; | ||
} else if (campaign.status === "queued" || campaign.status === "in_process") { | ||
return SendingState.SCHEDULED; | ||
} | ||
|
||
return SendingState.DRAFT; | ||
} | ||
|
||
public async createBrevoCampaign(campaign: EmailCampaignInterface, htmlContent: string, scheduledAt?: Date): Promise<number> { | ||
const emailCampaign = { | ||
name: campaign.title, | ||
subject: campaign.subject, | ||
sender: { name: this.config.brevo.sender.name, email: this.config.brevo.sender.email }, | ||
// TODO: add correct list after contact list/target groups are implemented | ||
recipients: { listIds: [2] }, | ||
htmlContent, | ||
scheduledAt: scheduledAt?.toISOString(), | ||
}; | ||
|
||
const data = await this.campaignsApi.createEmailCampaign(emailCampaign); | ||
return data.body.id; | ||
} | ||
|
||
public async updateBrevoCampaign(id: number, campaign: EmailCampaignInterface, htmlContent: string, scheduledAt?: Date): Promise<boolean> { | ||
const emailCampaign = { | ||
name: campaign.title, | ||
subject: campaign.subject, | ||
sender: { name: this.config.brevo.sender.name, email: this.config.brevo.sender.email }, | ||
// TODO: add correct list after contact list/target groups are implemented | ||
recipients: { listIds: [2] }, | ||
htmlContent, | ||
scheduledAt: scheduledAt?.toISOString(), | ||
}; | ||
|
||
const result = await this.campaignsApi.updateEmailCampaign(id, emailCampaign); | ||
return result.response.statusCode === 204; | ||
} | ||
|
||
public async sendBrevoCampaign(id: number): Promise<boolean> { | ||
const result = await this.campaignsApi.sendEmailCampaignNow(id); | ||
return result.response.statusCode === 204; | ||
} | ||
|
||
public async updateBrevoCampaignStatus(id: number, updated_status: SibApiV3Sdk.UpdateCampaignStatus.StatusEnum): Promise<boolean> { | ||
const status = new SibApiV3Sdk.UpdateCampaignStatus(); | ||
status.status = updated_status; | ||
const result = await this.campaignsApi.updateCampaignStatus(id, status); | ||
return result.response.statusCode === 204; | ||
} | ||
|
||
public async sendTestEmail(id: number, emails: string[]): Promise<boolean> { | ||
const result = await this.campaignsApi.sendTestEmail(id, { emailTo: emails }); | ||
return result.response.statusCode === 204; | ||
} | ||
|
||
public async loadBrevoCampaignsByIds(ids: number[]): Promise<BrevoApiCampaign[]> { | ||
const campaigns = []; | ||
for await (const campaign of await this.getCampaignsResponse(ids)) { | ||
campaigns.push(campaign); | ||
} | ||
|
||
return campaigns; | ||
} | ||
|
||
public async loadBrevoCampaignById(id: number): Promise<BrevoApiCampaign> { | ||
const response = await this.campaignsApi.getEmailCampaign(id); | ||
|
||
// wrong type in brevo library -> needs to be cast to unknown first | ||
return response.body as unknown as BrevoApiCampaign; | ||
} | ||
|
||
public async loadBrevoCampaignStatisticsById(id: number): Promise<BrevoApiCampaignStatistics> { | ||
const campaign = await this.campaignsApi.getEmailCampaign(id); | ||
|
||
return campaign.body.statistics.campaignStats[0]; | ||
} | ||
|
||
private async *getCampaignsResponse( | ||
ids: number[], | ||
status?: "suspended" | "archive" | "sent" | "queued" | "draft" | "inProcess", | ||
): AsyncGenerator<BrevoApiCampaign, void, undefined> { | ||
let offset = 0; | ||
const limit = 100; | ||
|
||
while (true) { | ||
const campaignsResponse = await this.campaignsApi.getEmailCampaigns(undefined, status, undefined, undefined, undefined, limit, offset); | ||
const campaignArray = (campaignsResponse.body.campaigns ?? []).filter((item) => ids.includes(item.id)); | ||
|
||
if (campaignArray.length === 0) { | ||
break; | ||
} | ||
yield* campaignArray; | ||
|
||
offset += limit; | ||
} | ||
} | ||
} |
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 |
---|---|---|
@@ -1,11 +1,12 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { ConfigModule } from "../config/config.module"; | ||
import { BrevoApiCampaignsService } from "./brevo-api-campaigns.service"; | ||
import { BrevoApiContactsService } from "./brevo-api-contact.service"; | ||
|
||
@Module({ | ||
imports: [ConfigModule], | ||
providers: [BrevoApiContactsService], | ||
exports: [BrevoApiContactsService], | ||
providers: [BrevoApiContactsService, BrevoApiCampaignsService], | ||
exports: [BrevoApiContactsService, BrevoApiCampaignsService], | ||
}) | ||
export class BrevoApiModule {} |
Oops, something went wrong.