-
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.
Add transactional mail service (#67)
* add brevo transactional mail service - also add controller route in demo to show an example usage * Add changelog * Fix wrong naming due to copy paste error * remove unnecessary line in changeset * Simplify code
- Loading branch information
1 parent
3fac174
commit 3f3fdeb
Showing
9 changed files
with
101 additions
and
3 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,15 @@ | ||
--- | ||
"@comet/brevo-api": minor | ||
--- | ||
|
||
Add and export `BrevoTransactionalMailsService` that can be used in the application for sending transactional mails. | ||
|
||
**Example Usage of `BrevoTransactionalMailsService`** | ||
|
||
```typescript | ||
constructor(private readonly brevoTransactionalMailsService: BrevoTransactionalMailsService) {} | ||
|
||
async send(email: string, htmlContent: string, subject: string): Promise<void> { | ||
await this.brevoTransactionalMailsService.send({ to: [{ email }], htmlContent, subject }, data.scope); | ||
} | ||
``` |
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
16 changes: 16 additions & 0 deletions
16
demo/api/src/brevo-transactional-mails/brevo-transactional-mails.controller.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,16 @@ | ||
import { BrevoTransactionalMailsService } from "@comet/brevo-api"; | ||
import { DisableGlobalGuard } from "@comet/cms-api"; | ||
import { Body, Controller, Post } from "@nestjs/common"; | ||
|
||
import { BrevoTransactionalMailsBody } from "./dto/transactional-mails.body"; | ||
|
||
@Controller("transactional-mails") | ||
export class BrevoTransactionalMailsController { | ||
constructor(private readonly brevoTransactionalMailsService: BrevoTransactionalMailsService) {} | ||
|
||
@DisableGlobalGuard() | ||
@Post(`/send`) | ||
async send(@Body() { text, subject, to, scope }: BrevoTransactionalMailsBody): Promise<void> { | ||
await this.brevoTransactionalMailsService.send({ to: [{ email: to }], textContent: text, subject }, scope); | ||
} | ||
} |
8 changes: 8 additions & 0 deletions
8
demo/api/src/brevo-transactional-mails/brevo-transactional-mails.module.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,8 @@ | ||
import { Module } from "@nestjs/common"; | ||
|
||
import { BrevoTransactionalMailsController } from "./brevo-transactional-mails.controller"; | ||
|
||
@Module({ | ||
controllers: [BrevoTransactionalMailsController], | ||
}) | ||
export class BrevoTransactionalMailsModule {} |
19 changes: 19 additions & 0 deletions
19
demo/api/src/brevo-transactional-mails/dto/transactional-mails.body.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,19 @@ | ||
import { EmailContactSubscribeScope } from "@src/brevo-contact/dto/brevo-contact-subscribe.scope"; | ||
import { Type } from "class-transformer"; | ||
import { IsEmail, IsNotEmpty, IsString, ValidateNested } from "class-validator"; | ||
|
||
export class BrevoTransactionalMailsBody { | ||
@IsString() | ||
subject: string; | ||
|
||
@IsString() | ||
text: string; | ||
|
||
@IsEmail() | ||
to: string; | ||
|
||
@ValidateNested() | ||
@Type(() => EmailContactSubscribeScope) | ||
@IsNotEmpty() | ||
scope: EmailContactSubscribeScope; | ||
} |
36 changes: 36 additions & 0 deletions
36
packages/api/src/brevo-api/brevo-api-transactional-mails.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,36 @@ | ||
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"; | ||
|
||
type SendTransacEmailResponse = ReturnType<Brevo.TransactionalEmailsApi["sendTransacEmail"]>; | ||
|
||
@Injectable() | ||
export class BrevoTransactionalMailsService { | ||
private readonly transactionalEmailsApi = new Map<string, Brevo.TransactionalEmailsApi>(); | ||
|
||
constructor(@Inject(BREVO_MODULE_CONFIG) private readonly config: BrevoModuleConfig) {} | ||
|
||
private getTransactionalEmailsApi(scope: EmailCampaignScopeInterface): Brevo.TransactionalEmailsApi { | ||
const existingTransactionalEmailsApiForScope = this.transactionalEmailsApi.get(JSON.stringify(scope)); | ||
|
||
if (existingTransactionalEmailsApiForScope) { | ||
return existingTransactionalEmailsApiForScope; | ||
} | ||
|
||
const { apiKey } = this.config.brevo.resolveConfig(scope); | ||
const transactionalEmailApi = new Brevo.TransactionalEmailsApi(); | ||
transactionalEmailApi.setApiKey(Brevo.TransactionalEmailsApiApiKeys.apiKey, apiKey); | ||
|
||
this.transactionalEmailsApi.set(JSON.stringify(scope), transactionalEmailApi); | ||
|
||
return transactionalEmailApi; | ||
} | ||
|
||
async send(options: Omit<Brevo.SendSmtpEmail, "sender">, scope: EmailCampaignScopeInterface): SendTransacEmailResponse { | ||
const config = this.config.brevo.resolveConfig(scope); | ||
return this.getTransactionalEmailsApi(scope).sendTransacEmail({ ...options, sender: config.sender }); | ||
} | ||
} |
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