chore(utils): clean util package deps (#4146)
This commit is contained in:
1 parent
f47b11293e
commit
db41995307
36 files changed
+593
-996
No files matched your search
@@ -1,4 +1,4 @@
|
||||
import { TransactionBaseService } from "@medusajs/utils"
|
||||
import { TransactionBaseService } from "./transaction-base-service"
|
||||
import { IdempotencyKey } from "../models"
|
||||
import { RequestContext } from "../types/request"
|
||||
|
||||
|
||||
@@ -82,7 +82,9 @@ export const CustomerGroupRepository = dataSource
|
||||
: { ...idsOrOptionsWithoutRelations.order }
|
||||
const originalSelect = isOptionsArray
|
||||
? undefined
|
||||
: objectToStringPath(idsOrOptionsWithoutRelations.select)
|
||||
: (objectToStringPath(
|
||||
idsOrOptionsWithoutRelations.select
|
||||
) as (keyof CustomerGroup)[])
|
||||
const clonedOptions = isOptionsArray
|
||||
? idsOrOptionsWithoutRelations
|
||||
: cloneDeep(idsOrOptionsWithoutRelations)
|
||||
|
||||
@@ -479,7 +479,9 @@ export const ProductRepository = dataSource.getRepository(Product).extend({
|
||||
: { ...idsOrOptionsWithoutRelations.order }
|
||||
const originalSelect = isOptionsArray
|
||||
? undefined
|
||||
: objectToStringPath(idsOrOptionsWithoutRelations.select)
|
||||
: (objectToStringPath(
|
||||
idsOrOptionsWithoutRelations.select
|
||||
) as (keyof Product)[])
|
||||
const clonedOptions = isOptionsArray
|
||||
? idsOrOptionsWithoutRelations
|
||||
: cloneDeep(idsOrOptionsWithoutRelations)
|
||||
|
||||
@@ -35,7 +35,7 @@ export const TaxRateRepository = dataSource.getRepository(TaxRate).extend({
|
||||
const selectableCols: (keyof TaxRate)[] = []
|
||||
const legacySelect = objectToStringPath(
|
||||
findOptions.select as FindOptionsSelect<TaxRate>
|
||||
)
|
||||
) as (keyof TaxRate)[]
|
||||
for (const k of legacySelect) {
|
||||
if (!resolveableFields.includes(k)) {
|
||||
selectableCols.push(k)
|
||||
|
||||
@@ -1,5 +1,10 @@
|
||||
import { IInventoryService } from "@medusajs/types"
|
||||
import { isDefined, MedusaError, TransactionBaseService } from "@medusajs/utils"
|
||||
import {
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
isDefined,
|
||||
MedusaError,
|
||||
} from "@medusajs/utils"
|
||||
import {
|
||||
EntityManager,
|
||||
FindManyOptions,
|
||||
@@ -45,6 +50,7 @@ import {
|
||||
Swap,
|
||||
TrackingLink,
|
||||
} from "../models"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { AddressRepository } from "../repositories/address"
|
||||
import { OrderRepository } from "../repositories/order"
|
||||
import { FindConfig, QuerySelector, Selector } from "../types/common"
|
||||
@@ -54,13 +60,7 @@ import {
|
||||
} from "../types/fulfillment"
|
||||
import { TotalsContext, UpdateOrderInput } from "../types/orders"
|
||||
import { CreateShippingMethodDto } from "../types/shipping-options"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
isString,
|
||||
setMetadata,
|
||||
} from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import EventBusService from "./event-bus"
|
||||
|
||||
|
||||
@@ -10,8 +10,9 @@ import {
|
||||
ReserveQuantityContext,
|
||||
} from "@medusajs/types"
|
||||
import { LineItem, Product, ProductVariant } from "../models"
|
||||
import { MedusaError, TransactionBaseService, isDefined } from "@medusajs/utils"
|
||||
import { MedusaError, isDefined } from "@medusajs/utils"
|
||||
import { PricedProduct, PricedVariant } from "../types/pricing"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
|
||||
import { ProductVariantInventoryItem } from "../models/product-variant-inventory-item"
|
||||
import ProductVariantService from "./product-variant"
|
||||
|
||||
@@ -40,7 +40,6 @@ import {
|
||||
} from "../types/product-variant"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelations,
|
||||
hasChanges,
|
||||
isObject,
|
||||
isString,
|
||||
@@ -54,6 +53,7 @@ import { ProductRepository } from "../repositories/product"
|
||||
import { ProductOptionValueRepository } from "../repositories/product-option-value"
|
||||
import EventBusService from "./event-bus"
|
||||
import RegionService from "./region"
|
||||
import { buildRelations } from "@medusajs/utils"
|
||||
|
||||
class ProductVariantService extends TransactionBaseService {
|
||||
static Events = {
|
||||
|
||||
@@ -31,15 +31,14 @@ import {
|
||||
ProductSelector,
|
||||
UpdateProductInput,
|
||||
} from "../types/product"
|
||||
import {
|
||||
buildQuery,
|
||||
buildRelationsOrSelect,
|
||||
isString,
|
||||
setMetadata,
|
||||
} from "../utils"
|
||||
import { buildQuery, isString, setMetadata } from "../utils"
|
||||
import { FlagRouter } from "../utils/flag-router"
|
||||
import EventBusService from "./event-bus"
|
||||
import { objectToStringPath } from "@medusajs/utils"
|
||||
import {
|
||||
buildRelations,
|
||||
buildSelects,
|
||||
objectToStringPath,
|
||||
} from "@medusajs/utils"
|
||||
|
||||
type InjectedDependencies = {
|
||||
manager: EntityManager
|
||||
@@ -967,11 +966,11 @@ class ProductService extends TransactionBaseService {
|
||||
query.order = config.order
|
||||
|
||||
if (config.relations && config.relations.length > 0) {
|
||||
query.relations = buildRelationsOrSelect(config.relations)
|
||||
query.relations = buildRelations(config.relations)
|
||||
}
|
||||
|
||||
if (config.select && config.select.length > 0) {
|
||||
query.select = buildRelationsOrSelect(config.select)
|
||||
query.select = buildSelects(config.select)
|
||||
}
|
||||
|
||||
const rels = objectToStringPath(query.relations)
|
||||
|
||||
@@ -1,5 +1,5 @@
|
||||
import { EventBusTypes, IInventoryService } from "@medusajs/types"
|
||||
import { TransactionBaseService } from "@medusajs/utils"
|
||||
import { TransactionBaseService } from "../interfaces"
|
||||
import { EntityManager } from "typeorm"
|
||||
import SalesChannelLocationService from "./sales-channel-location"
|
||||
|
||||
|
||||
@@ -17,6 +17,17 @@ import { ExtendedFindConfig, FindConfig } from "../types/common"
|
||||
|
||||
import { FindOptionsOrder } from "typeorm/find-options/FindOptionsOrder"
|
||||
import { isObject } from "./is-object"
|
||||
import { buildOrder, buildRelations, buildSelects } from "@medusajs/utils"
|
||||
|
||||
const operatorsMap = {
|
||||
lt: (value) => LessThan(value),
|
||||
gt: (value) => MoreThan(value),
|
||||
lte: (value) => LessThanOrEqual(value),
|
||||
gte: (value) => MoreThanOrEqual(value),
|
||||
contains: (value) => ILike(`%${value}%`),
|
||||
starts_with: (value) => ILike(`${value}%`),
|
||||
ends_with: (value) => ILike(`%${value}`),
|
||||
}
|
||||
|
||||
/**
|
||||
* Used to build TypeORM queries.
|
||||
@@ -45,15 +56,19 @@ export function buildQuery<TWhereKeys extends object, TEntity = unknown>(
|
||||
}
|
||||
|
||||
if (config.relations) {
|
||||
query.relations = buildRelations<TEntity>(config.relations)
|
||||
query.relations = buildRelations(
|
||||
config.relations
|
||||
) as FindOptionsRelations<TEntity>
|
||||
}
|
||||
|
||||
if (config.select) {
|
||||
query.select = buildSelects<TEntity>(config.select as string[])
|
||||
query.select = buildSelects(
|
||||
config.select as string[]
|
||||
) as FindOptionsSelect<TEntity>
|
||||
}
|
||||
|
||||
if (config.order) {
|
||||
query.order = buildOrder<TEntity>(config.order)
|
||||
query.order = buildOrder(config.order) as FindOptionsOrder<TEntity>
|
||||
}
|
||||
|
||||
return query
|
||||
@@ -109,34 +124,14 @@ function buildWhere<TWhereKeys extends object, TEntity>(
|
||||
if (typeof value === "object") {
|
||||
Object.entries(value).forEach(([objectKey, objectValue]) => {
|
||||
where[key] = where[key] || []
|
||||
switch (objectKey) {
|
||||
case "lt":
|
||||
where[key].push(LessThan(objectValue))
|
||||
break
|
||||
case "gt":
|
||||
where[key].push(MoreThan(objectValue))
|
||||
break
|
||||
case "lte":
|
||||
where[key].push(LessThanOrEqual(objectValue))
|
||||
break
|
||||
case "gte":
|
||||
where[key].push(MoreThanOrEqual(objectValue))
|
||||
break
|
||||
case "contains":
|
||||
where[key].push(ILike(`%${objectValue}%`))
|
||||
break
|
||||
case "starts_with":
|
||||
where[key].push(ILike(`${objectValue}%`))
|
||||
break
|
||||
case "ends_with":
|
||||
where[key].push(ILike(`%${objectValue}`))
|
||||
break
|
||||
default:
|
||||
if (objectValue != undefined && typeof objectValue === "object") {
|
||||
where[key] = buildWhere<any, TEntity>(objectValue)
|
||||
return
|
||||
}
|
||||
where[key] = value
|
||||
if (operatorsMap[objectKey]) {
|
||||
where[key].push(operatorsMap[objectKey](objectValue))
|
||||
} else {
|
||||
if (objectValue != undefined && typeof objectValue === "object") {
|
||||
where[key] = buildWhere<any, TEntity>(objectValue)
|
||||
return
|
||||
}
|
||||
where[key] = value
|
||||
}
|
||||
return
|
||||
})
|
||||
@@ -209,20 +204,6 @@ export function buildLegacyFieldsListFrom<TEntity>(
|
||||
return Array.from(output) as (keyof TEntity)[]
|
||||
}
|
||||
|
||||
export function buildSelects<TEntity>(
|
||||
selectCollection: string[]
|
||||
): FindOptionsSelect<TEntity> {
|
||||
return buildRelationsOrSelect(selectCollection) as FindOptionsSelect<TEntity>
|
||||
}
|
||||
|
||||
export function buildRelations<TEntity>(
|
||||
relationCollection: string[]
|
||||
): FindOptionsRelations<TEntity> {
|
||||
return buildRelationsOrSelect(
|
||||
relationCollection
|
||||
) as FindOptionsRelations<TEntity>
|
||||
}
|
||||
|
||||
export function addOrderToSelect<TEntity>(
|
||||
order: FindOptionsOrder<TEntity>,
|
||||
select: FindOptionsSelect<TEntity>
|
||||
@@ -241,125 +222,6 @@ export function addOrderToSelect<TEntity>(
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an collection of dot string into a nested object
|
||||
* @example
|
||||
* input: [
|
||||
* order,
|
||||
* order.items,
|
||||
* order.swaps,
|
||||
* order.swaps.additional_items,
|
||||
* order.discounts,
|
||||
* order.discounts.rule,
|
||||
* order.claims,
|
||||
* order.claims.additional_items,
|
||||
* additional_items,
|
||||
* additional_items.variant,
|
||||
* return_order,
|
||||
* return_order.items,
|
||||
* return_order.shipping_method,
|
||||
* return_order.shipping_method.tax_lines
|
||||
* ]
|
||||
* output: {
|
||||
* "order": {
|
||||
* "items": true,
|
||||
* "swaps": {
|
||||
* "additional_items": true
|
||||
* },
|
||||
* "discounts": {
|
||||
* "rule": true
|
||||
* },
|
||||
* "claims": {
|
||||
* "additional_items": true
|
||||
* }
|
||||
* },
|
||||
* "additional_items": {
|
||||
* "variant": true
|
||||
* },
|
||||
* "return_order": {
|
||||
* "items": true,
|
||||
* "shipping_method": {
|
||||
* "tax_lines": true
|
||||
* }
|
||||
* }
|
||||
* }
|
||||
* @param collection
|
||||
*/
|
||||
export function buildRelationsOrSelect<TEntity>(
|
||||
collection: string[]
|
||||
): FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> {
|
||||
const output: FindOptionsRelations<TEntity> | FindOptionsSelect<TEntity> = {}
|
||||
|
||||
for (const relation of collection) {
|
||||
if (relation.indexOf(".") > -1) {
|
||||
const nestedRelations = relation.split(".")
|
||||
|
||||
let parent = output
|
||||
|
||||
while (nestedRelations.length > 1) {
|
||||
const nestedRelation = nestedRelations.shift() as string
|
||||
parent = parent[nestedRelation] =
|
||||
parent[nestedRelation] !== true &&
|
||||
typeof parent[nestedRelation] === "object"
|
||||
? parent[nestedRelation]
|
||||
: {}
|
||||
}
|
||||
|
||||
parent[nestedRelations[0]] = true
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
output[relation] = output[relation] ?? true
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert an order of dot string into a nested object
|
||||
* @example
|
||||
* input: { id: "ASC", "items.title": "ASC", "items.variant.title": "ASC" }
|
||||
* output: {
|
||||
* "id": "ASC",
|
||||
* "items": {
|
||||
* "id": "ASC",
|
||||
* "variant": {
|
||||
* "title": "ASC"
|
||||
* }
|
||||
* },
|
||||
* }
|
||||
* @param orderBy
|
||||
*/
|
||||
function buildOrder<TEntity>(orderBy: {
|
||||
[k: string]: "ASC" | "DESC"
|
||||
}): FindOptionsOrder<TEntity> {
|
||||
const output: FindOptionsOrder<TEntity> = {}
|
||||
|
||||
const orderKeys = Object.keys(orderBy)
|
||||
|
||||
for (const order of orderKeys) {
|
||||
if (order.indexOf(".") > -1) {
|
||||
const nestedOrder = order.split(".")
|
||||
|
||||
let parent = output
|
||||
|
||||
while (nestedOrder.length > 1) {
|
||||
const nestedRelation = nestedOrder.shift() as string
|
||||
parent = parent[nestedRelation] = parent[nestedRelation] ?? {}
|
||||
}
|
||||
|
||||
parent[nestedOrder[0]] = orderBy[order]
|
||||
|
||||
continue
|
||||
}
|
||||
|
||||
output[order] = orderBy[order]
|
||||
}
|
||||
|
||||
return output
|
||||
}
|
||||
|
||||
export function nullableValue(value: any): FindOperator<any> {
|
||||
if (value === null) {
|
||||
return IsNull()
|
||||
|
||||
Reference in new issue
Block a user