1 parent
081c5278cb
commit
d6fc477636
22 files changed
+6037
-23
No files matched your search
@@ -0,0 +1,10 @@
|
||||
import { Router } from "express"
|
||||
import routes from "./routes"
|
||||
|
||||
export default (container) => {
|
||||
const app = Router()
|
||||
|
||||
routes(app)
|
||||
|
||||
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,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
|
||||
}
|
||||
@@ -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
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
@@ -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
|
||||
@@ -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
|
||||
Reference in new issue
Block a user