Skip to content

Commit

Permalink
Merge branch 'develop' into fix/customer-relations
Browse files Browse the repository at this point in the history
  • Loading branch information
pKorsholm committed Dec 14, 2023
2 parents c4d73d1 + 2826605 commit 16a8c49
Show file tree
Hide file tree
Showing 16 changed files with 111 additions and 51 deletions.
5 changes: 5 additions & 0 deletions .changeset/serious-flowers-provide.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---

fix(medusa): Order edit confirmation conflict line items update
16 changes: 14 additions & 2 deletions packages/inventory/src/utils/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 3 additions & 3 deletions packages/medusa/src/services/customer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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`
Expand Down
5 changes: 2 additions & 3 deletions packages/medusa/src/services/gift-card.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
35 changes: 19 additions & 16 deletions packages/medusa/src/services/line-item.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -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,
Expand Down
10 changes: 7 additions & 3 deletions packages/medusa/src/services/order-edit.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import {
FindOptionsWhere,
ILike,
IsNull,
Not,
} from "typeorm"
import { FindConfig, Selector } from "../types/common"
import {
Expand Down Expand Up @@ -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(
Expand All @@ -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,
{
Expand Down
7 changes: 3 additions & 4 deletions packages/medusa/src/services/order.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ import {
FlagRouter,
isDefined,
MedusaError,
promiseAll,
promiseAll, selectorConstraintsToString,
} from "@medusajs/utils"
import {
EntityManager,
Expand Down Expand Up @@ -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`
Expand Down
5 changes: 2 additions & 3 deletions packages/medusa/src/services/product-category.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
6 changes: 2 additions & 4 deletions packages/medusa/src/services/product.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions packages/medusa/src/services/publishable-api-key.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions packages/medusa/src/services/sales-channel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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,
Expand Down
8 changes: 5 additions & 3 deletions packages/medusa/src/types/common.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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][]
Expand All @@ -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"
Expand Down
16 changes: 14 additions & 2 deletions packages/medusa/src/utils/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
16 changes: 14 additions & 2 deletions packages/stock-location/src/utils/build-query.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions packages/utils/src/common/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down
16 changes: 16 additions & 0 deletions packages/utils/src/common/selector-constraints-to-string.ts
Original file line number Diff line number Diff line change
@@ -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 ")
}

0 comments on commit 16a8c49

Please sign in to comment.