feat(medusa): Pass location upon receiving returns (#2949)

This commit is contained in:
Philip Korsholm
2023-01-05 19:09:27 +01:00
committed by GitHub
parent 0b14814d5a
commit a153289ba3
3 changed files with 20 additions and 6 deletions
@@ -39,7 +39,8 @@ describe("POST /admin/returns/:id/receive", () => {
IdMap.getId("test-return"), IdMap.getId("test-return"),
[{ item_id: IdMap.getId("test"), quantity: 2 }], [{ item_id: IdMap.getId("test"), quantity: 2 }],
undefined, undefined,
true true,
{ location_id: undefined }
) )
}) })
@@ -5,7 +5,12 @@ import {
IsString, IsString,
ValidateNested, ValidateNested,
} from "class-validator" } from "class-validator"
import { OrderService, ReturnService, SwapService } from "../../../../services" import {
OrderService,
ProductVariantInventoryService,
ReturnService,
SwapService,
} from "../../../../services"
import { EntityManager } from "typeorm" import { EntityManager } from "typeorm"
import { Type } from "class-transformer" import { Type } from "class-transformer"
@@ -98,13 +103,15 @@ export default async (req, res) => {
await entityManager.transaction(async (manager) => { await entityManager.transaction(async (manager) => {
let refundAmount = validated.refund let refundAmount = validated.refund
if (isDefined(validated.refund) && validated.refund < 0) { if (isDefined(validated.refund) && validated.refund! < 0) {
refundAmount = 0 refundAmount = 0
} }
receivedReturn = await returnService receivedReturn = await returnService
.withTransaction(manager) .withTransaction(manager)
.receive(id, validated.items, refundAmount, true) .receive(id, validated.items, refundAmount, true, {
locationId: validated.location_id,
})
if (receivedReturn.order_id) { if (receivedReturn.order_id) {
await orderService await orderService
@@ -169,4 +176,8 @@ export class AdminPostReturnsReturnReceiveReq {
@IsOptional() @IsOptional()
@IsNumber() @IsNumber()
refund?: number refund?: number
@IsOptional()
@IsString()
location_id?: string
} }
+4 -2
View File
@@ -567,7 +567,8 @@ class ReturnService extends TransactionBaseService {
returnId: string, returnId: string,
receivedItems: OrdersReturnItem[], receivedItems: OrdersReturnItem[],
refundAmount?: number, refundAmount?: number,
allowMismatch = false allowMismatch = false,
context: { locationId?: string } = {}
): Promise<Return | never> { ): Promise<Return | never> {
return await this.atomicPhase_(async (manager) => { return await this.atomicPhase_(async (manager) => {
const returnRepository = manager.getCustomRepository( const returnRepository = manager.getCustomRepository(
@@ -657,6 +658,7 @@ class ReturnService extends TransactionBaseService {
const now = new Date() const now = new Date()
const updateObj = { const updateObj = {
...returnObj, ...returnObj,
location_id: context.locationId || returnObj.location_id,
status: returnStatus, status: returnStatus,
items: newLines, items: newLines,
refund_amount: totalRefundableAmount, refund_amount: totalRefundableAmount,
@@ -682,7 +684,7 @@ class ReturnService extends TransactionBaseService {
if (orderItem && orderItem.variant_id) { if (orderItem && orderItem.variant_id) {
await productVarInventoryTx.adjustInventory( await productVarInventoryTx.adjustInventory(
orderItem.variant_id, orderItem.variant_id,
returnObj.location_id!, result.location_id!,
line.received_quantity line.received_quantity
) )
} }