refactor(api): group public tag filters

This commit is contained in:
yeuimu
2026-08-21 11:34:57 +08:00
parent 4963a5c463
commit 8b38d6fef7
5 changed files with 116 additions and 29 deletions
+12 -9
View File
@@ -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 每个元素必须包含合法的 tagGroupIdtagIds',
);
}
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