From f88af0c28d3fa574cdeea3694607d4df563cb88d Mon Sep 17 00:00:00 2001 From: Oliver Windall Juhl <59018053+olivermrbl@users.noreply.github.com> Date: Fri, 17 Feb 2023 11:13:50 +0100 Subject: [PATCH] fix(medusa): Received quantity on return lines (#3267) --- .changeset/nasty-parents-prove.md | 5 + .../api/__tests__/admin/returns.js | 259 ++++++++++-------- .../api/__tests__/claims/index.js | 2 +- .../__snapshots__/index.js.snap | 4 +- .../routes/admin/returns/receive-return.ts | 4 +- packages/medusa/src/models/return.ts | 4 +- 6 files changed, 150 insertions(+), 128 deletions(-) create mode 100644 .changeset/nasty-parents-prove.md diff --git a/.changeset/nasty-parents-prove.md b/.changeset/nasty-parents-prove.md new file mode 100644 index 0000000000..3cf27f3ff7 --- /dev/null +++ b/.changeset/nasty-parents-prove.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Received quantity on return lines diff --git a/integration-tests/api/__tests__/admin/returns.js b/integration-tests/api/__tests__/admin/returns.js index 9e18caff20..ecb4507580 100644 --- a/integration-tests/api/__tests__/admin/returns.js +++ b/integration-tests/api/__tests__/admin/returns.js @@ -1,5 +1,6 @@ const path = require("path") import { ReturnReason, ShippingMethod } from "@medusajs/medusa" +import { createReturnableOrder } from "../claims" const setupServer = require("../../../helpers/setup-server") const { useApi } = require("../../../helpers/use-api") @@ -10,6 +11,12 @@ const adminSeeder = require("../../helpers/admin-seeder") jest.setTimeout(30000) +const authHeader = { + headers: { + Authorization: "Bearer test_token", + }, +} + describe("/admin/returns", () => { let medusaProcess let dbConnection @@ -51,26 +58,20 @@ describe("/admin/returns", () => { const api = useApi() // create a swap - const response = await api - .post( - "/admin/orders/test-order/swaps", - { - custom_shipping_options: [{ option_id: "test-option", price: 0 }], - return_items: [ - { - item_id: "test-item", - quantity: 1, - }, - ], - additional_items: [{ variant_id: "test-variant-2", quantity: 1 }], - }, - { - headers: { - authorization: "Bearer test_token", + const response = await api.post( + "/admin/orders/test-order/swaps", + { + custom_shipping_options: [{ option_id: "test-option", price: 0 }], + return_items: [ + { + item_id: "test-item", + quantity: 1, }, - } - ) - .catch((e) => console.log(e)) + ], + additional_items: [{ variant_id: "test-variant-2", quantity: 1 }], + }, + authHeader + ) const sid = response.data.order.swaps[0].id const manager = dbConnection.manager @@ -87,76 +88,52 @@ describe("/admin/returns", () => { await manager.save(sm) // fulfill the swap - const fulRes = await api - .post( - `/admin/orders/test-order/swaps/${sid}/fulfillments`, - {}, - { - headers: { - Authorization: "Bearer test_token", - }, - } - ) - .catch((e) => console.log(e)) + const fulRes = await api.post( + `/admin/orders/test-order/swaps/${sid}/fulfillments`, + {}, + authHeader + ) // ship the swap - await api - .post( - `/admin/orders/test-order/swaps/${sid}/shipments`, - { - fulfillment_id: fulRes.data.order.swaps[0].fulfillments[0].id, - }, - { - headers: { - Authorization: "Bearer test_token", - }, - } - ) - .catch((e) => console.log(e)) + await api.post( + `/admin/orders/test-order/swaps/${sid}/shipments`, + { + fulfillment_id: fulRes.data.order.swaps[0].fulfillments[0].id, + }, + authHeader + ) const swapItemId = fulRes.data.order.swaps[0].additional_items[0].id // request a return - const returnRes = await api - .post( - `/admin/orders/test-order/return`, - { - items: [ - { - item_id: swapItemId, - quantity: 1, - reason_id: rrId, - }, - ], - }, - { - headers: { - Authorization: "Bearer test_token", + const returnRes = await api.post( + `/admin/orders/test-order/return`, + { + items: [ + { + item_id: swapItemId, + quantity: 1, + reason_id: rrId, }, - } - ) - .catch((e) => console.log(e.response)) + ], + }, + authHeader + ) const returnId = returnRes.data.order.returns[0].id - const receiveRes = await api - .post( - `/admin/returns/${returnId}/receive`, - { - items: [ - { - item_id: swapItemId, - quantity: 1, - }, - ], - }, - { - headers: { - Authorization: "Bearer test_token", + const receiveRes = await api.post( + `/admin/returns/${returnId}/receive`, + { + items: [ + { + item_id: swapItemId, + quantity: 1, }, - } - ) - .catch((e) => console.log(e.response)) + ], + }, + authHeader + ) expect(receiveRes.status).toEqual(200) }) @@ -190,11 +167,7 @@ describe("/admin/returns", () => { }, ], }, - { - headers: { - Authorization: "Bearer test_token", - }, - } + authHeader ) .catch((err) => { console.log(err) @@ -214,52 +187,96 @@ describe("/admin/returns", () => { const claimItemId = fulRes.data.order.claims[0].additional_items[0].id // request a return - const returnRes = await api - .post( - `/admin/orders/test-order/return`, - { - items: [ - { - item_id: claimItemId, - quantity: 1, - reason_id: rrId, - }, - ], - return_shipping: { - option_id: "test-option", - price: 0, + const returnRes = await api.post( + `/admin/orders/test-order/return`, + { + items: [ + { + item_id: claimItemId, + quantity: 1, + reason_id: rrId, }, + ], + return_shipping: { + option_id: "test-option", + price: 0, }, - { - headers: { - Authorization: "Bearer test_token", - }, - } - ) - .catch((e) => console.log(e.response)) + }, + authHeader + ) const returnId = returnRes.data.order.returns[0].id - const receiveRes = await api - .post( - `/admin/returns/${returnId}/receive`, - { - items: [ - { - item_id: claimItemId, - quantity: 1, - }, - ], - }, - { - headers: { - Authorization: "Bearer test_token", + const receiveRes = await api.post( + `/admin/returns/${returnId}/receive`, + { + items: [ + { + item_id: claimItemId, + quantity: 1, }, - } - ) - .catch((e) => console.log(e.response)) + ], + }, + authHeader + ) expect(receiveRes.status).toEqual(200) }) }) + + describe("POST /admin/returns/:id/receive", () => { + beforeEach(async () => { + await adminSeeder(dbConnection) + }) + + afterEach(async () => { + const db = useDb() + return await db.teardown() + }) + + it("should receive a return partially", async () => { + const api = useApi() + + const order = await createReturnableOrder(dbConnection) + const itemId = "test-item" + + // create a return + const response = await api.post( + `/admin/orders/${order.id}/return`, + { + items: [ + { + item_id: itemId, + quantity: 2, + }, + ], + }, + authHeader + ) + + const returnId = response.data.order.returns[0].id + + // receive a return + const receiveRes = await api.post( + `/admin/returns/${returnId}/receive`, + { + items: [ + { + item_id: itemId, + quantity: 1, + }, + ], + }, + authHeader + ) + + const receivedReturn = receiveRes.data.return + + expect(receivedReturn.items.length).toEqual(1) + const receivedItem = receivedReturn.items[0] + + expect(receivedItem.requested_quantity).toEqual(2) + expect(receivedItem.received_quantity).toEqual(1) + }) + }) }) diff --git a/integration-tests/api/__tests__/claims/index.js b/integration-tests/api/__tests__/claims/index.js index 2a39509ee3..cc7690bc28 100644 --- a/integration-tests/api/__tests__/claims/index.js +++ b/integration-tests/api/__tests__/claims/index.js @@ -351,7 +351,7 @@ describe("Claims", () => { }) }) -const createReturnableOrder = async (dbConnection, options = {}) => { +export const createReturnableOrder = async (dbConnection, options = {}) => { await simpleProductFactory( dbConnection, { diff --git a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap index 186346654c..e9235797db 100644 --- a/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap +++ b/integration-tests/plugins/__tests__/medusa-plugin-sendgrid/__snapshots__/index.js.snap @@ -572,7 +572,7 @@ Object { "note": null, "quantity": 1, "reason_id": null, - "received_quantity": null, + "received_quantity": 1, "requested_quantity": 1, "return_id": Any, }, @@ -719,7 +719,7 @@ Object { "note": null, "quantity": 1, "reason_id": null, - "received_quantity": null, + "received_quantity": 1, "requested_quantity": 1, "return_id": Any, }, diff --git a/packages/medusa/src/api/routes/admin/returns/receive-return.ts b/packages/medusa/src/api/routes/admin/returns/receive-return.ts index c5d5a02871..684b9314dd 100644 --- a/packages/medusa/src/api/routes/admin/returns/receive-return.ts +++ b/packages/medusa/src/api/routes/admin/returns/receive-return.ts @@ -7,10 +7,10 @@ import { } from "class-validator" import { OrderService, ReturnService, SwapService } from "../../../../services" -import { EntityManager } from "typeorm" import { Type } from "class-transformer" -import { validator } from "../../../../utils/validator" import { isDefined } from "medusa-core-utils" +import { EntityManager } from "typeorm" +import { validator } from "../../../../utils/validator" /** * @oas [post] /returns/{id}/receive diff --git a/packages/medusa/src/models/return.ts b/packages/medusa/src/models/return.ts index 5b5f6558cc..49f41bc5ac 100644 --- a/packages/medusa/src/models/return.ts +++ b/packages/medusa/src/models/return.ts @@ -11,12 +11,12 @@ import { import { DbAwareColumn, resolveDbType } from "../utils/db-aware-column" import { BaseEntity } from "../interfaces/models/base-entity" +import { generateEntityId } from "../utils/generate-entity-id" import { ClaimOrder } from "./claim-order" import { Order } from "./order" import { ReturnItem } from "./return-item" import { ShippingMethod } from "./shipping-method" import { Swap } from "./swap" -import { generateEntityId } from "../utils/generate-entity-id" export enum ReturnStatus { REQUESTED = "requested", @@ -36,7 +36,7 @@ export class Return extends BaseEntity { @OneToMany(() => ReturnItem, (item) => item.return_order, { eager: true, - cascade: ["insert"], + cascade: ["insert", "update"], }) items: ReturnItem[]