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
parent 1a60c6f58d
commit 282e239dfc
3 changed files with 51 additions and 98 deletions

View File

@@ -0,0 +1,5 @@
---
"@medusajs/medusa": patch
---
chore(medusa): Migrate price list repository

View File

@@ -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<PriceList>,
q?: string
): Promise<[PriceList[], number]> {
const query_ = { ...query }
query_.relationLoadStrategy = "query"
query_.where = query.where as FindOptionsWhere<PriceList>
if (q) {
query_.where = query_.where as FindOptionsWhere<PriceList>
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_)
},
})

View File

@@ -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<FilterablePriceListProps> = { skip: 0, take: 20 }
config: FindConfig<PriceList> = { skip: 0, take: 20 }
): Promise<PriceList[]> {
const priceListRepo = this.activeManager_.withRepository(
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)
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<PriceList>
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_(