SendGrid plugin (#77)

Closes #73 and #77
This commit is contained in:
Oliver Windall Juhl authored and GitHub committed 2020-07-03 18:00:54 +02:00
1 parent 081c5278cb
commit d6fc477636
22 files changed
+6037 -23

No files matched your search

@@ -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 () => {
+9 -4
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
}
+55 -19
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,9 +159,16 @@ class OrderService extends BaseService {
* @return {Promise} resolves to the creation result.
*/
async create(order) {
return this.orderModel_.create(order).catch(err => {
throw new MedusaError(MedusaError.Types.DB_ERROR, err.message)
})
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,14 +274,23 @@ class OrderService extends BaseService {
// TODO: cancel payment method
return this.orderModel_.updateOne(
{
_id: orderId,
},
{
$set: { status: "cancelled" },
}
)
return this.orderModel_
.updateOne(
{
_id: orderId,
},
{
$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,14 +371,23 @@ class OrderService extends BaseService {
})
)
return this.orderModel_.updateOne(
{
_id: orderId,
},
{
$set: updateFields,
}
)
return this.orderModel_
.updateOne(
{
_id: orderId,
},
{
$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)
})
}
/**
+9
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
}
+15
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"