fix: includes variant prices when listing products using a search query (#1607)

* fix: return prices when lsiting products with a query

* add: integration test

* fix: integration test

* fix: move WithRequiredProperty to common.ts

* fix: comment
This commit is contained in:
Zakaria El Asri
2022-06-05 21:52:31 +01:00
committed by GitHub
parent abaf10b31d
commit 247ad6dc6d
4 changed files with 222 additions and 124 deletions
+73 -8
View File
@@ -1,5 +1,6 @@
import { flatten, groupBy, map, merge } from "lodash"
import {
Brackets,
EntityRepository,
FindManyOptions,
FindOperator,
@@ -8,8 +9,9 @@ import {
Repository,
} from "typeorm"
import { ProductTag } from ".."
import { Product } from "../models/product"
import { PriceList } from "../models/price-list"
import { Product } from "../models/product"
import { WithRequiredProperty } from "../types/common"
type DefaultWithoutRelations = Omit<FindManyOptions<Product>, "relations">
@@ -103,7 +105,9 @@ export class ProductRepository extends Repository<Product> {
return [entities, count]
}
private getGroupedRelations(relations: Array<keyof Product>): {
private getGroupedRelations(
relations: Array<keyof Product>
): {
[toplevel: string]: string[]
} {
const groupedRelations: { [toplevel: string]: string[] } = {}
@@ -227,15 +231,16 @@ export class ProductRepository extends Repository<Product> {
)
const entitiesAndRelations = entitiesIdsWithRelations.concat(entities)
const entitiesToReturn =
this.mergeEntitiesWithRelations(entitiesAndRelations)
const entitiesToReturn = this.mergeEntitiesWithRelations(
entitiesAndRelations
)
return [entitiesToReturn, count]
}
public async findWithRelations(
relations: Array<keyof Product> = [],
idsOrOptionsWithoutRelations: FindWithRelationsOptions = {},
idsOrOptionsWithoutRelations: FindWithRelationsOptions | string[] = {},
withDeleted = false
): Promise<Product[]> {
let entities: Product[]
@@ -257,7 +262,10 @@ export class ProductRepository extends Repository<Product> {
return []
}
if (relations.length === 0) {
if (
relations.length === 0 &&
!Array.isArray(idsOrOptionsWithoutRelations)
) {
return await this.findByIds(entitiesIds, idsOrOptionsWithoutRelations)
}
@@ -269,8 +277,9 @@ export class ProductRepository extends Repository<Product> {
)
const entitiesAndRelations = entitiesIdsWithRelations.concat(entities)
const entitiesToReturn =
this.mergeEntitiesWithRelations(entitiesAndRelations)
const entitiesToReturn = this.mergeEntitiesWithRelations(
entitiesAndRelations
)
return entitiesToReturn
}
@@ -314,4 +323,60 @@ export class ProductRepository extends Repository<Product> {
return this.findByIds(productIds)
}
public async getFreeTextSearchResultsAndCount(
q: string,
options: CustomOptions = { where: {} },
relations: (keyof Product)[] = []
): Promise<[Product[], number]> {
const cleanedOptions = this._cleanOptions(options)
let qb = this.createQueryBuilder("product")
.leftJoinAndSelect("product.variants", "variant")
.leftJoinAndSelect("product.collection", "collection")
.select(["product.id"])
.where(cleanedOptions.where)
.andWhere(
new Brackets((qb) => {
qb.where(`product.description ILIKE :q`, { q: `%${q}%` })
.orWhere(`product.title ILIKE :q`, { q: `%${q}%` })
.orWhere(`variant.title ILIKE :q`, { q: `%${q}%` })
.orWhere(`variant.sku ILIKE :q`, { q: `%${q}%` })
.orWhere(`collection.title ILIKE :q`, { q: `%${q}%` })
})
)
.skip(cleanedOptions.skip)
.take(cleanedOptions.take)
if (cleanedOptions.withDeleted) {
qb = qb.withDeleted()
}
const [results, count] = await qb.getManyAndCount()
const products = await this.findWithRelations(
relations,
results.map((r) => r.id),
cleanedOptions.withDeleted
)
return [products, count]
}
private _cleanOptions(
options: CustomOptions
): WithRequiredProperty<CustomOptions, "where"> {
const where = options.where ?? {}
if ("description" in where) {
delete where.description
}
if ("title" in where) {
delete where.title
}
return {
...options,
where,
}
}
}
+22 -66
View File
@@ -1,8 +1,7 @@
import { MedusaError } from "medusa-core-utils"
import { BaseService } from "medusa-interfaces"
import { Brackets } from "typeorm"
import { formatException } from "../utils/exception-formatter"
import { defaultAdminProductsVariantsRelations } from "../api/routes/admin/products"
import { formatException } from "../utils/exception-formatter"
/**
* Provides layer to manipulate products.
@@ -127,13 +126,13 @@ class ProductService extends BaseService {
const { q, query, relations } = this.prepareListQuery_(selector, config)
if (q) {
const qb = this.getFreeTextQueryBuilder_(productRepo, query, q)
const raw = await qb.getMany()
return productRepo.findWithRelations(
relations,
raw.map((i) => i.id),
query.withDeleted ?? false
const [products] = await productRepo.getFreeTextSearchResultsAndCount(
q,
query,
relations
)
return products
}
const products = productRepo.findWithRelations(relations, query)
@@ -182,23 +181,21 @@ class ProductService extends BaseService {
const { q, query, relations } = this.prepareListQuery_(selector, config)
let products
let count
if (q) {
const qb = this.getFreeTextQueryBuilder_(productRepo, query, q)
const [raw, count] = await qb.getManyAndCount()
const products = await productRepo.findWithRelations(
;[products, count] = await productRepo.getFreeTextSearchResultsAndCount(
q,
query,
relations
)
} else {
;[products, count] = await productRepo.findWithRelationsAndCount(
relations,
raw.map((i) => i.id),
query.withDeleted ?? false
query
)
return [products, count]
}
const [products, count] = await productRepo.findWithRelationsAndCount(
relations,
query
)
if (priceIndex > -1) {
const productsWithAdditionalPrices = await this.setAdditionalPrices(
products,
@@ -1004,44 +1001,6 @@ class ProductService extends BaseService {
}
}
/**
* Creates a QueryBuilder that can fetch products based on free text.
* @param {ProductRepository} productRepo - an instance of a ProductRepositry
* @param {FindOptions<Product>} query - the query to get products by
* @param {string} q - the text to perform free text search from
* @return {QueryBuilder<Product>} a query builder that can fetch products
*/
getFreeTextQueryBuilder_(productRepo, query, q) {
const where = query.where
delete where.description
delete where.title
let qb = productRepo
.createQueryBuilder("product")
.leftJoinAndSelect("product.variants", "variant")
.leftJoinAndSelect("product.collection", "collection")
.select(["product.id"])
.where(where)
.andWhere(
new Brackets((qb) => {
qb.where(`product.description ILIKE :q`, { q: `%${q}%` })
.orWhere(`product.title ILIKE :q`, { q: `%${q}%` })
.orWhere(`variant.title ILIKE :q`, { q: `%${q}%` })
.orWhere(`variant.sku ILIKE :q`, { q: `%${q}%` })
.orWhere(`collection.title ILIKE :q`, { q: `%${q}%` })
})
)
.skip(query.skip)
.take(query.take)
if (query.withDeleted) {
qb = qb.withDeleted()
}
return qb
}
/**
* Set additional prices on a list of products.
* @param {Product[] | Product} products list of products on which to set additional prices
@@ -1078,25 +1037,22 @@ class ProductService extends BaseService {
const productArray = Array.isArray(products) ? products : [products]
const priceSelectionStrategy = this.priceSelectionStrategy_.withTransaction(
manager
)
const priceSelectionStrategy =
this.priceSelectionStrategy_.withTransaction(manager)
const productsWithPrices = await Promise.all(
productArray.map(async (p) => {
if (p.variants?.length) {
p.variants = await Promise.all(
p.variants.map(async (v) => {
const prices = await priceSelectionStrategy.calculateVariantPrice(
v.id,
{
const prices =
await priceSelectionStrategy.calculateVariantPrice(v.id, {
region_id: regionId,
currency_code: currencyCode,
cart_id: cart_id,
customer_id: customer_id,
include_discount_prices,
}
)
})
return {
...v,
+9
View File
@@ -10,6 +10,15 @@ import "reflect-metadata"
import { FindManyOptions, FindOperator, OrderByCondition } from "typeorm"
import { transformDate } from "../utils/validators/date-transform"
/**
* Utility type used to remove some optional attributes (coming from K) from a type T
*/
export type WithRequiredProperty<T, K extends keyof T> = T &
{
// -? removes 'optional' from a property
[Property in K]-?: T[Property]
}
export type PartialPick<T, K extends keyof T> = {
[P in K]?: T[P]
}