feat(admin): manage and sync product details

This commit is contained in:
yeuimu
2026-08-21 10:18:57 +08:00
parent 7d09077f1d
commit d375df810d
18 changed files with 507 additions and 106 deletions
+38 -10
View File
@@ -10,20 +10,30 @@ import { UpdateGoodDto } from './dto/update-good.dto';
import { QueryGoodDto } from './dto/query-good.dto';
import { BatchCreateGoodDto } from './dto/batch-create-good.dto';
import { BatchPriorityDto } from './dto/batch-priority.dto';
import { GoodDto, PaginatedGoods } from './dto/good.dto';
import { GoodDetailDto, GoodDto, PaginatedGoods } from './dto/good.dto';
import { SyncService } from '../sync/sync.service';
const GOOD_INCLUDE = {
country: true,
category: true,
tag: true,
position: true,
originGood: true,
originGood: {
include: {
detail: true,
variants: { orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }] },
_count: { select: { variants: true } },
},
},
goodTags: { include: { tag: true } },
} satisfies Prisma.GoodInclude;
@Injectable()
export class GoodsService {
constructor(private readonly prisma: PrismaService) {}
constructor(
private readonly prisma: PrismaService,
private readonly syncService: SyncService,
) {}
async findAll(query: QueryGoodDto): Promise<PaginatedGoods> {
const { page, pageSize, countryId, categoryId, tagId, positionId, keyword } = query;
@@ -65,13 +75,13 @@ export class GoodsService {
};
}
async findOne(id: bigint): Promise<GoodDto> {
async findOne(id: bigint): Promise<GoodDetailDto> {
const good = await this.prisma.good.findUnique({
where: { id },
include: GOOD_INCLUDE,
});
if (!good) throw new NotFoundException(`Good ${id} not found`);
return GoodDto.from(good, {
return GoodDetailDto.fromGood(good, {
country: good.country,
category: good.category,
tag: good.tag,
@@ -83,7 +93,7 @@ export class GoodsService {
async create(dto: CreateGoodDto): Promise<GoodDto> {
await this.ensureReferences(dto);
return this.prisma.$transaction(async (tx) => {
const result = await this.prisma.$transaction(async (tx) => {
const created = await tx.good.create({
data: {
goodName: dto.goodName,
@@ -116,6 +126,10 @@ export class GoodsService {
goodTags: result.goodTags,
});
});
if (result.originGood?.sdsGoodId && !result.originGood.hasDetail) {
this.syncService.queueProductDetailSync(result.originGood.sdsGoodId);
}
return result;
}
async update(id: bigint, dto: UpdateGoodDto): Promise<GoodDto> {
@@ -148,7 +162,7 @@ export class GoodsService {
}
}
return this.prisma.$transaction(async (tx) => {
const result = await this.prisma.$transaction(async (tx) => {
if (dto.tagIds !== undefined) {
await tx.goodTag.deleteMany({ where: { goodId: id } });
if (dto.tagIds.length > 0) {
@@ -174,6 +188,10 @@ export class GoodsService {
goodTags: updated.goodTags,
});
});
if (result.originGood?.sdsGoodId && !result.originGood.hasDetail) {
this.syncService.queueProductDetailSync(result.originGood.sdsGoodId);
}
return result;
}
async remove(id: bigint): Promise<{ id: string }> {
@@ -187,7 +205,7 @@ export class GoodsService {
* or none do.
*/
async batchUpdatePriority(dto: BatchPriorityDto): Promise<{ count: number }> {
return this.prisma.$transaction(async (tx) => {
const result = await this.prisma.$transaction(async (tx) => {
for (const item of dto.items) {
await tx.good.update({
where: { id: BigInt(item.id) },
@@ -196,6 +214,7 @@ export class GoodsService {
}
return { count: dto.items.length };
});
return result;
}
/**
@@ -209,7 +228,7 @@ export class GoodsService {
await this.ensureTag(tagId);
}
}
return this.prisma.$transaction(async (tx) => {
const result = await this.prisma.$transaction(async (tx) => {
const created: GoodDto[] = [];
for (const item of dto.items) {
const og = await tx.originGood.findUnique({
@@ -254,6 +273,15 @@ export class GoodsService {
}
return created;
});
for (const goodId of new Set(
result
.filter((item) => !item.originGood?.hasDetail)
.map((item) => item.originGood?.sdsGoodId)
.filter((id): id is string => Boolean(id)),
)) {
this.syncService.queueProductDetailSync(goodId);
}
return result;
}
/**
@@ -313,4 +341,4 @@ export class GoodsService {
if (!p) throw new BadRequestException(`Position ${dto.positionId} not found`);
}
}
}
}