refactor(api): group public tag filters
This commit is contained in:
@@ -4,6 +4,7 @@ import { PrismaService } from '../prisma/prisma.service';
|
||||
import {
|
||||
PublicHomeGoodsQueryDto,
|
||||
PublicQueryGoodDto,
|
||||
PublicTagFilterDto,
|
||||
} from './dto/public-query-good.dto';
|
||||
import { PublicCategoryNodeDto } from './dto/public-category.dto';
|
||||
import { PublicCountryDto } from './dto/public-country.dto';
|
||||
@@ -145,9 +146,7 @@ export class PublicService {
|
||||
where.categoryId = { in: await this.collectCategoryDescendants(BigInt(query.categoryId)) };
|
||||
}
|
||||
|
||||
const tagFilters = await this.buildTagGroupFilters([
|
||||
...new Set(query.tagIds ?? []),
|
||||
]);
|
||||
const tagFilters = await this.buildTagGroupFilters(query.tags ?? []);
|
||||
if (tagFilters.length) where.AND = tagFilters;
|
||||
|
||||
const minPrice = this.parsePrice(query.minPrice, 'minPrice');
|
||||
@@ -319,16 +318,20 @@ export class PublicService {
|
||||
}
|
||||
|
||||
private async buildTagGroupFilters(
|
||||
selectedTagValues: string[],
|
||||
selectedGroups: PublicTagFilterDto[],
|
||||
): Promise<Prisma.GoodWhereInput[]> {
|
||||
const selections = selectedTagValues.map((value) => {
|
||||
const [tagGroupId, tagId] = value.split(':');
|
||||
if (!tagGroupId || !tagId || !/^\d+$/.test(tagGroupId) || !/^\d+$/.test(tagId)) {
|
||||
const selections = selectedGroups.flatMap((group) => {
|
||||
if (!/^\d+$/.test(group.tagGroupId) || !Array.isArray(group.tagIds)) {
|
||||
throw new BadRequestException(
|
||||
'tagIds 每个元素必须为 tagGroupId:tagId',
|
||||
'tags 每个元素必须包含合法的 tagGroupId 和 tagIds',
|
||||
);
|
||||
}
|
||||
return { tagGroupId, tagId };
|
||||
return group.tagIds.map((tagId) => {
|
||||
if (!/^\d+$/.test(tagId)) {
|
||||
throw new BadRequestException('tagIds 必须全部为数字字符串');
|
||||
}
|
||||
return { tagGroupId: group.tagGroupId, tagId };
|
||||
});
|
||||
});
|
||||
const uniqueTagIds = [...new Set(selections.map((item) => item.tagId))];
|
||||
const selected = uniqueTagIds.length
|
||||
|
||||
Reference in New Issue
Block a user