feat(plugins): Adds Mailchimp newsletter subscription
This commit is contained in:
@@ -0,0 +1,10 @@
|
||||
import { Router } from "express"
|
||||
import routes from "./routes"
|
||||
|
||||
export default (rootDirectory) => {
|
||||
const app = Router()
|
||||
|
||||
routes(app, rootDirectory)
|
||||
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
export default (fn) => (...args) => fn(...args).catch(args[2])
|
||||
@@ -0,0 +1,5 @@
|
||||
import { default as wrap } from "./await-middleware"
|
||||
|
||||
export default {
|
||||
wrap,
|
||||
}
|
||||
@@ -0,0 +1,30 @@
|
||||
import { Router } from "express"
|
||||
import bodyParser from "body-parser"
|
||||
import middlewares from "../middleware"
|
||||
import { getConfigFile } from "medusa-core-utils"
|
||||
import cors from "cors"
|
||||
|
||||
const route = Router()
|
||||
|
||||
export default (app, rootDirectory) => {
|
||||
const { configModule } = getConfigFile(rootDirectory, `medusa-config`)
|
||||
const config = (configModule && configModule.projectConfig) || {}
|
||||
|
||||
const storeCors = config.store_cors || ""
|
||||
|
||||
route.use(
|
||||
cors({
|
||||
origin: storeCors.split(","),
|
||||
credentials: true,
|
||||
})
|
||||
)
|
||||
|
||||
app.use("/mailchimp", route)
|
||||
|
||||
route.post(
|
||||
"/subscribe",
|
||||
bodyParser.json(),
|
||||
middlewares.wrap(require("./subscribe-newsletter").default)
|
||||
)
|
||||
return app
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { Validator, MedusaError } from "medusa-core-utils"
|
||||
|
||||
export default async (req, res) => {
|
||||
const schema = Validator.object().keys({
|
||||
email: Validator.string().required(),
|
||||
data: Validator.object().default({}),
|
||||
})
|
||||
|
||||
const { value, error } = schema.validate(req.body)
|
||||
if (error) {
|
||||
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
|
||||
}
|
||||
console.log(value)
|
||||
|
||||
try {
|
||||
const mailchimpService = req.scope.resolve("mailchimpService")
|
||||
await mailchimpService.subscribeNewsletter(value.email, value.data)
|
||||
res.sendStatus(200)
|
||||
} catch (err) {
|
||||
throw err
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
const MailchimpMock = jest.fn().mockImplementation(() => {
|
||||
return {
|
||||
subscribeNewsletter: jest.fn().mockReturnValue(Promise.resolve("success")),
|
||||
}
|
||||
})
|
||||
|
||||
describe("MailchimpService", () => {
|
||||
describe("newsletterSubscribe", () => {
|
||||
let result
|
||||
beforeAll(async () => {
|
||||
jest.clearAllMocks()
|
||||
const mailchimpService = new MailchimpMock()
|
||||
|
||||
result = await mailchimpService.subscribeNewsletter("medusa@medusa.com")
|
||||
})
|
||||
|
||||
it("resolves successfully", () => {
|
||||
expect(result).toEqual("success")
|
||||
})
|
||||
})
|
||||
})
|
||||
@@ -0,0 +1,39 @@
|
||||
import { BaseService } from "medusa-interfaces"
|
||||
import Mailchimp from "mailchimp-api-v3"
|
||||
|
||||
class MailchimpService extends BaseService {
|
||||
/**
|
||||
* @param {Object} options - options defined in `medusa-config.js`
|
||||
* e.g.
|
||||
* {
|
||||
* api_key: Mailchimp api key
|
||||
* newsletter_list_id: "123456789"
|
||||
* }
|
||||
*/
|
||||
constructor({}, options) {
|
||||
super()
|
||||
|
||||
this.options_ = options
|
||||
|
||||
this.mailchimp_ = new Mailchimp(options.api_key)
|
||||
}
|
||||
|
||||
/**
|
||||
* Subscribes an email to a newsletter.
|
||||
* @param {string} email - email to use for the subscription
|
||||
* @param {Object} data - additional data (see https://mailchimp.com/developer/api/marketing/list-members/add-member-to-list/)
|
||||
* @return {Promise} result of newsletter subscription
|
||||
*/
|
||||
async subscribeNewsletter(email, data) {
|
||||
return this.mailchimp_.post(
|
||||
`/lists/${this.options_.newsletter_list_id}/members`,
|
||||
{
|
||||
email_address: email,
|
||||
status: "subscribed",
|
||||
...data,
|
||||
}
|
||||
)
|
||||
}
|
||||
}
|
||||
|
||||
export default MailchimpService
|
||||
Reference in New Issue
Block a user