fix(product): 以 AppData 产品树作为展示主源
This commit is contained in:
@@ -3,6 +3,17 @@ import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { CreateProductDto } from './dto/create-product.dto';
|
||||
import { UpdateProductDto } from './dto/update-product.dto';
|
||||
|
||||
type ProductOverviewItem = {
|
||||
id: string;
|
||||
name: string;
|
||||
description?: string;
|
||||
createdAt?: string;
|
||||
updatedAt?: string;
|
||||
projects?: { id: string; name: string; description?: string; createdAt?: string }[];
|
||||
versions?: { id: string; name: string; releaseDate?: string | null; createdAt?: string }[];
|
||||
_count?: { requirements?: number; projects?: number; versions?: number };
|
||||
};
|
||||
|
||||
@Injectable()
|
||||
export class ProductService {
|
||||
constructor(private prisma: PrismaService) {}
|
||||
@@ -11,7 +22,16 @@ export class ProductService {
|
||||
return this.prisma.product.create({ data: dto });
|
||||
}
|
||||
|
||||
findAll() {
|
||||
async findAll() {
|
||||
const overview = await this.getAppDataOverview();
|
||||
if (overview) {
|
||||
return overview.map((product) => {
|
||||
const normalized = this.normalizeOverviewProduct(product);
|
||||
const { projects: _projects, versions: _versions, ...rest } = normalized;
|
||||
return rest;
|
||||
});
|
||||
}
|
||||
|
||||
return this.prisma.product.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
@@ -20,7 +40,12 @@ export class ProductService {
|
||||
});
|
||||
}
|
||||
|
||||
findAllWithChildren() {
|
||||
async findAllWithChildren() {
|
||||
const overview = await this.getAppDataOverview();
|
||||
if (overview) {
|
||||
return overview.map((product) => this.normalizeOverviewProduct(product));
|
||||
}
|
||||
|
||||
return this.prisma.product.findMany({
|
||||
orderBy: { createdAt: 'desc' },
|
||||
include: {
|
||||
@@ -38,6 +63,16 @@ export class ProductService {
|
||||
}
|
||||
|
||||
async findOne(id: string) {
|
||||
const overview = await this.getAppDataOverview();
|
||||
if (overview) {
|
||||
const product = overview.find((item) => item.id === id);
|
||||
if (!product) throw new NotFoundException('产品不存在');
|
||||
return {
|
||||
...this.normalizeOverviewProduct(product),
|
||||
requirements: [],
|
||||
};
|
||||
}
|
||||
|
||||
const product = await this.prisma.product.findUnique({
|
||||
where: { id },
|
||||
include: {
|
||||
@@ -64,4 +99,28 @@ export class ProductService {
|
||||
const exists = await this.prisma.product.findUnique({ where: { id } });
|
||||
if (!exists) throw new NotFoundException('产品不存在');
|
||||
}
|
||||
|
||||
private async getAppDataOverview(): Promise<ProductOverviewItem[] | null> {
|
||||
const row = await this.prisma.appData.findUnique({
|
||||
where: { key: 'products-overview' },
|
||||
});
|
||||
if (!Array.isArray(row?.value)) return null;
|
||||
return row.value as unknown as ProductOverviewItem[];
|
||||
}
|
||||
|
||||
private normalizeOverviewProduct(product: ProductOverviewItem) {
|
||||
const projects = product.projects ?? [];
|
||||
const versions = product.versions ?? [];
|
||||
return {
|
||||
...product,
|
||||
description: product.description ?? '',
|
||||
projects,
|
||||
versions,
|
||||
_count: {
|
||||
requirements: product._count?.requirements ?? 0,
|
||||
projects: projects.length,
|
||||
versions: versions.length,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user