diff --git a/integration-tests/http/__tests__/returns/returns.spec.ts b/integration-tests/http/__tests__/returns/returns.spec.ts index ff21806b87..3339570227 100644 --- a/integration-tests/http/__tests__/returns/returns.spec.ts +++ b/integration-tests/http/__tests__/returns/returns.spec.ts @@ -973,6 +973,7 @@ medusaIntegrationTestRunner({ items: [ expect.objectContaining({ received_quantity: 2, + damaged_quantity: 1, }), ], }) diff --git a/packages/core/core-flows/src/order/workflows/return/confirm-receive-return-request.ts b/packages/core/core-flows/src/order/workflows/return/confirm-receive-return-request.ts index 78c6e4ae82..ebaf8bda49 100644 --- a/packages/core/core-flows/src/order/workflows/return/confirm-receive-return-request.ts +++ b/packages/core/core-flows/src/order/workflows/return/confirm-receive-return-request.ts @@ -237,12 +237,24 @@ export const confirmReturnReceiveWorkflow = createWorkflow( itemUpdates[itemId].received_quantity, act.details!.quantity as BigNumberInput ) + + if (act.action === ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM) { + itemUpdates[itemId].damaged_quantity = MathBN.add( + itemUpdates[itemId].damaged_quantity, + act.details!.quantity as BigNumberInput + ) + } + return } itemUpdates[itemId] = { id: itemMap[itemId], received_quantity: act.details!.quantity, + damaged_quantity: + act.action === ChangeActionType.RECEIVE_DAMAGED_RETURN_ITEM + ? act.details!.quantity + : 0, } }) diff --git a/packages/modules/order/src/migrations/.snapshot-medusa-order.json b/packages/modules/order/src/migrations/.snapshot-medusa-order.json index 2c9d08461a..a04808ca90 100644 --- a/packages/modules/order/src/migrations/.snapshot-medusa-order.json +++ b/packages/modules/order/src/migrations/.snapshot-medusa-order.json @@ -3685,6 +3685,25 @@ "nullable": false, "mappedType": "json" }, + "damaged_quantity": { + "name": "damaged_quantity", + "type": "numeric", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "default": "0", + "mappedType": "decimal" + }, + "raw_damaged_quantity": { + "name": "raw_damaged_quantity", + "type": "jsonb", + "unsigned": false, + "autoincrement": false, + "primary": false, + "nullable": false, + "mappedType": "json" + }, "return_id": { "name": "return_id", "type": "text", diff --git a/packages/modules/order/src/migrations/Migration20240827133639.ts b/packages/modules/order/src/migrations/Migration20240827133639.ts new file mode 100644 index 0000000000..979d56c359 --- /dev/null +++ b/packages/modules/order/src/migrations/Migration20240827133639.ts @@ -0,0 +1,18 @@ +import { Migration } from "@mikro-orm/migrations" + +export class Migration20240827133639 extends Migration { + async up(): Promise { + this.addSql( + 'alter table if exists "return_item" add column if not exists "damaged_quantity" numeric not null default 0, add column if not exists "raw_damaged_quantity" jsonb not null;' + ) + } + + async down(): Promise { + this.addSql( + 'alter table if exists "return_item" drop column if exists "damaged_quantity";' + ) + this.addSql( + 'alter table if exists "return_item" drop column if exists "raw_damaged_quantity";' + ) + } +} diff --git a/packages/modules/order/src/models/return-item.ts b/packages/modules/order/src/models/return-item.ts index ec0846ac80..864c4e1302 100644 --- a/packages/modules/order/src/models/return-item.ts +++ b/packages/modules/order/src/models/return-item.ts @@ -77,6 +77,12 @@ export default class ReturnItem { @Property({ columnType: "jsonb" }) raw_received_quantity: BigNumberRawValue + @MikroOrmBigNumberProperty() + damaged_quantity: Number | number = 0 + + @Property({ columnType: "jsonb" }) + raw_damaged_quantity: BigNumberRawValue + @ManyToOne(() => Return, { columnType: "text", fieldName: "return_id",