diff --git a/.changeset/grumpy-cars-tease.md b/.changeset/grumpy-cars-tease.md new file mode 100644 index 0000000000..af83a78827 --- /dev/null +++ b/.changeset/grumpy-cars-tease.md @@ -0,0 +1,5 @@ +--- +"@medusajs/medusa": patch +--- + +chore(medusa): Migrate price list repository diff --git a/packages/medusa/src/repositories/price-list.ts b/packages/medusa/src/repositories/price-list.ts index 78afeffeb0..ed57a4c384 100644 --- a/packages/medusa/src/repositories/price-list.ts +++ b/packages/medusa/src/repositories/price-list.ts @@ -1,39 +1,58 @@ -import { FindOptionsWhere, ILike } from "typeorm" +import { FindOperator, FindOptionsWhere, ILike, In } from "typeorm" import { PriceList } from "../models" -import { CustomFindOptions, ExtendedFindConfig } from "../types/common" +import { ExtendedFindConfig } from "../types/common" import { dataSource } from "../loaders/database" -export type PriceListFindOptions = CustomFindOptions< - PriceList, - "status" | "type" -> - export const PriceListRepository = dataSource.getRepository(PriceList).extend({ async listAndCount( query: ExtendedFindConfig, q?: string ): Promise<[PriceList[], number]> { const query_ = { ...query } + query_.relationLoadStrategy = "query" + query_.where = query.where as FindOptionsWhere - if (q) { - query_.where = query_.where as FindOptionsWhere - delete query_.where.description - delete query_.where.name + const groups = query_.where.customer_groups as unknown as FindOperator< + string[] + > + delete query_.where.customer_groups - query_.where.description = ILike(`%${q}%`) - query_.where.name = ILike(`%${q}%`) - query_.where.customer_groups = { - name: ILike(`%${q}%`), + if (groups || q) { + query_.relations = query_.relations ?? {} + query_.relations.customer_groups = + query_.relations.customer_groups ?? true + + if (groups) { + query_.where.customer_groups = { + id: In(groups.value), + } } } - return await this.findAndCount({ - ...query_, - relations: { - customer_groups: true, - }, - relationLoadStrategy: "query", - }) + if (q) { + const groupsWhere = query_.where.customer_groups ?? {} + + query_.where = query_.where ?? {} + query_.where = [ + { + ...query_.where, + name: ILike(`%${q}%`), + }, + { + ...query_.where, + description: ILike(`%${q}%`), + }, + { + ...query_.where, + customer_groups: { + ...groupsWhere, + name: ILike(`%${q}%`), + }, + }, + ] + } + + return await this.findAndCount(query_) }, }) diff --git a/packages/medusa/src/services/price-list.ts b/packages/medusa/src/services/price-list.ts index 4e79ab4121..c1e38228aa 100644 --- a/packages/medusa/src/services/price-list.ts +++ b/packages/medusa/src/services/price-list.ts @@ -1,18 +1,10 @@ import { isDefined, MedusaError } from "medusa-core-utils" -import { - DeepPartial, - EntityManager, - FindManyOptions, - FindOperator, - FindOptionsWhere, - ILike, - In, -} from "typeorm" +import { DeepPartial, EntityManager } from "typeorm" import { CustomerGroupService } from "." import { CustomerGroup, PriceList, Product, ProductVariant } from "../models" import { MoneyAmountRepository } from "../repositories/money-amount" import { PriceListRepository } from "../repositories/price-list" -import { ExtendedFindConfig, FindConfig, Selector } from "../types/common" +import { FindConfig, Selector } from "../types/common" import { CreatePriceListInput, FilterablePriceListProps, @@ -293,25 +285,9 @@ class PriceListService extends TransactionBaseService { */ async list( selector: FilterablePriceListProps = {}, - config: FindConfig = { skip: 0, take: 20 } + config: FindConfig = { skip: 0, take: 20 } ): Promise { - const priceListRepo = this.activeManager_.withRepository( - this.priceListRepo_ - ) - - const { q, ...priceListSelector } = selector - const query = buildQuery( - priceListSelector, - config - ) as FindManyOptions & { - where: { customer_groups?: FindOperator } - } & ExtendedFindConfig - - const groups = query.where.customer_groups - query.where.customer_groups = undefined - - const [priceLists] = await priceListRepo.listAndCount(query as any) - + const [priceLists] = await this.listAndCount(selector, config) return priceLists } @@ -333,55 +309,8 @@ class PriceListService extends TransactionBaseService { ) const { q, ...priceListSelector } = selector const query = buildQuery(priceListSelector, config) - query.where = query.where as FindOptionsWhere - const groups = query.where.customer_groups as unknown as FindOperator< - string[] - > - delete query.where.customer_groups - - if (groups) { - query.relations = query.relations ?? {} - query.relations.customer_groups = query.relations.customer_groups ?? true - - query.where.customer_groups = { - ...(query.where.customer_groups ?? {}), - id: In(groups.value), - } - } - - if (q) { - if (!groups) { - query.relations = query.relations ?? {} - query.relations.customer_groups = - query.relations.customer_groups ?? true - } - - const where = [ - { - ...query.where, - name: ILike(`%${q}%`), - }, - { - ...query.where, - description: ILike(`%${q}%`), - }, - { - ...query.where, - customer_groups: { - ...query.where.customer_groups, - name: ILike(`%${q}%`), - }, - }, - ] - - return await priceListRepo.findAndCount({ - ...query, - where, - }) - } - - return await priceListRepo.listAndCount(query) + return await priceListRepo.listAndCount(query, q) } protected async upsertCustomerGroups_(