began testing no_notification in different layers

This commit is contained in:
--list
2021-06-10 13:14:42 +02:00
parent 2934acab67
commit 5b65345af1
10 changed files with 148 additions and 13 deletions
@@ -62,6 +62,7 @@ const defaultFields = [
"total", "total",
"paid_total", "paid_total",
"refundable_amount", "refundable_amount",
"no_notification",
] ]
describe("GET /admin/orders", () => { describe("GET /admin/orders", () => {
@@ -2,6 +2,8 @@ import { IdMap } from "medusa-test-utils"
import { request } from "../../../../../helpers/test-request" import { request } from "../../../../../helpers/test-request"
import { orders } from "../../../../../services/__mocks__/order" import { orders } from "../../../../../services/__mocks__/order"
import { ReturnService } from "../../../../../services/__mocks__/return" 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("POST /admin/orders/:id/return", () => {
describe("successfully returns full order", () => { describe("successfully returns full order", () => {
@@ -21,6 +23,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: 10, refund: 10,
no_notification: true,
}, },
adminSession: { adminSession: {
jwt: { jwt: {
@@ -47,6 +50,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 10, refund_amount: 10,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -69,6 +73,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: -1, refund: -1,
no_notification: true,
}, },
adminSession: { adminSession: {
jwt: { jwt: {
@@ -95,6 +100,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 0, refund_amount: 0,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -118,6 +124,7 @@ describe("POST /admin/orders/:id/return", () => {
], ],
refund: -1, refund: -1,
}, },
no_notification: true,
adminSession: { adminSession: {
jwt: { jwt: {
userId: IdMap.getId("admin_user"), userId: IdMap.getId("admin_user"),
@@ -143,6 +150,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 0, refund_amount: 0,
no_notification: true,
shipping_method: undefined, shipping_method: undefined,
}) })
}) })
@@ -165,6 +173,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund: 100, refund: 100,
no_notification: true,
return_shipping: { return_shipping: {
option_id: "opt_1234", option_id: "opt_1234",
price: 12, price: 12,
@@ -195,6 +204,7 @@ describe("POST /admin/orders/:id/return", () => {
}, },
], ],
refund_amount: 100, refund_amount: 100,
no_notification: true,
shipping_method: { shipping_method: {
option_id: "opt_1234", option_id: "opt_1234",
price: 12, price: 12,
@@ -205,4 +215,76 @@ describe("POST /admin/orders/:id/return", () => {
expect(ReturnService.fulfill).toHaveBeenCalledWith("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: * receive_now:
* description: A flag to indicate if the Return should be registerd as received immediately. * description: A flag to indicate if the Return should be registerd as received immediately.
* type: boolean * type: boolean
* no_notification:
* description: A flag to indicate if no notifications should be emitted related to the requested Return.
* type: boolean
* refund: * refund:
* description: The amount to refund. * description: The amount to refund.
* type: integer * type: integer
@@ -79,6 +82,7 @@ export default async (req, res) => {
}) })
.optional(), .optional(),
receive_now: Validator.boolean().default(false), receive_now: Validator.boolean().default(false),
no_notification: Validator.boolean().optional(),
refund: Validator.number() refund: Validator.number()
.integer() .integer()
.optional(), .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 const createdReturn = await returnService
.withTransaction(manager) .withTransaction(manager)
.create(returnObj) .create(returnObj)
@@ -150,12 +161,13 @@ export default async (req, res) => {
.withTransaction(manager) .withTransaction(manager)
.fulfill(createdReturn.id) .fulfill(createdReturn.id)
} }
await eventBus await eventBus
.withTransaction(manager) .withTransaction(manager)
.emit("order.return_requested", { .emit("order.return_requested", {
id, id,
return_id: createdReturn.id, return_id: createdReturn.id,
no_notification: evaluatedNoNotification
}) })
return { return {
@@ -47,6 +47,7 @@ export const orders = {
providerid: "default_provider", providerid: "default_provider",
data: {}, data: {},
}, },
no_notification: true,
shipping_method: [ shipping_method: [
{ {
providerid: "default_provider", providerid: "default_provider",
@@ -99,6 +100,7 @@ export const orders = {
payment_method: { payment_method: {
providerid: "default_provider", providerid: "default_provider",
}, },
no_notification: false,
shipping_methods: [ shipping_methods: [
{ {
id: IdMap.getId("expensiveShipping"), id: IdMap.getId("expensiveShipping"),
@@ -1129,5 +1129,21 @@ describe("OrderService", () => {
) )
).rejects.toThrow("Cannot refund more than the original order amount") ).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)
} )
})
}) })
}) })
+22 -4
View File
@@ -57,6 +57,7 @@ const testOrder = generateOrder(
currency_code: "dkk", currency_code: "dkk",
region_id: IdMap.getId("region"), region_id: IdMap.getId("region"),
tax_rate: 0, tax_rate: 0,
no_notification: true,
shipping_address: { shipping_address: {
first_name: "test", first_name: "test",
last_name: "testson", last_name: "testson",
@@ -327,6 +328,7 @@ describe("SwapService", () => {
order_id: IdMap.getId("test"), order_id: IdMap.getId("test"),
fulfillment_status: "not_fulfilled", fulfillment_status: "not_fulfilled",
payment_status: "not_paid", payment_status: "not_paid",
no_notification: true,
additional_items: [ additional_items: [
{ {
unit_price: 100, unit_price: 100,
@@ -342,8 +344,7 @@ describe("SwapService", () => {
it.each([ it.each([
[true, true], [true, true],
[false, false], [false, false],
[undefined, undefined] ])( "passes correct no_notification to eventBus with %s", async (input, expected) => {
])( "passes correct notification to eventBus with %s", async (input, expected) => {
await swapService.create( await swapService.create(
testOrder, testOrder,
@@ -357,11 +358,28 @@ describe("SwapService", () => {
) )
expect(eventBusService.emit).toHaveBeenCalledWith( expect(eventBusService.emit).toHaveBeenCalledWith(
expect.anything(), expect.any(String),
{"id": undefined, "no_notification": expected}) {"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", () => { describe("receiveReturn", () => {
beforeEach(() => { beforeEach(() => {
+5 -5
View File
@@ -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({ const created = claimRepo.create({
shipping_address_id: addressId, shipping_address_id: addressId,
@@ -293,7 +293,7 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.CREATED, { .emit(ClaimService.Events.CREATED, {
id: result.id, id: result.id,
no_notification: claim.no_notification no_notification: result.no_notification
}) })
return result return result
@@ -437,7 +437,7 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.REFUND_PROCESSED, { .emit(ClaimService.Events.REFUND_PROCESSED, {
id, id,
no_notification: claim.no_notification no_notification: result.no_notification
}) })
return result return result
@@ -482,7 +482,7 @@ class ClaimService extends BaseService {
.emit(ClaimService.Events.SHIPMENT_CREATED, { .emit(ClaimService.Events.SHIPMENT_CREATED, {
id, id,
fulfillment_id: shipment.id, fulfillment_id: shipment.id,
no_notification: claim.no_notification no_notification: result.no_notification
}) })
return result return result
@@ -533,7 +533,7 @@ class ClaimService extends BaseService {
.withTransaction(manager) .withTransaction(manager)
.emit(ClaimService.Events.CANCELED, { .emit(ClaimService.Events.CANCELED, {
id: result.id, id: result.id,
no_notification: claim.no_notification no_notification: result.no_notification
}) })
return result return result
+5 -2
View File
@@ -1112,7 +1112,7 @@ class OrderService extends BaseService {
/** /**
* Refunds a given amount back to the customer. * 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 => { return this.atomicPhase_(async manager => {
const order = await this.retrieve(orderId, { const order = await this.retrieve(orderId, {
select: ["refundable_amount", "total", "refunded_total", "no_notification"], select: ["refundable_amount", "total", "refunded_total", "no_notification"],
@@ -1131,10 +1131,13 @@ class OrderService extends BaseService {
.refundPayment(order.payments, refundAmount, reason, note) .refundPayment(order.payments, refundAmount, reason, note)
const result = await this.retrieve(orderId) const result = await this.retrieve(orderId)
const evaluatedNoNotification = noNotification != undefined ? noNotification : order.no_notification
this.eventBus_.emit(OrderService.Events.REFUND_CREATED, { this.eventBus_.emit(OrderService.Events.REFUND_CREATED, {
id: result.id, id: result.id,
refund_id: refund.id, refund_id: refund.id,
no_notification: order.no_notification no_notification: evaluatedNoNotification
}) })
return result return result
}) })
+1
View File
@@ -330,6 +330,7 @@ class ReturnService extends BaseService {
reason_id: i.reason_id, reason_id: i.reason_id,
note: i.note, note: i.note,
metadata: i.metadata, metadata: i.metadata,
no_notification: data.no_notification,
}) })
) )
+1 -1
View File
@@ -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 swapRepo = manager.getCustomRepository(this.swapRepository_)
const created = swapRepo.create({ const created = swapRepo.create({