@@ -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,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
|
||||
}
|
||||
|
||||
|
||||
@@ -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,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
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user