chore(medusa): Migrate price list repository (#3725)

* chore(medusa): Migrate price list repository

* Create grumpy-cars-tease.md

* fixes

* fix

* fix

* cleanup

---------

Co-authored-by: Riqwan Thamir <rmthamir@gmail.com>
This commit is contained in:
Adrien de Peretti
2023-04-08 18:54:08 +02:00
committed by GitHub
co-authored by Riqwan Thamir
parent 1a60c6f58d
commit 282e239dfc
3 changed files with 51 additions and 98 deletions
+5
View File
@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
chore(medusa): Migrate price list repository
+41 -22
View File
@@ -1,39 +1,58 @@
import { FindOptionsWhere, ILike } from "typeorm" import { FindOperator, FindOptionsWhere, ILike, In } from "typeorm"
import { PriceList } from "../models" import { PriceList } from "../models"
import { CustomFindOptions, ExtendedFindConfig } from "../types/common" import { ExtendedFindConfig } from "../types/common"
import { dataSource } from "../loaders/database" import { dataSource } from "../loaders/database"
export type PriceListFindOptions = CustomFindOptions<
PriceList,
"status" | "type"
>
export const PriceListRepository = dataSource.getRepository(PriceList).extend({ export const PriceListRepository = dataSource.getRepository(PriceList).extend({
async listAndCount( async listAndCount(
query: ExtendedFindConfig<PriceList>, query: ExtendedFindConfig<PriceList>,
q?: string q?: string
): Promise<[PriceList[], number]> { ): Promise<[PriceList[], number]> {
const query_ = { ...query } const query_ = { ...query }
query_.relationLoadStrategy = "query"
query_.where = query.where as FindOptionsWhere<PriceList>
if (q) { const groups = query_.where.customer_groups as unknown as FindOperator<
query_.where = query_.where as FindOptionsWhere<PriceList> string[]
delete query_.where.description >
delete query_.where.name delete query_.where.customer_groups
query_.where.description = ILike(`%${q}%`) if (groups || q) {
query_.where.name = ILike(`%${q}%`) query_.relations = query_.relations ?? {}
query_.where.customer_groups = { query_.relations.customer_groups =
name: ILike(`%${q}%`), query_.relations.customer_groups ?? true
if (groups) {
query_.where.customer_groups = {
id: In(groups.value),
}
} }
} }
return await this.findAndCount({ if (q) {
...query_, const groupsWhere = query_.where.customer_groups ?? {}
relations: {
customer_groups: true, query_.where = query_.where ?? {}
}, query_.where = [
relationLoadStrategy: "query", {
}) ...query_.where,
name: ILike(`%${q}%`),
},
{
...query_.where,
description: ILike(`%${q}%`),
},
{
...query_.where,
customer_groups: {
...groupsWhere,
name: ILike(`%${q}%`),
},
},
]
}
return await this.findAndCount(query_)
}, },
}) })
+5 -76
View File
@@ -1,18 +1,10 @@
import { isDefined, MedusaError } from "medusa-core-utils" import { isDefined, MedusaError } from "medusa-core-utils"
import { import { DeepPartial, EntityManager } from "typeorm"
DeepPartial,
EntityManager,
FindManyOptions,
FindOperator,
FindOptionsWhere,
ILike,
In,
} from "typeorm"
import { CustomerGroupService } from "." import { CustomerGroupService } from "."
import { CustomerGroup, PriceList, Product, ProductVariant } from "../models" import { CustomerGroup, PriceList, Product, ProductVariant } from "../models"
import { MoneyAmountRepository } from "../repositories/money-amount" import { MoneyAmountRepository } from "../repositories/money-amount"
import { PriceListRepository } from "../repositories/price-list" import { PriceListRepository } from "../repositories/price-list"
import { ExtendedFindConfig, FindConfig, Selector } from "../types/common" import { FindConfig, Selector } from "../types/common"
import { import {
CreatePriceListInput, CreatePriceListInput,
FilterablePriceListProps, FilterablePriceListProps,
@@ -293,25 +285,9 @@ class PriceListService extends TransactionBaseService {
*/ */
async list( async list(
selector: FilterablePriceListProps = {}, selector: FilterablePriceListProps = {},
config: FindConfig<FilterablePriceListProps> = { skip: 0, take: 20 } config: FindConfig<PriceList> = { skip: 0, take: 20 }
): Promise<PriceList[]> { ): Promise<PriceList[]> {
const priceListRepo = this.activeManager_.withRepository( const [priceLists] = await this.listAndCount(selector, config)
this.priceListRepo_
)
const { q, ...priceListSelector } = selector
const query = buildQuery(
priceListSelector,
config
) as FindManyOptions<FilterablePriceListProps> & {
where: { customer_groups?: FindOperator<string[]> }
} & ExtendedFindConfig<FilterablePriceListProps>
const groups = query.where.customer_groups
query.where.customer_groups = undefined
const [priceLists] = await priceListRepo.listAndCount(query as any)
return priceLists return priceLists
} }
@@ -333,55 +309,8 @@ class PriceListService extends TransactionBaseService {
) )
const { q, ...priceListSelector } = selector const { q, ...priceListSelector } = selector
const query = buildQuery(priceListSelector, config) const query = buildQuery(priceListSelector, config)
query.where = query.where as FindOptionsWhere<PriceList>
const groups = query.where.customer_groups as unknown as FindOperator< return await priceListRepo.listAndCount(query, q)
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)
} }
protected async upsertCustomerGroups_( protected async upsertCustomerGroups_(