fix(medusa): Order edit confirmation conflict line items update (#5867)
* fix(medusa): Order edit confirmation conflict line items update * naming * Create serious-flowers-provide.md * another proposal * fixes
This commit is contained in:
committed by
GitHub
parent
1c6c759aa8
commit
2826605b01
5
.changeset/serious-flowers-provide.md
Normal file
5
.changeset/serious-flowers-provide.md
Normal file
@@ -0,0 +1,5 @@
|
||||
---
|
||||
"@medusajs/medusa": patch
|
||||
---
|
||||
|
||||
fix(medusa): Order edit confirmation conflict line items update
|
||||
@@ -97,8 +97,20 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
): FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] {
|
||||
let where: FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] = {}
|
||||
|
||||
if (Array.isArray(constraints)) {
|
||||
where = []
|
||||
constraints.forEach((constraint) => {
|
||||
;(where as FindOptionsWhere<TEntity>[]).push(
|
||||
buildWhere(constraint) as FindOptionsWhere<TEntity>
|
||||
)
|
||||
})
|
||||
|
||||
return where
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
{
|
||||
|
||||
@@ -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`
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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,
|
||||
|
||||
@@ -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<TEntity> = QuerySelector<TEntity> & {
|
||||
include_descendants_tree?: boolean
|
||||
}
|
||||
|
||||
export type Selector<TEntity> = {
|
||||
type InnerSelector<TEntity> = {
|
||||
[key in keyof TEntity]?:
|
||||
| TEntity[key]
|
||||
| TEntity[key][]
|
||||
@@ -80,6 +78,10 @@ export type Selector<TEntity> = {
|
||||
| FindOperator<TEntity[key][] | string | string[]>
|
||||
}
|
||||
|
||||
export type Selector<TEntity> =
|
||||
| InnerSelector<TEntity>
|
||||
| InnerSelector<TEntity>[]
|
||||
|
||||
export type TotalField =
|
||||
| "shipping_total"
|
||||
| "discount_total"
|
||||
|
||||
@@ -99,8 +99,20 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
): FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] {
|
||||
let where: FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] = {}
|
||||
|
||||
if (Array.isArray(constraints)) {
|
||||
where = []
|
||||
constraints.forEach((constraint) => {
|
||||
;(where as FindOptionsWhere<TEntity>[]).push(
|
||||
buildWhere(constraint) as FindOptionsWhere<TEntity>
|
||||
)
|
||||
})
|
||||
|
||||
return where
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
|
||||
@@ -97,8 +97,20 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
*/
|
||||
function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
constraints: TWhereKeys
|
||||
): FindOptionsWhere<TEntity> {
|
||||
const where: FindOptionsWhere<TEntity> = {}
|
||||
): FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] {
|
||||
let where: FindOptionsWhere<TEntity> | FindOptionsWhere<TEntity>[] = {}
|
||||
|
||||
if (Array.isArray(constraints)) {
|
||||
where = []
|
||||
constraints.forEach((constraint) => {
|
||||
;(where as FindOptionsWhere<TEntity>[]).push(
|
||||
buildWhere(constraint) as FindOptionsWhere<TEntity>
|
||||
)
|
||||
})
|
||||
|
||||
return where
|
||||
}
|
||||
|
||||
for (const [key, value] of Object.entries(constraints)) {
|
||||
if (value === undefined) {
|
||||
continue
|
||||
|
||||
@@ -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"
|
||||
|
||||
16
packages/utils/src/common/selector-constraints-to-string.ts
Normal file
16
packages/utils/src/common/selector-constraints-to-string.ts
Normal file
@@ -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 ")
|
||||
}
|
||||
Reference in New Issue
Block a user