SendGrid plugin (#77)

Closes #73 and #77
This commit is contained in:
Oliver Windall Juhl
2020-07-03 18:00:54 +02:00
committed by GitHub
parent 081c5278cb
commit d6fc477636
22 changed files with 6037 additions and 23 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 @@
// noop

View File

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

View File

@@ -0,0 +1,43 @@
{
"name": "medusa-plugin-sendgrid",
"version": "0.3.0",
"description": "SendGrid transactional emails",
"main": "index.js",
"repository": {
"type": "git",
"url": "https://github.com/medusajs/medusa",
"directory": "packages/medusa-plugin-sendgrid"
},
"author": "Oliver Juhl",
"license": "AGPL-3.0-or-later",
"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"
},
"dependencies": {
"@babel/plugin-transform-classes": "^7.9.5",
"@sendgrid/mail": "^7.1.1",
"body-parser": "^1.19.0",
"express": "^4.17.1",
"medusa-core-utils": "^0.3.0",
"medusa-interfaces": "^0.3.0",
"medusa-test-utils": "^0.3.0"
}
}

View File

@@ -0,0 +1,10 @@
import { Router } from "express"
import routes from "./routes"
export default (container) => {
const app = Router()
routes(app)
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,16 @@
import { Router } from "express"
import bodyParser from "body-parser"
import middlewares from "../middleware"
const route = Router()
export default (app) => {
app.use("/sendgrid", route)
route.post(
"/send",
bodyParser.raw({ type: "application/json" }),
middlewares.wrap(require("./send-email").default)
)
return app
}

View File

@@ -0,0 +1,28 @@
import { Validator, MedusaError } from "medusa-core-utils"
export default async (req, res) => {
const schema = Validator.object().keys({
template_id: Validator.string().required(),
from: Validator.string().required(),
to: Validator.string().required(),
data: Validator.object().optional().default({}),
})
const { value, error } = schema.validate(req.body)
if (error) {
throw new MedusaError(MedusaError.Types.INVALID_DATA, error.details)
}
try {
const sendgridService = req.scope.resolve("sendgridService")
await sendgridService.sendEmail(
value.template_id,
value.from,
value.to,
value.data
)
res.sendStatus(200)
} catch (err) {
throw err
}
}

View File

@@ -0,0 +1,89 @@
import { BaseService } from "medusa-interfaces"
import SendGrid from "@sendgrid/mail"
class SendGridService extends BaseService {
/**
* @param {Object} options - options defined in `medusa-config.js`
* e.g.
* {
* api_key: SendGrid api key
* from: Medusa <hello@medusa.example>,
* order_placed_template: 01234,
* order_updated_template: 56789,
* order_updated_cancelled_template: 4242,
* user_password_reset_template: 0000,
* customer_password_reset_template: 1111,
* }
*/
constructor({}, options) {
super()
this.options_ = options
SendGrid.setApiKey(options.api_key)
}
/**
* Sends a transactional email based on an event using SendGrid.
* @param {string} event - event related to the order
* @param {Object} order - the order object sent to SendGrid, that must
* correlate with the structure specificed in the dynamic template
* @returns {Promise} result of the send operation
*/
async transactionalEmail(event, order) {
let templateId
switch (event) {
case "order.placed":
templateId = this.options_.order_placed_template
break
case "order.updated":
templateId = this.options_.order_updated_template
break
case "order.cancelled":
templateId = this.options_.order_cancelled_template
break
case "user.password_reset":
templateId = this.options_.user_password_reset_template
break
case "customer.password_reset":
templateId = this.options_.customer_password_reset_template
break
default:
return
}
try {
return SendGrid.send({
template_id: templateId,
from: options.from,
to: order.email,
dynamic_template_data: order,
})
} catch (error) {
throw error
}
}
/**
* Sends an email using SendGrid.
* @param {string} templateId - id of template in SendGrid
* @param {string} from - sender of email
* @param {string} to - receiver of email
* @param {Object} data - data to send in mail (match with template)
* @returns {Promise} result of the send operation
*/
async sendEmail(templateId, from, to, data) {
try {
return SendGrid.send({
to,
from,
template_id: templateId,
dynamic_template_data: data,
})
} catch (error) {
throw error
}
}
}
export default SendGridService

View File

@@ -0,0 +1,21 @@
class OrderSubscriber {
constructor({ sendgridService, eventBusService }) {
this.sendgridService_ = sendgridService
this.eventBus_ = eventBusService
this.eventBus_.subscribe("order.placed", async (order) => {
await this.sendgridService_.transactionalEmail("order.placed", order)
})
this.eventBus_.subscribe("order.cancelled", async (order) => {
await this.sendgridService_.transactionalEmail("order.cancelled", order)
})
this.eventBus_.subscribe("order.updated", async (order) => {
await this.sendgridService_.transactionalEmail("order.updated", order)
})
}
}
export default OrderSubscriber

View File

@@ -0,0 +1,23 @@
class UserSubscriber {
constructor({ sendgridService, eventBusService }) {
this.sendgridService_ = sendgridService
this.eventBus_ = eventBusService
this.eventBus_.subscribe("user.password_reset", async (data) => {
await this.sendgridService_.transactionalEmail(
"user.password_reset",
data
)
})
this.eventBus_.subscribe("customer.password_reset", async (data) => {
await this.sendgridService_.transactionalEmail(
"customer.password_reset",
data
)
})
}
}
export default UserSubscriber

File diff suppressed because it is too large Load Diff

View File

@@ -5,11 +5,13 @@ import { PaymentProviderServiceMock } from "../__mocks__/payment-provider"
import { FulfillmentProviderServiceMock } from "../__mocks__/fulfillment-provider"
import { ShippingProfileServiceMock } from "../__mocks__/shipping-profile"
import { TotalsServiceMock } from "../__mocks__/totals"
import { EventBusServiceMock } from "../__mocks__/event-bus"
describe("OrderService", () => {
describe("create", () => {
const orderService = new OrderService({
orderModel: OrderModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(async () => {
@@ -54,6 +56,7 @@ describe("OrderService", () => {
describe("update", () => {
const orderService = new OrderService({
orderModel: OrderModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(async () => {
@@ -182,6 +185,7 @@ describe("OrderService", () => {
describe("cancel", () => {
const orderService = new OrderService({
orderModel: OrderModelMock,
eventBusService: EventBusServiceMock,
})
beforeEach(async () => {
@@ -252,6 +256,7 @@ describe("OrderService", () => {
paymentProviderService: PaymentProviderServiceMock,
fulfillmentProviderService: FulfillmentProviderServiceMock,
shippingProfileService: ShippingProfileServiceMock,
eventBusService: EventBusServiceMock,
})
beforeEach(async () => {

View File

@@ -9,6 +9,10 @@ import { BaseService } from "medusa-interfaces"
* @implements BaseService
*/
class CustomerService extends BaseService {
static Events = {
PASSWORD_RESET: "customer.password_reset",
}
constructor({ customerModel, eventBusService }) {
super()
@@ -92,10 +96,11 @@ class CustomerService extends BaseService {
const expiry = Math.floor(Date.now() / 1000) + 60 * 15 // 15 minutes ahead
const payload = { customer_id: customer._id, exp: expiry }
const token = jwt.sign(payload, secret)
// TODO: Call event layer to ensure that there is an email service that
// sends the token.
// Notify subscribers
this.eventBus_.emit(CustomerService.Events.PASSWORD_RESET, {
email: customer.email,
token,
})
return token
}

View File

@@ -3,6 +3,12 @@ import { Validator, MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
class OrderService extends BaseService {
static Events = {
PLACED: "order.placed",
UPDATED: "order.updated",
CANCELLED: "order.cancelled",
}
constructor({
orderModel,
paymentProviderService,
@@ -153,7 +159,14 @@ class OrderService extends BaseService {
* @return {Promise} resolves to the creation result.
*/
async create(order) {
return this.orderModel_.create(order).catch(err => {
return this.orderModel_
.create(order)
.then(result => {
// Notify subscribers
this.eventBus_.emit(OrderService.Events.PLACED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
@@ -225,6 +238,11 @@ class OrderService extends BaseService {
{ $set: updateFields },
{ runValidators: true }
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(OrderService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
@@ -256,7 +274,8 @@ class OrderService extends BaseService {
// TODO: cancel payment method
return this.orderModel_.updateOne(
return this.orderModel_
.updateOne(
{
_id: orderId,
},
@@ -264,6 +283,14 @@ class OrderService extends BaseService {
$set: { status: "cancelled" },
}
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(OrderService.Events.CANCELLED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**
@@ -344,7 +371,8 @@ class OrderService extends BaseService {
})
)
return this.orderModel_.updateOne(
return this.orderModel_
.updateOne(
{
_id: orderId,
},
@@ -352,6 +380,14 @@ class OrderService extends BaseService {
$set: updateFields,
}
)
.then(result => {
// Notify subscribers
this.eventBus_.emit(OrderService.Events.UPDATED, result)
return result
})
.catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
}
/**

View File

@@ -9,6 +9,10 @@ import { BaseService } from "medusa-interfaces"
* @implements BaseService
*/
class UserService extends BaseService {
static Events = {
PASSWORD_RESET: "user.password_reset",
}
constructor({ userModel, eventBusService }) {
super()
@@ -245,6 +249,11 @@ class UserService extends BaseService {
const expiry = Math.floor(Date.now() / 1000) + 60 * 15
const payload = { user_id: user._id, exp: expiry }
const token = jwt.sign(payload, secret)
// Notify subscribers
this.eventBus_.emit(UserService.Events.PASSWORD_RESET, {
email: user.email,
token,
})
return token
}

View File

@@ -4522,6 +4522,14 @@ media-typer@0.3.0:
resolved "https://registry.yarnpkg.com/media-typer/-/media-typer-0.3.0.tgz#8710d7af0aa626f8fffa1ce00168545263255748"
integrity sha1-hxDXrwqmJvj/+hzgAWhUUmMlV0g=
medusa-core-utils@^0.3.0:
version "0.1.39"
resolved "https://registry.yarnpkg.com/medusa-core-utils/-/medusa-core-utils-0.1.39.tgz#d57816c9bd43f9a92883650c1e66add1665291df"
integrity sha512-R8+U1ile7if+nR6Cjh5exunx0ETV0OfkWUUBUpz1KmHSDv0V0CcvQqU9lcZesPFDEbu3Y2iEjsCqidVA4nG2nQ==
dependencies:
"@hapi/joi" "^16.1.8"
joi-objectid "^3.0.1"
medusa-interfaces@^0.1.27:
version "0.1.27"
resolved "https://registry.yarnpkg.com/medusa-interfaces/-/medusa-interfaces-0.1.27.tgz#e77f9a9f82a7118eac8b35c1498ef8a5cec78898"
@@ -4529,6 +4537,13 @@ medusa-interfaces@^0.1.27:
dependencies:
mongoose "^5.8.0"
medusa-test-utils@^0.3.0:
version "0.1.39"
resolved "https://registry.yarnpkg.com/medusa-test-utils/-/medusa-test-utils-0.1.39.tgz#b7c166006a2fa4f02e52ab3bfafc19a3ae787f3e"
integrity sha512-M/Br8/HYvl7x2oLnme4NxdQwoyV0XUyOWiCyvPp7q1HUTB684lhJf1MikZVrcSjsh2L1rpyi3GRbKdf4cpJWvw==
dependencies:
mongoose "^5.8.0"
memory-pager@^1.0.2:
version "1.5.0"
resolved "https://registry.yarnpkg.com/memory-pager/-/memory-pager-1.5.0.tgz#d8751655d22d384682741c972f2c3d6dfa3e66b5"