feat(medusa): Filter product list by discount condition id (#2464)
This commit is contained in:
1 parent
9deec0fc3c
commit
8be67c734c
7 files changed
+144
-25
No files matched your search
@@ -14,6 +14,7 @@ import { FilterableProductProps } from "../../../../types/product"
|
||||
* x-authenticated: true
|
||||
* parameters:
|
||||
* - (query) q {string} Query used for searching product title and description, variant title and sku, and collection title.
|
||||
* - (query) discount_condition_id {string} The discount condition id on which to filter the product.
|
||||
* - in: query
|
||||
* name: id
|
||||
* style: form
|
||||
|
||||
@@ -26,6 +26,7 @@ export type FindWithoutRelationsOptions = DefaultWithoutRelations & {
|
||||
where: DefaultWithoutRelations["where"] & {
|
||||
price_list_id?: FindOperator<PriceList>
|
||||
sales_channel_id?: FindOperator<SalesChannel>
|
||||
discount_condition_id?: string
|
||||
}
|
||||
}
|
||||
|
||||
@@ -53,6 +54,10 @@ export class ProductRepository extends Repository<Product> {
|
||||
const sales_channels = optionsWithoutRelations?.where?.sales_channel_id
|
||||
delete optionsWithoutRelations?.where?.sales_channel_id
|
||||
|
||||
const discount_condition_id =
|
||||
optionsWithoutRelations?.where?.discount_condition_id
|
||||
delete optionsWithoutRelations?.where?.discount_condition_id
|
||||
|
||||
const qb = this.createQueryBuilder("product")
|
||||
.select(["product.id"])
|
||||
.skip(optionsWithoutRelations.skip)
|
||||
@@ -100,6 +105,15 @@ export class ProductRepository extends Repository<Product> {
|
||||
)
|
||||
}
|
||||
|
||||
if (discount_condition_id) {
|
||||
qb.innerJoin(
|
||||
"discount_condition_product",
|
||||
"dc_product",
|
||||
`dc_product.product_id = product.id AND dc_product.condition_id = :dcId`,
|
||||
{ dcId: discount_condition_id }
|
||||
)
|
||||
}
|
||||
|
||||
if (optionsWithoutRelations.withDeleted) {
|
||||
qb.withDeleted()
|
||||
}
|
||||
@@ -358,6 +372,16 @@ export class ProductRepository extends Repository<Product> {
|
||||
.skip(cleanedOptions.skip)
|
||||
.take(cleanedOptions.take)
|
||||
|
||||
const discountConditionId = options.where.discount_condition_id
|
||||
if (discountConditionId) {
|
||||
qb.innerJoin(
|
||||
"discount_condition_product",
|
||||
"dc_product",
|
||||
`dc_product.product_id = product.id AND dc_product.condition_id = :dcId`,
|
||||
{ dcId: discountConditionId }
|
||||
)
|
||||
}
|
||||
|
||||
if (cleanedOptions.withDeleted) {
|
||||
qb = qb.withDeleted()
|
||||
}
|
||||
@@ -388,6 +412,10 @@ export class ProductRepository extends Repository<Product> {
|
||||
delete where?.price_list_id
|
||||
}
|
||||
|
||||
if ("discount_condition_id" in where) {
|
||||
delete where?.discount_condition_id
|
||||
}
|
||||
|
||||
return {
|
||||
...options,
|
||||
where,
|
||||
|
||||
@@ -1062,7 +1062,7 @@ class CartService extends TransactionBaseService {
|
||||
const productsToKeep = await this.productService_
|
||||
.withTransaction(this.manager_)
|
||||
.filterProductsBySalesChannel(productIds, newSalesChannelId, {
|
||||
select: ["id", "sales_channels"],
|
||||
select: ["id"],
|
||||
take: productIds.length,
|
||||
})
|
||||
const productIdsToKeep = new Set<string>(
|
||||
|
||||
@@ -28,6 +28,7 @@ import {
|
||||
FilterableProductProps,
|
||||
FindProductConfig,
|
||||
ProductOptionInput,
|
||||
ProductSelector,
|
||||
UpdateProductInput,
|
||||
} from "../types/product"
|
||||
import { buildQuery, isDefined, setMetadata } from "../utils"
|
||||
@@ -108,7 +109,7 @@ class ProductService extends TransactionBaseService {
|
||||
* @return the result of the find operation
|
||||
*/
|
||||
async list(
|
||||
selector: FilterableProductProps | Selector<Product> = {},
|
||||
selector: ProductSelector,
|
||||
config: FindProductConfig = {
|
||||
relations: [],
|
||||
skip: 0,
|
||||
@@ -116,20 +117,8 @@ class ProductService extends TransactionBaseService {
|
||||
include_discount_prices: false,
|
||||
}
|
||||
): Promise<Product[]> {
|
||||
const manager = this.manager_
|
||||
const productRepo = manager.getCustomRepository(this.productRepository_)
|
||||
|
||||
const { q, query, relations } = this.prepareListQuery_(selector, config)
|
||||
if (q) {
|
||||
const [products] = await productRepo.getFreeTextSearchResultsAndCount(
|
||||
q,
|
||||
query,
|
||||
relations
|
||||
)
|
||||
return products
|
||||
}
|
||||
|
||||
return await productRepo.findWithRelations(relations, query)
|
||||
const [products] = await this.listAndCount(selector, config)
|
||||
return products
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -144,7 +133,7 @@ class ProductService extends TransactionBaseService {
|
||||
* as the second element.
|
||||
*/
|
||||
async listAndCount(
|
||||
selector: FilterableProductProps | Selector<Product>,
|
||||
selector: ProductSelector,
|
||||
config: FindProductConfig = {
|
||||
relations: [],
|
||||
skip: 0,
|
||||
|
||||
@@ -8,12 +8,19 @@ import {
|
||||
ValidateNested,
|
||||
} from "class-validator"
|
||||
import SalesChannelFeatureFlag from "../loaders/feature-flags/sales-channels"
|
||||
import { Product, ProductOptionValue, ProductStatus } from "../models"
|
||||
import {
|
||||
PriceList,
|
||||
Product,
|
||||
ProductOptionValue,
|
||||
ProductStatus,
|
||||
SalesChannel,
|
||||
} from "../models"
|
||||
import { FeatureFlagDecorators } from "../utils/feature-flag-decorators"
|
||||
import { optionalBooleanMapper } from "../utils/validators/is-boolean"
|
||||
import { IsType } from "../utils/validators/is-type"
|
||||
import { DateComparisonOperator, FindConfig } from "./common"
|
||||
import { DateComparisonOperator, FindConfig, Selector } from "./common"
|
||||
import { PriceListLoadConfig } from "./price-list"
|
||||
import { FindOperator } from "typeorm"
|
||||
|
||||
/**
|
||||
* API Level DTOs + Validation rules
|
||||
@@ -67,6 +74,10 @@ export class FilterableProductProps {
|
||||
@FeatureFlagDecorators(SalesChannelFeatureFlag.key, [IsOptional(), IsArray()])
|
||||
sales_channel_id?: string[]
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
discount_condition_id?: string
|
||||
|
||||
@IsOptional()
|
||||
@ValidateNested()
|
||||
@Type(() => DateComparisonOperator)
|
||||
@@ -83,6 +94,15 @@ export class FilterableProductProps {
|
||||
deleted_at?: DateComparisonOperator
|
||||
}
|
||||
|
||||
export type ProductSelector =
|
||||
| FilterableProductProps
|
||||
| (Selector<Product> & {
|
||||
q?: string
|
||||
discount_condition_id?: string
|
||||
price_list_id?: string[] | FindOperator<PriceList>
|
||||
sales_channel_id?: string[] | FindOperator<SalesChannel>
|
||||
})
|
||||
|
||||
/**
|
||||
* Service Level DTOs
|
||||
*/
|
||||
|
||||
Reference in new issue
Block a user