diff --git a/src/app/api/notary/MailchimpController.ts b/src/app/api/notary/MailchimpController.ts new file mode 100644 index 00000000..b9f3a8f3 --- /dev/null +++ b/src/app/api/notary/MailchimpController.ts @@ -0,0 +1,35 @@ +import { Response, Request } from "express"; +import { Controller, Post } from "@ControllerPattern/index"; +import ApiController from "@Common/system/controller-pattern/ApiController"; +import { Service } from "typedi"; +import authHandler from "@App/middlewares/AuthHandler"; +import MailchimpService from "@Services/common/MailchimpService/MailchimpService"; + + +@Controller() +@Service() +export default class MailchimpController extends ApiController { + constructor(private mailchimpService: MailchimpService) { + super(); + } + + /** + * @description Create a new customer + */ + @Post("/api/v1/notary/mailchimp", [authHandler]) + protected async post(req: Request, response: Response) { + try { + //init IUser resource with request body values + const email = req.body.email; + + //call service to get prisma entity + const responseCall = await this.mailchimpService.addToMailchimpList(email); + + //success + this.httpCreated(response, responseCall); + } catch (error) { + this.httpInternalError(response, error); + return; + } + } +} diff --git a/src/app/index.ts b/src/app/index.ts index c5767457..3b747d8a 100644 --- a/src/app/index.ts +++ b/src/app/index.ts @@ -54,6 +54,7 @@ import StripeController from "./api/admin/StripeController"; import StripeWebhooks from "@Common/webhooks/stripeWebhooks"; import RulesGroupsController from "./api/admin/RulesGroupsController"; import NotesController from "./api/customer/NotesController"; +import MailchimpController from "./api/notary/MailchimpController"; /** * @description This allow to declare all controllers used in the application @@ -116,5 +117,6 @@ export default { Container.get(StripeWebhooks); Container.get(RulesGroupsController); Container.get(NotesController); + Container.get(MailchimpController); }, }; diff --git a/src/common/config/variables/Variables.ts b/src/common/config/variables/Variables.ts index ac3179db..08026778 100644 --- a/src/common/config/variables/Variables.ts +++ b/src/common/config/variables/Variables.ts @@ -160,6 +160,12 @@ export class BackendVariables { @IsNotEmpty() public readonly IDNOT_PROD_BASE_URL!: string; + @IsNotEmpty() + public readonly MAILCHIMP_KEY!: string; + + @IsNotEmpty() + public readonly MAILCHIMP_LIST_ID!: string; + public constructor() { dotenv.config(); this.DATABASE_PORT = process.env["DATABASE_PORT"]!; @@ -214,6 +220,8 @@ export class BackendVariables { this.STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_SUBSCRIPTION_PRICE_ID"]!; this.STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID = process.env["STRIPE_UNLIMITED_ANNUAL_SUBSCRIPTION_PRICE_ID"]!; this.IDNOT_PROD_BASE_URL = process.env["IDNOT_PROD_BASE_URL"]!; + this.MAILCHIMP_KEY = process.env["MAILCHIMP_KEY"]!; + this.MAILCHIMP_LIST_ID = process.env["MAILCHIMP_LIST_ID"]!; } public async validate(groups?: string[]) { const validationOptions = groups ? { groups } : undefined; diff --git a/src/services/common/MailchimpService/MailchimpService.ts b/src/services/common/MailchimpService/MailchimpService.ts index 9e0f4d09..fbc0b03a 100644 --- a/src/services/common/MailchimpService/MailchimpService.ts +++ b/src/services/common/MailchimpService/MailchimpService.ts @@ -114,4 +114,24 @@ export default class MailchimpService extends BaseService { }; }); } + + public async addToMailchimpList(email: string) { + const MAILCHIMP_API_KEY = this.variables.MAILCHIMP_KEY; + const data = { + email_address: email, + status: 'subscribed' + }; + + const response = await fetch(`https://us17.api.mailchimp.com/3.0/lists/`+ this.variables.MAILCHIMP_LIST_ID + `/members`, { + method: 'POST', + headers: { + 'Authorization': `apikey ${MAILCHIMP_API_KEY}`, + 'Content-Type': 'application/json' + }, + body: JSON.stringify(data) + }); + + return response.json(); + + } }