diff --git a/.changeset/serious-flowers-provide.md b/.changeset/serious-flowers-provide.md new file mode 100644 index 0000000000000..aefcf7d557155 --- /dev/null +++ b/.changeset/serious-flowers-provide.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +fix(medusa): Order edit confirmation conflict line items update diff --git a/packages/inventory/src/utils/build-query.ts b/packages/inventory/src/utils/build-query.ts index 11e9d84228ea5..ee59ece03da38 100644 --- a/packages/inventory/src/utils/build-query.ts +++ b/packages/inventory/src/utils/build-query.ts @@ -97,8 +97,20 @@ export function buildQuery( */ function buildWhere( constraints: TWhereKeys -): FindOptionsWhere { - const where: FindOptionsWhere = {} +): FindOptionsWhere | FindOptionsWhere[] { + let where: FindOptionsWhere | FindOptionsWhere[] = {} + + if (Array.isArray(constraints)) { + where = [] + constraints.forEach((constraint) => { + ;(where as FindOptionsWhere[]).push( + buildWhere(constraint) as FindOptionsWhere + ) + }) + + return where + } + for (const [key, value] of Object.entries(constraints)) { if (value === undefined) { continue diff --git a/packages/medusa/src/services/customer.ts b/packages/medusa/src/services/customer.ts index d10eafbe9fa4c..39fcd3ca5c2b2 100644 --- a/packages/medusa/src/services/customer.ts +++ b/packages/medusa/src/services/customer.ts @@ -21,6 +21,7 @@ import { } from "../types/common" import { CreateCustomerInput, UpdateCustomerInput } from "../types/customers" import { buildQuery, setMetadata } from "../utils" +import { selectorConstraintsToString } from "@medusajs/utils" type InjectedDependencies = { manager: EntityManager @@ -194,9 +195,8 @@ class CustomerService extends TransactionBaseService { const customer = await customerRepo.findOne(query) if (!customer) { - const selectorConstraints = Object.entries(selector) - .map((key, value) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) + throw new MedusaError( MedusaError.Types.NOT_FOUND, `Customer with ${selectorConstraints} was not found` diff --git a/packages/medusa/src/services/gift-card.ts b/packages/medusa/src/services/gift-card.ts index 0ba1146c3c784..a31f684eb0405 100644 --- a/packages/medusa/src/services/gift-card.ts +++ b/packages/medusa/src/services/gift-card.ts @@ -14,6 +14,7 @@ import { import { buildQuery, setMetadata } from "../utils" import EventBusService from "./event-bus" import RegionService from "./region" +import {selectorConstraintsToString} from "@medusajs/utils"; type InjectedDependencies = { manager: EntityManager @@ -193,9 +194,7 @@ class GiftCardService extends TransactionBaseService { const giftCard = await giftCardRepo.findOne(query) if (!giftCard) { - const selectorConstraints = Object.entries(selector) - .map(([key, value]) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/services/line-item.ts b/packages/medusa/src/services/line-item.ts index 1069ca30e52e5..cb89ec787efaf 100644 --- a/packages/medusa/src/services/line-item.ts +++ b/packages/medusa/src/services/line-item.ts @@ -1,23 +1,28 @@ -import { MedusaError } from "medusa-core-utils" -import { EntityManager, In } from "typeorm" -import { DeepPartial } from "typeorm/common/DeepPartial" +import {MedusaError} from "medusa-core-utils" +import {EntityManager, In} from "typeorm" +import {DeepPartial} from "typeorm/common/DeepPartial" -import { FlagRouter, MedusaV2Flag } from "@medusajs/utils" -import { TransactionBaseService } from "../interfaces" -import TaxInclusivePricingFeatureFlag from "../loaders/feature-flags/tax-inclusive-pricing" +import { + FlagRouter, + MedusaV2Flag, + selectorConstraintsToString +} from "@medusajs/utils" +import {TransactionBaseService} from "../interfaces" +import TaxInclusivePricingFeatureFlag + from "../loaders/feature-flags/tax-inclusive-pricing" import { LineItem, LineItemAdjustment, LineItemTaxLine, ProductVariant, } from "../models" -import { CartRepository } from "../repositories/cart" -import { LineItemRepository } from "../repositories/line-item" -import { LineItemTaxLineRepository } from "../repositories/line-item-tax-line" -import { FindConfig, Selector } from "../types/common" -import { GenerateInputData, GenerateLineItemContext } from "../types/line-item" -import { ProductVariantPricing } from "../types/pricing" -import { buildQuery, isString, setMetadata } from "../utils" +import {CartRepository} from "../repositories/cart" +import {LineItemRepository} from "../repositories/line-item" +import {LineItemTaxLineRepository} from "../repositories/line-item-tax-line" +import {FindConfig, Selector} from "../types/common" +import {GenerateInputData, GenerateLineItemContext} from "../types/line-item" +import {ProductVariantPricing} from "../types/pricing" +import {buildQuery, isString, setMetadata} from "../utils" import { PricingService, ProductService, @@ -461,9 +466,7 @@ class LineItemService extends TransactionBaseService { let lineItems = await this.list(selector) if (!lineItems.length) { - const selectorConstraints = Object.entries(selector) - .map(([key, value]) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/services/order-edit.ts b/packages/medusa/src/services/order-edit.ts index 5b826646516ad..f8f833e98fdd3 100644 --- a/packages/medusa/src/services/order-edit.ts +++ b/packages/medusa/src/services/order-edit.ts @@ -15,6 +15,7 @@ import { FindOptionsWhere, ILike, IsNull, + Not, } from "typeorm" import { FindConfig, Selector } from "../types/common" import { @@ -753,9 +754,12 @@ export default class OrderEditService extends TransactionBaseService { const lineItemServiceTx = this.lineItemService_.withTransaction(manager) - const [lineItems] = await promiseAll([ + const [originalOrderLineItems] = await promiseAll([ lineItemServiceTx.update( - { order_id: orderEdit.order_id }, + [ + { order_id: orderEdit.order_id, order_edit_id: Not(orderEditId) }, + { order_id: orderEdit.order_id, order_edit_id: IsNull() }, + ], { order_id: null } ), lineItemServiceTx.update( @@ -770,7 +774,7 @@ export default class OrderEditService extends TransactionBaseService { orderEdit = await orderEditRepository.save(orderEdit) if (this.inventoryService_) { - const itemsIds = lineItems.map((i) => i.id) + const itemsIds = originalOrderLineItems.map((i) => i.id) await this.inventoryService_!.deleteReservationItemsByLineItem( itemsIds, { diff --git a/packages/medusa/src/services/order.ts b/packages/medusa/src/services/order.ts index 59b61faa86170..d8d9fc0fc9716 100644 --- a/packages/medusa/src/services/order.ts +++ b/packages/medusa/src/services/order.ts @@ -5,7 +5,7 @@ import { FlagRouter, isDefined, MedusaError, - promiseAll, + promiseAll, selectorConstraintsToString, } from "@medusajs/utils" import { EntityManager, @@ -453,9 +453,8 @@ class OrderService extends TransactionBaseService { const raw = await orderRepo.findOneWithRelations(rels, query) if (!raw) { - const selectorConstraints = Object.entries(selector) - .map((key, value) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) + throw new MedusaError( MedusaError.Types.NOT_FOUND, `Order with ${selectorConstraints} was not found` diff --git a/packages/medusa/src/services/product-category.ts b/packages/medusa/src/services/product-category.ts index 36e74db301c0d..be46974641938 100644 --- a/packages/medusa/src/services/product-category.ts +++ b/packages/medusa/src/services/product-category.ts @@ -17,6 +17,7 @@ import { UpdateProductCategoryInput, } from "../types/product-category" import { buildQuery, nullableValue, setMetadata } from "../utils" +import {selectorConstraintsToString} from "@medusajs/utils"; type InjectedDependencies = { manager: EntityManager @@ -115,9 +116,7 @@ class ProductCategoryService extends TransactionBaseService { ) if (!productCategory) { - const selectorConstraints = Object.entries(selector) - .map(([key, value]) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/services/product.ts b/packages/medusa/src/services/product.ts index 58b580ea57f7d..d6220613809f0 100644 --- a/packages/medusa/src/services/product.ts +++ b/packages/medusa/src/services/product.ts @@ -3,7 +3,7 @@ import { buildSelects, FlagRouter, objectToStringPath, - promiseAll, + promiseAll, selectorConstraintsToString, } from "@medusajs/utils" import { isDefined, MedusaError } from "medusa-core-utils" import { EntityManager, In } from "typeorm" @@ -306,9 +306,7 @@ class ProductService extends TransactionBaseService { ) if (!product) { - const selectorConstraints = Object.entries(selector) - .map(([key, value]) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/services/publishable-api-key.ts b/packages/medusa/src/services/publishable-api-key.ts index e8ffa007f17b2..18c88cb175a30 100644 --- a/packages/medusa/src/services/publishable-api-key.ts +++ b/packages/medusa/src/services/publishable-api-key.ts @@ -12,6 +12,7 @@ import { } from "../types/publishable-api-key" import { buildQuery, isString } from "../utils" import EventBusService from "./event-bus" +import {selectorConstraintsToString} from "@medusajs/utils"; type InjectedDependencies = { manager: EntityManager @@ -123,9 +124,7 @@ class PublishableApiKeyService extends TransactionBaseService { const publishableApiKey = await repo.findOne(query) if (!publishableApiKey) { - const selectorConstraints = Object.entries(selector) - .map((key, value) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/services/sales-channel.ts b/packages/medusa/src/services/sales-channel.ts index b9d902ef2881d..41e7c4d4f30f2 100644 --- a/packages/medusa/src/services/sales-channel.ts +++ b/packages/medusa/src/services/sales-channel.ts @@ -12,6 +12,7 @@ import { SalesChannelRepository } from "../repositories/sales-channel" import { buildQuery } from "../utils" import EventBusService from "./event-bus" import StoreService from "./store" +import {selectorConstraintsToString} from "@medusajs/utils"; type InjectedDependencies = { salesChannelRepository: typeof SalesChannelRepository @@ -67,9 +68,7 @@ class SalesChannelService extends TransactionBaseService { }) if (!salesChannel) { - const selectorConstraints = Object.entries(selector) - .map(([key, value]) => `${key}: ${value}`) - .join(", ") + const selectorConstraints = selectorConstraintsToString(selector) throw new MedusaError( MedusaError.Types.NOT_FOUND, diff --git a/packages/medusa/src/types/common.ts b/packages/medusa/src/types/common.ts index a8aaecdf414b6..381214cfbe922 100644 --- a/packages/medusa/src/types/common.ts +++ b/packages/medusa/src/types/common.ts @@ -14,13 +14,11 @@ import { IsObject, IsOptional, IsString, - Validate, } from "class-validator" import { Transform, Type } from "class-transformer" import { BaseEntity } from "../interfaces" import { ClassConstructor } from "./global" -import { ExactlyOne } from "./validators/exactly-one" import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder" import { FindOptionsRelations } from "typeorm/find-options/FindOptionsRelations" import { transformDate } from "../utils/validators/date-transform" @@ -70,7 +68,7 @@ export type TreeQuerySelector = QuerySelector & { include_descendants_tree?: boolean } -export type Selector = { +type InnerSelector = { [key in keyof TEntity]?: | TEntity[key] | TEntity[key][] @@ -80,6 +78,10 @@ export type Selector = { | FindOperator } +export type Selector = + | InnerSelector + | InnerSelector[] + export type TotalField = | "shipping_total" | "discount_total" diff --git a/packages/medusa/src/utils/build-query.ts b/packages/medusa/src/utils/build-query.ts index c1d3b89ddf6f3..7a6cda5cb419c 100644 --- a/packages/medusa/src/utils/build-query.ts +++ b/packages/medusa/src/utils/build-query.ts @@ -99,8 +99,20 @@ export function buildQuery( */ function buildWhere( constraints: TWhereKeys -): FindOptionsWhere { - const where: FindOptionsWhere = {} +): FindOptionsWhere | FindOptionsWhere[] { + let where: FindOptionsWhere | FindOptionsWhere[] = {} + + if (Array.isArray(constraints)) { + where = [] + constraints.forEach((constraint) => { + ;(where as FindOptionsWhere[]).push( + buildWhere(constraint) as FindOptionsWhere + ) + }) + + return where + } + for (const [key, value] of Object.entries(constraints)) { if (value === undefined) { continue diff --git a/packages/stock-location/src/utils/build-query.ts b/packages/stock-location/src/utils/build-query.ts index 1f92c76f8a12c..ee0fb07fe9bc2 100644 --- a/packages/stock-location/src/utils/build-query.ts +++ b/packages/stock-location/src/utils/build-query.ts @@ -97,8 +97,20 @@ export function buildQuery( */ function buildWhere( constraints: TWhereKeys -): FindOptionsWhere { - const where: FindOptionsWhere = {} +): FindOptionsWhere | FindOptionsWhere[] { + let where: FindOptionsWhere | FindOptionsWhere[] = {} + + if (Array.isArray(constraints)) { + where = [] + constraints.forEach((constraint) => { + ;(where as FindOptionsWhere[]).push( + buildWhere(constraint) as FindOptionsWhere + ) + }) + + return where + } + for (const [key, value] of Object.entries(constraints)) { if (value === undefined) { continue diff --git a/packages/utils/src/common/index.ts b/packages/utils/src/common/index.ts index 509d40b162368..b878492f04be0 100644 --- a/packages/utils/src/common/index.ts +++ b/packages/utils/src/common/index.ts @@ -24,6 +24,7 @@ export * from "./promise-all" export * from "./remote-query-object-from-string" export * from "./remote-query-object-to-string" export * from "./remove-nullisih" +export * from "./selector-constraints-to-string" export * from "./set-metadata" export * from "./simple-hash" export * from "./string-to-select-relation-object" diff --git a/packages/utils/src/common/selector-constraints-to-string.ts b/packages/utils/src/common/selector-constraints-to-string.ts new file mode 100644 index 0000000000000..2046d91f111d0 --- /dev/null +++ b/packages/utils/src/common/selector-constraints-to-string.ts @@ -0,0 +1,16 @@ +export function selectorConstraintsToString( + selector: object | object[] +): string { + const selectors = Array.isArray(selector) ? selector : [selector] + + return selectors + .map((selector_) => { + return Object.entries(selector_) + .map( + ([key, value]: [string, any]) => + `${key}: ${value._type ? `${value._type}(${value._value})` : value}` + ) + .join(", ") + }) + .join(" or ") +}