From 5507f35acf9484901bf5dc5ac1e14a5fed8dfa61 Mon Sep 17 00:00:00 2001 From: --list Date: Wed, 9 Jun 2021 16:34:03 +0200 Subject: [PATCH] began testing no_notification in different layers --- .../admin/orders/__tests__/get-order.js | 1 + .../admin/orders/__tests__/return-order.js | 82 +++++++++++++++++++ .../api/routes/admin/orders/request-return.js | 14 +++- .../medusa/src/services/__mocks__/order.js | 2 + .../medusa/src/services/__tests__/order.js | 16 ++++ .../medusa/src/services/__tests__/swap.js | 26 +++++- packages/medusa/src/services/claim.js | 10 +-- packages/medusa/src/services/order.js | 7 +- packages/medusa/src/services/return.js | 1 + packages/medusa/src/services/swap.js | 2 +- 10 files changed, 148 insertions(+), 13 deletions(-) diff --git a/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js b/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js index 5001ed0def..57cb1db7ea 100644 --- a/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js +++ b/packages/medusa/src/api/routes/admin/orders/__tests__/get-order.js @@ -62,6 +62,7 @@ const defaultFields = [ "total", "paid_total", "refundable_amount", + "no_notification", ] describe("GET /admin/orders", () => { diff --git a/packages/medusa/src/api/routes/admin/orders/__tests__/return-order.js b/packages/medusa/src/api/routes/admin/orders/__tests__/return-order.js index 5a36010d3e..f84e0bc65a 100644 --- a/packages/medusa/src/api/routes/admin/orders/__tests__/return-order.js +++ b/packages/medusa/src/api/routes/admin/orders/__tests__/return-order.js @@ -2,6 +2,8 @@ import { IdMap } from "medusa-test-utils" import { request } from "../../../../../helpers/test-request" import { orders } from "../../../../../services/__mocks__/order" import { ReturnService } from "../../../../../services/__mocks__/return" +import { EventBusServiceMock } from "../../../../../services/__mocks__/event-bus" +import { OrderServiceMock } from "../../../../../services/__mocks__/order" describe("POST /admin/orders/:id/return", () => { describe("successfully returns full order", () => { @@ -21,6 +23,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund: 10, + no_notification: true, }, adminSession: { jwt: { @@ -47,6 +50,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund_amount: 10, + no_notification: true, shipping_method: undefined, }) }) @@ -69,6 +73,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund: -1, + no_notification: true, }, adminSession: { jwt: { @@ -95,6 +100,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund_amount: 0, + no_notification: true, shipping_method: undefined, }) }) @@ -118,6 +124,7 @@ describe("POST /admin/orders/:id/return", () => { ], refund: -1, }, + no_notification: true, adminSession: { jwt: { userId: IdMap.getId("admin_user"), @@ -143,6 +150,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund_amount: 0, + no_notification: true, shipping_method: undefined, }) }) @@ -165,6 +173,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund: 100, + no_notification: true, return_shipping: { option_id: "opt_1234", price: 12, @@ -195,6 +204,7 @@ describe("POST /admin/orders/:id/return", () => { }, ], refund_amount: 100, + no_notification: true, shipping_method: { option_id: "opt_1234", price: 12, @@ -205,4 +215,76 @@ describe("POST /admin/orders/:id/return", () => { expect(ReturnService.fulfill).toHaveBeenCalledWith("return") }) }) + + describe("the api call overrides notification settings of order", () => { + it("eventBus is called with the proper no notification feature", async () => { + jest.clearAllMocks() + const subject = await request( + "POST", + `/admin/orders/${IdMap.getId("test-order")}/return`, + { + payload: { + items: [ + { + item_id: IdMap.getId("existingLine"), + quantity: 10, + }, + ], + refund: 100, + return_shipping: { + option_id: "opt_1234", + price: 12, + }, + no_notification: false, + }, + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + expect(EventBusServiceMock.emit).toHaveBeenCalledWith(expect.any(String),{ + id: expect.any(String), + no_notification: false, + return_id: expect.any(String) + }) + }) + }) + + describe("the api call inherits notification settings of order", () => { + it("eventBus is called with the proper no notification feature", async () => { + jest.clearAllMocks() + await request( + "POST", + `/admin/orders/${IdMap.getId("test-order")}/return`, + { + payload: { + items: [ + { + item_id: IdMap.getId("existingLine"), + quantity: 10, + }, + ], + refund: 100, + return_shipping: { + option_id: "opt_1234", + price: 12, + }, + }, + adminSession: { + jwt: { + userId: IdMap.getId("admin_user"), + }, + }, + } + ) + + expect(EventBusServiceMock.emit).toHaveBeenCalledWith(expect.any(String),{ + id: expect.any(String), + no_notification: true, + return_id: expect.any(String) + }) + }) + }) }) diff --git a/packages/medusa/src/api/routes/admin/orders/request-return.js b/packages/medusa/src/api/routes/admin/orders/request-return.js index 3c0b49ed9f..1d3e0c0da0 100644 --- a/packages/medusa/src/api/routes/admin/orders/request-return.js +++ b/packages/medusa/src/api/routes/admin/orders/request-return.js @@ -43,6 +43,9 @@ import { defaultRelations, defaultFields } from "./" * receive_now: * description: A flag to indicate if the Return should be registerd as received immediately. * type: boolean + * no_notification: + * description: A flag to indicate if no notifications should be emitted related to the requested Return. + * type: boolean * refund: * description: The amount to refund. * type: integer @@ -79,6 +82,7 @@ export default async (req, res) => { }) .optional(), receive_now: Validator.boolean().default(false), + no_notification: Validator.boolean().optional(), refund: Validator.number() .integer() .optional(), @@ -141,6 +145,13 @@ export default async (req, res) => { } } + let order = await orderService + .withTransaction(manager) + .retrieve(id) + + const evaluatedNoNotification = value.no_notification !== undefined ? value.no_notification : order.no_notification + returnObj.no_notification = evaluatedNoNotification + const createdReturn = await returnService .withTransaction(manager) .create(returnObj) @@ -150,12 +161,13 @@ export default async (req, res) => { .withTransaction(manager) .fulfill(createdReturn.id) } - + await eventBus .withTransaction(manager) .emit("order.return_requested", { id, return_id: createdReturn.id, + no_notification: evaluatedNoNotification }) return { diff --git a/packages/medusa/src/services/__mocks__/order.js b/packages/medusa/src/services/__mocks__/order.js index bd8d0d6c05..5779f64e3b 100644 --- a/packages/medusa/src/services/__mocks__/order.js +++ b/packages/medusa/src/services/__mocks__/order.js @@ -47,6 +47,7 @@ export const orders = { providerid: "default_provider", data: {}, }, + no_notification: true, shipping_method: [ { providerid: "default_provider", @@ -99,6 +100,7 @@ export const orders = { payment_method: { providerid: "default_provider", }, + no_notification: false, shipping_methods: [ { id: IdMap.getId("expensiveShipping"), diff --git a/packages/medusa/src/services/__tests__/order.js b/packages/medusa/src/services/__tests__/order.js index 6e9755094a..2536f014e7 100644 --- a/packages/medusa/src/services/__tests__/order.js +++ b/packages/medusa/src/services/__tests__/order.js @@ -1129,5 +1129,21 @@ describe("OrderService", () => { ) ).rejects.toThrow("Cannot refund more than the original order amount") }) + + it("emits correct no_notification option", async () => { + await orderService.createRefund( + IdMap.getId("order_123"), + 100, + "discount", + "note", + false + ) + + expect(eventBusService.emit).toHaveBeenCalledWith(expect.any(String),{ + id: expect.any(String), + no_notification: false, + refund_id: expect.any(String) + } ) + }) }) }) diff --git a/packages/medusa/src/services/__tests__/swap.js b/packages/medusa/src/services/__tests__/swap.js index 695583da6a..99d50f750d 100644 --- a/packages/medusa/src/services/__tests__/swap.js +++ b/packages/medusa/src/services/__tests__/swap.js @@ -57,6 +57,7 @@ const testOrder = generateOrder( currency_code: "dkk", region_id: IdMap.getId("region"), tax_rate: 0, + no_notification: true, shipping_address: { first_name: "test", last_name: "testson", @@ -327,6 +328,7 @@ describe("SwapService", () => { order_id: IdMap.getId("test"), fulfillment_status: "not_fulfilled", payment_status: "not_paid", + no_notification: true, additional_items: [ { unit_price: 100, @@ -342,8 +344,7 @@ describe("SwapService", () => { it.each([ [true, true], [false, false], - [undefined, undefined] - ])( "passes correct notification to eventBus with %s", async (input, expected) => { + ])( "passes correct no_notification to eventBus with %s", async (input, expected) => { await swapService.create( testOrder, @@ -357,11 +358,28 @@ describe("SwapService", () => { ) expect(eventBusService.emit).toHaveBeenCalledWith( - expect.anything(), + expect.any(String), {"id": undefined, "no_notification": expected}) }) + + it("passes inherited value of order when no no_notification value is given ", async () => { + 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, + } + ) + + expect(eventBusService.emit).toHaveBeenCalledWith( + expect.anything(), + {"id": undefined, "no_notification": true}) + }) + + }) }) - }) describe("receiveReturn", () => { beforeEach(() => { diff --git a/packages/medusa/src/services/claim.js b/packages/medusa/src/services/claim.js index 18ed345134..80b5858331 100644 --- a/packages/medusa/src/services/claim.js +++ b/packages/medusa/src/services/claim.js @@ -235,7 +235,7 @@ class ClaimService extends BaseService { ) ) - const evaluatedNoNotification = noNotification ? noNotification : order.no_notification + const evaluatedNoNotification = no_notification ? no_notification : order.no_notification const created = claimRepo.create({ shipping_address_id: addressId, @@ -293,7 +293,7 @@ class ClaimService extends BaseService { .withTransaction(manager) .emit(ClaimService.Events.CREATED, { id: result.id, - no_notification: claim.no_notification + no_notification: result.no_notification }) return result @@ -437,7 +437,7 @@ class ClaimService extends BaseService { .withTransaction(manager) .emit(ClaimService.Events.REFUND_PROCESSED, { id, - no_notification: claim.no_notification + no_notification: result.no_notification }) return result @@ -482,7 +482,7 @@ class ClaimService extends BaseService { .emit(ClaimService.Events.SHIPMENT_CREATED, { id, fulfillment_id: shipment.id, - no_notification: claim.no_notification + no_notification: result.no_notification }) return result @@ -533,7 +533,7 @@ class ClaimService extends BaseService { .withTransaction(manager) .emit(ClaimService.Events.CANCELED, { id: result.id, - no_notification: claim.no_notification + no_notification: result.no_notification }) return result diff --git a/packages/medusa/src/services/order.js b/packages/medusa/src/services/order.js index cb9af46a5e..86caba4c51 100644 --- a/packages/medusa/src/services/order.js +++ b/packages/medusa/src/services/order.js @@ -1113,7 +1113,7 @@ class OrderService extends BaseService { /** * Refunds a given amount back to the customer. */ - async createRefund(orderId, refundAmount, reason, note) { + async createRefund(orderId, refundAmount, reason, note, noNotification) { return this.atomicPhase_(async manager => { const order = await this.retrieve(orderId, { select: ["refundable_amount", "total", "refunded_total", "no_notification"], @@ -1132,10 +1132,13 @@ class OrderService extends BaseService { .refundPayment(order.payments, refundAmount, reason, note) const result = await this.retrieve(orderId) + + const evaluatedNoNotification = noNotification != undefined ? noNotification : order.no_notification + this.eventBus_.emit(OrderService.Events.REFUND_CREATED, { id: result.id, refund_id: refund.id, - no_notification: order.no_notification + no_notification: evaluatedNoNotification }) return result }) diff --git a/packages/medusa/src/services/return.js b/packages/medusa/src/services/return.js index c3399d8718..c7ae661064 100644 --- a/packages/medusa/src/services/return.js +++ b/packages/medusa/src/services/return.js @@ -330,6 +330,7 @@ class ReturnService extends BaseService { reason_id: i.reason_id, note: i.note, metadata: i.metadata, + no_notification: data.no_notification, }) ) diff --git a/packages/medusa/src/services/swap.js b/packages/medusa/src/services/swap.js index 07d017ee06..5eb222ec82 100644 --- a/packages/medusa/src/services/swap.js +++ b/packages/medusa/src/services/swap.js @@ -238,7 +238,7 @@ class SwapService extends BaseService { }) ) - const evaluatedNoNotification = noNotification ? noNotification : order.noNotification + const evaluatedNoNotification = noNotification !== undefined ? noNotification : order.no_notification const swapRepo = manager.getCustomRepository(this.swapRepository_) const created = swapRepo.create({