feat(products): fix search query filter overwrite and add minPrice maxPrice sorting filters [TASK-26.10] [TASK-26.11]
This commit is contained in:
1 parent
7f3c04acf5
commit
3497a743b3
3 files changed
+59
-37
No files matched your search
@@ -25,4 +25,24 @@ export class GetProductsDto extends PaginationDto {
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
requiresRx?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'حداقل قیمت (تومان)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
minPrice?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'حداکثر قیمت (تومان)' })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
maxPrice?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'فیلد مرتبسازی', enum: ['priceValue', 'createdAt', 'nameFa'] })
|
||||
@IsOptional()
|
||||
@IsString()
|
||||
sortBy?: string;
|
||||
|
||||
@ApiPropertyOptional({ description: 'جهت مرتبسازی', enum: ['asc', 'desc'] })
|
||||
@IsOptional()
|
||||
@IsEnum(['asc', 'desc'])
|
||||
sortOrder?: 'asc' | 'desc';
|
||||
}
|
||||
@@ -13,66 +13,68 @@ export class ProductsService {
|
||||
search,
|
||||
symptom,
|
||||
requiresRx,
|
||||
minPrice,
|
||||
maxPrice,
|
||||
page = 1,
|
||||
limit = 10,
|
||||
sortBy = 'createdAt',
|
||||
sortOrder = 'desc',
|
||||
} = filters;
|
||||
|
||||
const whereClause: any = {};
|
||||
const andConditions: any[] = [];
|
||||
|
||||
if (requiresRx !== undefined && requiresRx !== null && requiresRx !== '') {
|
||||
whereClause.requiresRx = requiresRx === 'true' || requiresRx === '1';
|
||||
andConditions.push({ requiresRx: requiresRx === 'true' || requiresRx === '1' });
|
||||
}
|
||||
|
||||
if (category) {
|
||||
whereClause.categorySlug = category;
|
||||
andConditions.push({ categorySlug: category });
|
||||
}
|
||||
|
||||
if (petType && petType !== 'all') {
|
||||
whereClause.suitableFor = { in: [petType, 'هر دو'] };
|
||||
andConditions.push({ suitableFor: { in: [petType, 'هر دو'] } });
|
||||
}
|
||||
|
||||
// Filter by a specific symptom (from URL param ?symptom=...)
|
||||
if (symptom) {
|
||||
whereClause.symptoms = {
|
||||
some: {
|
||||
symptom: { contains: symptom, mode: 'insensitive' },
|
||||
andConditions.push({
|
||||
symptoms: {
|
||||
some: {
|
||||
symptom: { contains: symptom, mode: 'insensitive' },
|
||||
},
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
if (minPrice) {
|
||||
andConditions.push({ priceValue: { gte: Number(minPrice) } });
|
||||
}
|
||||
|
||||
if (maxPrice) {
|
||||
andConditions.push({ priceValue: { lte: Number(maxPrice) } });
|
||||
}
|
||||
|
||||
if (search) {
|
||||
// If symptom filter is already applied, extend via AND to also search names/desc
|
||||
// If not, use OR across names, description AND symptoms
|
||||
const searchConditions = [
|
||||
{ artNo: { contains: search, mode: 'insensitive' } },
|
||||
{ barcode: { contains: search, mode: 'insensitive' } },
|
||||
{ nameFa: { contains: search, mode: 'insensitive' } },
|
||||
{ nameEn: { contains: search, mode: 'insensitive' } },
|
||||
{ description: { contains: search, mode: 'insensitive' } },
|
||||
{ shortDescription: { contains: search, mode: 'insensitive' } },
|
||||
{
|
||||
symptoms: {
|
||||
some: {
|
||||
symptom: { contains: search, mode: 'insensitive' },
|
||||
andConditions.push({
|
||||
OR: [
|
||||
{ artNo: { contains: search, mode: 'insensitive' } },
|
||||
{ barcode: { contains: search, mode: 'insensitive' } },
|
||||
{ nameFa: { contains: search, mode: 'insensitive' } },
|
||||
{ nameEn: { contains: search, mode: 'insensitive' } },
|
||||
{ description: { contains: search, mode: 'insensitive' } },
|
||||
{ shortDescription: { contains: search, mode: 'insensitive' } },
|
||||
{
|
||||
symptoms: {
|
||||
some: {
|
||||
symptom: { contains: search, mode: 'insensitive' },
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
if (symptom) {
|
||||
// Already have a symptom filter; combine with AND
|
||||
whereClause.AND = [
|
||||
{ symptoms: whereClause.symptoms },
|
||||
{ OR: searchConditions.filter((c) => !('symptoms' in c)) },
|
||||
];
|
||||
delete whereClause.symptoms;
|
||||
} else {
|
||||
whereClause.OR = searchConditions;
|
||||
}
|
||||
],
|
||||
});
|
||||
}
|
||||
|
||||
const whereClause: any = andConditions.length > 0 ? { AND: andConditions } : {};
|
||||
|
||||
const skip = (page - 1) * limit;
|
||||
|
||||
const [rawProducts, total] = await Promise.all([
|
||||
|
||||
Reference in new issue
Block a user