added changes to swap and notification service

This commit is contained in:
--list
2021-06-08 16:01:03 +02:00
parent 4f76088382
commit 49c4ce2c7a
10 changed files with 2335 additions and 2235 deletions
@@ -45,6 +45,9 @@ import { defaultFields, defaultRelations } from "./"
* quantity:
* description: The quantity of the Product Variant to ship.
* type: integer
* no_notification:
* description: If set to true no notification will be send.
* type: boolean
* tags:
* - Order
* responses:
@@ -65,7 +68,7 @@ export default async (req, res) => {
.items({
item_id: Validator.string().required(),
quantity: Validator.number().required(),
})
})
.required(),
return_shipping: Validator.object()
.keys({
@@ -79,6 +82,7 @@ export default async (req, res) => {
variant_id: Validator.string().required(),
quantity: Validator.number().required(),
}),
no_notification: Validator.boolean().optional()
})
const { value, error } = schema.validate(req.body)
@@ -134,6 +138,7 @@ export default async (req, res) => {
value.return_items,
value.additional_items,
value.return_shipping,
no_notification,
{ idempotency_key: idempotencyKey.idempotency_key }
)
@@ -31,6 +31,10 @@ import { MedusaError, Validator } from "medusa-core-utils"
* option_id:
* type: string
* description: The id of the Shipping Option to create the Shipping Method from.
* no_notification:
* description: If set to true no notification will be send
* type: boolean
*
* tags:
* - Return
* responses:
@@ -59,6 +63,7 @@ export default async (req, res) => {
option_id: Validator.string().optional(),
})
.optional(),
no_notification: Validator.boolean().optional()
})
const { value, error } = schema.validate(req.body)
@@ -132,6 +137,7 @@ export default async (req, res) => {
.emit("order.return_requested", {
id: value.order_id,
return_id: createdReturn.id,
no_notification: no_notification
})
return {
@@ -1,30 +1,31 @@
import NotificationService from "../notification"
import { IdMap, MockManager, MockRepository } from "medusa-test-utils"
import { MockManager, MockRepository } from "medusa-test-utils"
describe("NotificationService", () => {
describe("send", () => {
const notificationRepository = MockRepository({ create: c => c })
const notificationRepository = MockRepository({ create: c => c })
const container = {
manager: MockManager,
notificationRepository,
noti_test: {
sendNotification: jest.fn(() =>
Promise.resolve({
to: "test@mail.com",
data: { id: "something" },
})
),
},
}
const container = {
manager: MockManager,
notificationRepository,
noti_test: {
sendNotification: jest.fn(() =>
Promise.resolve({
to: "test@mail.com",
data: { id: "something" },
})
),
},
}
const notificationService = new NotificationService(container)
beforeEach(() => {
jest.clearAllMocks()
})
beforeEach(() => {
jest.clearAllMocks()
})
describe("send", () =>{
it("successfully calls provider and saves noti", async () => {
const notificationService = new NotificationService(container)
await notificationService.send("event.test", { id: "test" }, "test")
expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1)
@@ -51,4 +52,30 @@ describe("NotificationService", () => {
expect(notificationRepository.save).toHaveBeenCalledWith(constructed)
})
})
describe("handleEvent", () => {
it("cancels notification if no_notification is set", async () => {
const notificationService = new NotificationService(container)
const event = "event.test"
notificationService.subscribe(event, "test")
await notificationService.handleEvent(event, {id: "id",
return_id: "id",
no_notification: true})
expect(container.noti_test.sendNotification).not.toHaveBeenCalled()
})
it("if no_notification is not set notification is send", async () => {
const notificationService = new NotificationService(container)
const event = "event.test"
notificationService.subscribe(event, "test")
await notificationService.handleEvent(event, {id: "id", return_id: "id"})
expect(container.noti_test.sendNotification).toHaveBeenCalledTimes(1)
})
})
})
@@ -338,6 +338,28 @@ describe("SwapService", () => {
expect(returnService.create).toHaveBeenCalledTimes(1)
})
it.each([
[true, true],
[false, false],
[undefined, undefined]
])( "passes correct notification to eventBus with %s", async (input, expected) => {
await swapService.create(
testOrder,
[{ item_id: IdMap.getId("line"), quantity: 1 }],
[{ variant_id: IdMap.getId("new-variant"), quantity: 1 }],
{
id: IdMap.getId("return-shipping"),
price: 20,
},
input
)
expect(eventBusService.emit).toHaveBeenCalledWith(
expect.anything(),
{"id": undefined, "no_notification": expected})
})
})
})
@@ -918,4 +940,6 @@ describe("SwapService", () => {
})
})
})
})
@@ -164,6 +164,9 @@ class NotificationService extends BaseService {
if (!subs) {
return
}
if(data['no_notification'] === true) {
return
}
return Promise.all(
subs.map(async providerId => {
+3
View File
@@ -204,6 +204,7 @@ class SwapService extends BaseService {
* the customer.
* @param {ReturnShipping?} returnShipping - an optional shipping method for
* returning the returnItems.
* @param {boolean?} noNotification - an optional flag to disable sending notification when creating swap
* @returns {Promise<Swap>} the newly created swap.
*/
async create(
@@ -211,6 +212,7 @@ class SwapService extends BaseService {
returnItems,
additionalItems,
returnShipping,
noNotification,
custom = {}
) {
return this.atomicPhase_(async manager => {
@@ -256,6 +258,7 @@ class SwapService extends BaseService {
.withTransaction(manager)
.emit(SwapService.Events.CREATED, {
id: result.id,
no_notification: noNotification,
})
return result