feat(plugins): Adds Mailchimp newsletter subscription

This commit is contained in:
Oliver Windall Juhl
2020-10-01 12:30:11 +02:00
committed by GitHub
parent 47e2ad2952
commit 89c21f2f5b
17 changed files with 5952 additions and 0 deletions

View File

@@ -0,0 +1,13 @@
{
"plugins": [
"@babel/plugin-proposal-class-properties",
"@babel/plugin-transform-instanceof",
"@babel/plugin-transform-classes"
],
"presets": ["@babel/preset-env"],
"env": {
"test": {
"plugins": ["@babel/plugin-transform-runtime"]
}
}
}

View File

@@ -0,0 +1,9 @@
{
"plugins": ["prettier"],
"extends": ["prettier"],
"rules": {
"prettier/prettier": "error",
"semi": "error",
"no-unused-expressions": "true"
}
}

View File

@@ -0,0 +1,15 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
!jest.config.js
/dist
/api
/services
/models
/subscribers

View File

@@ -0,0 +1,9 @@
/lib
node_modules
.DS_store
.env*
/*.js
!index.js
yarn.lock

View File

@@ -0,0 +1,7 @@
{
"endOfLine": "lf",
"semi": false,
"singleQuote": false,
"tabWidth": 2,
"trailingComma": "es5"
}

View File

@@ -0,0 +1,27 @@
# medusa-plugin-mailchimp
Mailchimp plugin for Medusa that supports newsletter subscriptions.
## Plugin Options
```
{
api_key: [your mailchimp api key] (required),
newsletter_list_id: ["123456789"] (required)
}
```
## Dynamic usage
You can resolve the Mailchimp service, such that your server can handle newsletter subscriptions.
Example:
```js
const mailchimpService = req.scope.resolve("mailchimpService")
mailchimpService.subscribeNewsletter(
"acme@mail.com",
// merge_fields to include in the subscription
{ FIRSTNAME: "Acme", LASTNAME: "Inc." }
)
```

View File

@@ -0,0 +1 @@
// noop

View File

@@ -0,0 +1,3 @@
module.exports = {
testEnvironment: "node",
}

View File

@@ -0,0 +1,46 @@
{
"name": "medusa-plugin-mailchimp",
"version": "1.0.11",
"description": "Mailchimp newsletter subscriptions",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-mailchimp"
},
"author": "Oliver Juhl",
"license": "MIT",
"devDependencies": {
"@babel/cli": "^7.7.5",
"@babel/core": "^7.7.5",
"@babel/node": "^7.7.4",
"@babel/plugin-proposal-class-properties": "^7.7.4",
"@babel/plugin-transform-instanceof": "^7.8.3",
"@babel/plugin-transform-runtime": "^7.7.6",
"@babel/preset-env": "^7.7.5",
"@babel/register": "^7.7.4",
"@babel/runtime": "^7.9.6",
"client-sessions": "^0.8.0",
"cross-env": "^5.2.1",
"eslint": "^6.8.0",
"jest": "^25.5.2"
},
"scripts": {
"build": "babel src -d .",
"prepare": "cross-env NODE_ENV=production npm run build",
"watch": "babel -w src --out-dir . --ignore **/__tests__",
"test": "jest"
},
"peerDependencies": {
"medusa-interfaces": "1.x"
},
"dependencies": {
"@babel/plugin-transform-classes": "^7.9.5",
"body-parser": "^1.19.0",
"cors": "^2.8.5",
"express": "^4.17.1",
"mailchimp-api-v3": "^1.14.0",
"medusa-core-utils": "^1.0.10",
"medusa-test-utils": "^1.0.10"
}
}

View File

@@ -0,0 +1,10 @@
import { Router } from "express"
import routes from "./routes"
export default (rootDirectory) => {
const app = Router()
routes(app, rootDirectory)
return app
}

View File

@@ -0,0 +1 @@
export default (fn) => (...args) => fn(...args).catch(args[2])

View File

@@ -0,0 +1,5 @@
import { default as wrap } from "./await-middleware"
export default {
wrap,
}

View File

@@ -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
}

View File

@@ -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
}
}

View File

@@ -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")
})
})
})

View File

@@ -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

File diff suppressed because it is too large Load Diff