-
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 public api route to subscribe to brevo
- Loading branch information
1 parent
23371d5
commit ef65801
Showing
8 changed files
with
106 additions
and
19 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,5 @@ | ||
--- | ||
"@comet/brevo-api": major | ||
--- | ||
|
||
Add a new public route that allows subscribing to the Brevo Module. By default, this route is disabled and can be enabled in the Brevo module configuration via `enablePublicApiSubscriptionRoute: true`. This feature allows external applications to create a newsletter form and subscribe via REST to the Brevo module. |
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 |
---|---|---|
|
@@ -173,6 +173,7 @@ export class AppModule { | |
}, | ||
}, | ||
}, | ||
enablePublicApiSubscriptionRoute: true, | ||
}), | ||
], | ||
}; | ||
|
56 changes: 56 additions & 0 deletions
56
packages/api/src/brevo-contact/brevo-contact.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,56 @@ | ||
import { DisableGlobalGuard } from "@comet/cms-api"; | ||
import { Body, Controller, Post, Type as NestType } from "@nestjs/common"; | ||
import { Type } from "class-transformer"; | ||
import { IsEmail, IsNotEmpty, IsUrl, Validate, ValidateNested } from "class-validator"; | ||
import { BrevoContactAttributesInterface } from "src/types"; | ||
|
||
import { EmailCampaignScopeInterface } from "../types"; | ||
import { BrevoContactsService } from "./brevo-contacts.service"; | ||
import { SubscribeResponse } from "./dto/subscribe-response.enum"; | ||
import { IsValidRedirectURLConstraint } from "./validator/redirect-url.validator"; | ||
|
||
export function createBrevoContactController({ | ||
BrevoContactAttributes, | ||
Scope, | ||
}: { | ||
BrevoContactAttributes?: NestType<BrevoContactAttributesInterface>; | ||
Scope: NestType<EmailCampaignScopeInterface>; | ||
}): NestType<unknown> { | ||
class SubscribeInputBase { | ||
@IsEmail() | ||
email: string; | ||
|
||
@IsUrl({ require_tld: process.env.NODE_ENV === "production" }) | ||
@Validate(IsValidRedirectURLConstraint) | ||
redirectionUrl: string; | ||
|
||
@IsNotEmpty() | ||
@Type(() => Scope) | ||
@ValidateNested() | ||
scope: typeof Scope; | ||
} | ||
|
||
class SubscribeInputWithAttributes extends SubscribeInputBase { | ||
@IsNotEmpty() | ||
@Type(typeof BrevoContactAttributes !== "undefined" ? () => BrevoContactAttributes : undefined) | ||
@ValidateNested() | ||
attributes: typeof BrevoContactAttributes; | ||
} | ||
|
||
class SubscribeInput extends (typeof BrevoContactAttributes !== "undefined" ? SubscribeInputWithAttributes : SubscribeInputBase) {} | ||
|
||
@Controller("brevo-contacts") | ||
class BrevoContactsController { | ||
constructor(private readonly brevoContactsService: BrevoContactsService) {} | ||
|
||
@DisableGlobalGuard() | ||
@Post(`/subscribe`) | ||
async subscribe(@Body() data: SubscribeInput): Promise<SubscribeResponse> { | ||
const { scope, ...input } = data; | ||
|
||
return this.brevoContactsService.subscribeBrevoContact(input, data.scope); | ||
} | ||
} | ||
|
||
return BrevoContactsController; | ||
} |
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