began testing no_notification in different layers
This commit is contained in:
@@ -62,6 +62,7 @@ const defaultFields = [
|
||||
"total",
|
||||
"paid_total",
|
||||
"refundable_amount",
|
||||
"no_notification",
|
||||
]
|
||||
|
||||
describe("GET /admin/orders", () => {
|
||||
|
||||
@@ -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)
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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 {
|
||||
|
||||
@@ -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"),
|
||||
|
||||
@@ -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)
|
||||
} )
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
@@ -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(() => {
|
||||
|
||||
@@ -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
|
||||
|
||||
@@ -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
|
||||
})
|
||||
|
||||
@@ -330,6 +330,7 @@ class ReturnService extends BaseService {
|
||||
reason_id: i.reason_id,
|
||||
note: i.note,
|
||||
metadata: i.metadata,
|
||||
no_notification: data.no_notification,
|
||||
})
|
||||
)
|
||||
|
||||
|
||||
@@ -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({
|
||||
|
||||
Reference in New Issue
Block a user