From 746a1d82f82097d99d95216cbb7158d3e45795ef Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 19 Oct 2022 16:43:23 +0200 Subject: [PATCH 1/3] fix: item change conflict --- .../src/services/order-edit-item-change.ts | 28 +++++++++++++------ packages/medusa/src/services/order-edit.ts | 5 ++-- 2 files changed, 23 insertions(+), 10 deletions(-) diff --git a/packages/medusa/src/services/order-edit-item-change.ts b/packages/medusa/src/services/order-edit-item-change.ts index da8a9ebae7e00..928b77af4713b 100644 --- a/packages/medusa/src/services/order-edit-item-change.ts +++ b/packages/medusa/src/services/order-edit-item-change.ts @@ -83,19 +83,31 @@ export default class OrderEditItemChangeService extends TransactionBaseService { return await orderItemChangeRepo.find(query) } - async create(data: CreateOrderEditItemChangeInput): Promise { + async create( + data: CreateOrderEditItemChangeInput + ): Promise { return await this.atomicPhase_(async (manager) => { const orderItemChangeRepo = manager.getCustomRepository( this.orderItemChangeRepository_ ) const changeEntity = orderItemChangeRepo.create(data) - const change = await orderItemChangeRepo.save(changeEntity) - - await this.eventBus_ - .withTransaction(manager) - .emit(OrderEditItemChangeService.Events.CREATED, { id: change.id }) - - return change + try { + const change = await orderItemChangeRepo.save(changeEntity) + await this.eventBus_ + .withTransaction(manager) + .emit(OrderEditItemChangeService.Events.CREATED, { id: change.id }) + + return change + } catch (e) { + if (e.code === "23505") { + throw new MedusaError( + MedusaError.Types.CONFLICT, + "Item change for this item already exists" + ) + } else { + throw e + } + } }) } diff --git a/packages/medusa/src/services/order-edit.ts b/packages/medusa/src/services/order-edit.ts index f9e681a1a74de..8ce337c2906d0 100644 --- a/packages/medusa/src/services/order-edit.ts +++ b/packages/medusa/src/services/order-edit.ts @@ -10,6 +10,7 @@ import { OrderEdit, OrderEditItemChangeType, OrderEditStatus, + OrderItemChange, } from "../models" import { TransactionBaseService } from "../interfaces" import { @@ -412,12 +413,12 @@ export default class OrderEditService extends TransactionBaseService { // if a change does not exist it means that we are updating an existing item and therefore creating an update change. // otherwise we are updating either a change of type ADD or UPDATE if (!change) { - change = await orderEditItemChangeServiceTx.create({ + change = (await orderEditItemChangeServiceTx.create({ type: OrderEditItemChangeType.ITEM_UPDATE, order_edit_id: orderEditId, original_line_item_id: lineItem.original_item_id as string, line_item_id: itemId, - }) + })) as OrderItemChange } await this.lineItemService_ From 144208ab1ea3fa0db37aeaf36ccca2adacb041b5 Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 19 Oct 2022 16:48:09 +0200 Subject: [PATCH 2/3] fix: never type --- packages/medusa/src/services/order-edit-item-change.ts | 2 +- packages/medusa/src/services/order-edit.ts | 4 ++-- 2 files changed, 3 insertions(+), 3 deletions(-) diff --git a/packages/medusa/src/services/order-edit-item-change.ts b/packages/medusa/src/services/order-edit-item-change.ts index 928b77af4713b..3d56a6f4523aa 100644 --- a/packages/medusa/src/services/order-edit-item-change.ts +++ b/packages/medusa/src/services/order-edit-item-change.ts @@ -85,7 +85,7 @@ export default class OrderEditItemChangeService extends TransactionBaseService { async create( data: CreateOrderEditItemChangeInput - ): Promise { + ): Promise { return await this.atomicPhase_(async (manager) => { const orderItemChangeRepo = manager.getCustomRepository( this.orderItemChangeRepository_ diff --git a/packages/medusa/src/services/order-edit.ts b/packages/medusa/src/services/order-edit.ts index 8ce337c2906d0..0881573126217 100644 --- a/packages/medusa/src/services/order-edit.ts +++ b/packages/medusa/src/services/order-edit.ts @@ -413,12 +413,12 @@ export default class OrderEditService extends TransactionBaseService { // if a change does not exist it means that we are updating an existing item and therefore creating an update change. // otherwise we are updating either a change of type ADD or UPDATE if (!change) { - change = (await orderEditItemChangeServiceTx.create({ + change = await orderEditItemChangeServiceTx.create({ type: OrderEditItemChangeType.ITEM_UPDATE, order_edit_id: orderEditId, original_line_item_id: lineItem.original_item_id as string, line_item_id: itemId, - })) as OrderItemChange + }) } await this.lineItemService_ From f4c7711bad6aa64b5f10394126910cf7e4833a7e Mon Sep 17 00:00:00 2001 From: fPolic Date: Wed, 19 Oct 2022 16:49:44 +0200 Subject: [PATCH 3/3] fix: remove import --- packages/medusa/src/services/order-edit.ts | 1 - 1 file changed, 1 deletion(-) diff --git a/packages/medusa/src/services/order-edit.ts b/packages/medusa/src/services/order-edit.ts index 0881573126217..f9e681a1a74de 100644 --- a/packages/medusa/src/services/order-edit.ts +++ b/packages/medusa/src/services/order-edit.ts @@ -10,7 +10,6 @@ import { OrderEdit, OrderEditItemChangeType, OrderEditStatus, - OrderItemChange, } from "../models" import { TransactionBaseService } from "../interfaces" import {