feat: 重构追溯码及上链出库流程
This commit is contained in:
@@ -54,7 +54,10 @@ export class PublicTraceService {
|
||||
description: string | null;
|
||||
mediaFiles: unknown;
|
||||
}>,
|
||||
code: { outTime?: Date | null; outImages?: unknown },
|
||||
outbound: {
|
||||
outTime: Date;
|
||||
images: Array<{ fileKey: string; fileName: string; mimeType: string; id: bigint; size: bigint; width: number; height: number; sortOrder: number }>;
|
||||
} | null,
|
||||
) {
|
||||
const timeline = nodes.map((n) => ({
|
||||
nodeType: n.nodeType,
|
||||
@@ -66,20 +69,15 @@ export class PublicTraceService {
|
||||
media: this.mapMedia(n.mediaFiles),
|
||||
}));
|
||||
|
||||
const outMedia = Array.isArray(code.outImages)
|
||||
? (code.outImages as Array<{ key?: string; name?: string; type?: string }>)
|
||||
.map((item) => (typeof item === 'string' ? { key: item } : item))
|
||||
.filter((item) => item?.key)
|
||||
.map((item) => ({ name: item.name || '出库实况图', type: item.type || 'image', url: this.storage.getUrl(item.key as string) }))
|
||||
: [];
|
||||
const outMedia = outbound?.images.map((image) => this.mapOutboundImage(image)) ?? [];
|
||||
|
||||
if (code.outTime || outMedia.length) {
|
||||
if (outbound) {
|
||||
// 单件出库记录:不下发中文标题/说明,标记 system=OUTBOUND_RECORD 由 C 端按语言本地化
|
||||
timeline.push({
|
||||
nodeType: 'OUTBOUND',
|
||||
system: 'OUTBOUND_RECORD',
|
||||
title: null,
|
||||
occurredAt: code.outTime ?? null,
|
||||
occurredAt: outbound.outTime,
|
||||
location: null,
|
||||
description: null,
|
||||
media: outMedia,
|
||||
@@ -89,6 +87,30 @@ export class PublicTraceService {
|
||||
return timeline;
|
||||
}
|
||||
|
||||
private mapOutboundImage(image: {
|
||||
id: bigint;
|
||||
fileKey: string;
|
||||
fileName: string;
|
||||
mimeType: string;
|
||||
size: bigint;
|
||||
width: number;
|
||||
height: number;
|
||||
sortOrder: number;
|
||||
}) {
|
||||
return {
|
||||
id: image.id,
|
||||
key: image.fileKey,
|
||||
url: this.storage.getUrl(image.fileKey),
|
||||
name: image.fileName,
|
||||
type: 'image',
|
||||
mimeType: image.mimeType,
|
||||
size: image.size,
|
||||
width: image.width,
|
||||
height: image.height,
|
||||
sortOrder: image.sortOrder,
|
||||
};
|
||||
}
|
||||
|
||||
private async resolveId(shortCode: string): Promise<bigint | null> {
|
||||
const cacheKey = `trace:map:${shortCode}`;
|
||||
const cached = await this.redis.get(cacheKey);
|
||||
@@ -120,7 +142,16 @@ export class PublicTraceService {
|
||||
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { id },
|
||||
include: { batch: true, artwork: true },
|
||||
include: {
|
||||
batch: true,
|
||||
artwork: true,
|
||||
outbound: {
|
||||
include: {
|
||||
images: { orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }], take: 20 },
|
||||
_count: { select: { images: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!code) {
|
||||
await this.writeLog(id, null, shortCode, QueryResult.INVALID, ctx);
|
||||
@@ -146,7 +177,7 @@ export class PublicTraceService {
|
||||
// 仅展示「未停用」且「审核通过」的作品,否则视为无效,避免未审核数据外泄
|
||||
if (
|
||||
!artwork ||
|
||||
artwork.status === ArtworkStatus.DISABLED ||
|
||||
artwork.status !== ArtworkStatus.ACTIVE ||
|
||||
artwork.auditStatus !== AuditStatus.APPROVED
|
||||
) {
|
||||
await this.writeLog(id, code.artworkId, shortCode, QueryResult.INVALID, ctx, code);
|
||||
@@ -179,10 +210,7 @@ export class PublicTraceService {
|
||||
// 回查最新值组装响应(首次激活时读到自身写入,重复查询读到自增后的值)
|
||||
const updated = await this.prisma.traceCode.findUniqueOrThrow({ where: { id } });
|
||||
|
||||
const proof = await this.prisma.blockchainProof.findFirst({
|
||||
where: { traceCodeId: id, status: ProofStatus.ONCHAIN },
|
||||
orderBy: { id: 'desc' },
|
||||
});
|
||||
const proof = await this.prisma.blockchainProof.findUnique({ where: { traceCodeId: id } });
|
||||
|
||||
const result = firstActivate ? QueryResult.FIRST_ACTIVATE : QueryResult.REPEAT;
|
||||
await this.writeLog(id, artwork.id, shortCode, result, ctx, { ...code, ...updated });
|
||||
@@ -209,7 +237,16 @@ export class PublicTraceService {
|
||||
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { id },
|
||||
include: { batch: true, artwork: true },
|
||||
include: {
|
||||
batch: true,
|
||||
artwork: true,
|
||||
outbound: {
|
||||
include: {
|
||||
images: { orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }], take: 20 },
|
||||
_count: { select: { images: true } },
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
if (!code) return { status: 'INVALID' as const };
|
||||
if (code.status === TraceCodeStatus.DISABLED) return { status: 'DISABLED' as const };
|
||||
@@ -225,16 +262,13 @@ export class PublicTraceService {
|
||||
});
|
||||
if (
|
||||
!artwork ||
|
||||
artwork.status === ArtworkStatus.DISABLED ||
|
||||
artwork.status !== ArtworkStatus.ACTIVE ||
|
||||
artwork.auditStatus !== AuditStatus.APPROVED
|
||||
) {
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
|
||||
const proof = await this.prisma.blockchainProof.findFirst({
|
||||
where: { traceCodeId: id, status: ProofStatus.ONCHAIN },
|
||||
orderBy: { id: 'desc' },
|
||||
});
|
||||
const proof = await this.prisma.blockchainProof.findUnique({ where: { traceCodeId: id } });
|
||||
|
||||
const now = new Date();
|
||||
const firstQuery = state === 'inactive';
|
||||
@@ -257,17 +291,25 @@ export class PublicTraceService {
|
||||
// trace/preview 共用的作品展示数据(作品信息 / 时间线 / 证书 / 区块链存证)
|
||||
private buildArtworkPayload(
|
||||
artwork: ArtworkWithRel,
|
||||
code: { outTime?: Date | null; outImages?: unknown },
|
||||
code: {
|
||||
outbound?: {
|
||||
outTime: Date;
|
||||
remark: string | null;
|
||||
images: Array<{ id: bigint; fileKey: string; fileName: string; mimeType: string; size: bigint; width: number; height: number; sortOrder: number }>;
|
||||
_count: { images: number };
|
||||
} | null;
|
||||
},
|
||||
proof: {
|
||||
txHash: string | null;
|
||||
dataHash: string;
|
||||
blockHash: string | null;
|
||||
blockHeight: bigint | null;
|
||||
chainName: string | null;
|
||||
onchainAt: Date | null;
|
||||
explorerUrl: string | null;
|
||||
tokenId: bigint | null;
|
||||
contractAddress: string | null;
|
||||
status: string;
|
||||
failureReason: string | null;
|
||||
submittedAt: Date | null;
|
||||
} | null,
|
||||
explorerUrl: string | null,
|
||||
) {
|
||||
@@ -279,7 +321,20 @@ export class PublicTraceService {
|
||||
images: this.mapMedia(artwork.images),
|
||||
fields: this.buildFields(artwork.template.fields, artwork.dynamicFields),
|
||||
},
|
||||
timeline: this.buildTimeline(artwork.traceNodes, code),
|
||||
timeline: this.buildTimeline(artwork.traceNodes, code.outbound ?? null),
|
||||
outbound: code.outbound
|
||||
? {
|
||||
outTime: code.outbound.outTime,
|
||||
remark: code.outbound.remark,
|
||||
images: {
|
||||
items: code.outbound.images.map((image) => this.mapOutboundImage(image)),
|
||||
total: code.outbound._count.images,
|
||||
page: 1,
|
||||
pageSize: 20,
|
||||
hasMore: code.outbound._count.images > code.outbound.images.length,
|
||||
},
|
||||
}
|
||||
: null,
|
||||
certificates: artwork.certificates.map((c) => ({
|
||||
name: c.name,
|
||||
issuer: c.issuer,
|
||||
@@ -288,21 +343,70 @@ export class PublicTraceService {
|
||||
url: c.watermarkFileUrl ? this.storage.getUrl(c.watermarkFileUrl) : null,
|
||||
})),
|
||||
blockchain: proof
|
||||
? {
|
||||
? {
|
||||
status: proof.status,
|
||||
txHash: proof.txHash,
|
||||
dataHash: proof.dataHash,
|
||||
blockHash: proof.blockHash,
|
||||
blockHeight: proof.blockHeight,
|
||||
chainName: proof.chainName,
|
||||
onchainAt: proof.onchainAt,
|
||||
explorerUrl,
|
||||
tokenId: proof.tokenId,
|
||||
contractAddress: proof.contractAddress,
|
||||
submittedAt: proof.submittedAt,
|
||||
failureReason: this.publicFailureReason(proof.status, proof.failureReason),
|
||||
}
|
||||
: null,
|
||||
};
|
||||
}
|
||||
|
||||
private publicFailureReason(status: string, reason: string | null): string | null {
|
||||
if (status === ProofStatus.UNKNOWN) return '链上结果暂时无法确认,请稍后重试或联系平台管理员';
|
||||
if (status === ProofStatus.FAILED) return reason ? '上链处理失败,请联系平台管理员' : '上链处理失败';
|
||||
return null;
|
||||
}
|
||||
|
||||
async outboundImages(shortCode: string, page: number, pageSize: number, previewAuthorized: boolean) {
|
||||
if (!TRACE_CODE_PATTERN.test(shortCode)) return { status: 'INVALID' as const };
|
||||
const id = await this.resolveId(shortCode);
|
||||
if (id === null) return { status: 'INVALID' as const };
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { id },
|
||||
select: {
|
||||
status: true,
|
||||
artwork: { select: { status: true, auditStatus: true, deletedAt: true } },
|
||||
outbound: { select: { id: true } },
|
||||
},
|
||||
});
|
||||
if (
|
||||
!code ||
|
||||
(!previewAuthorized && code.status !== TraceCodeStatus.ACTIVE) ||
|
||||
code.status === TraceCodeStatus.DISABLED ||
|
||||
code.status === TraceCodeStatus.ABNORMAL ||
|
||||
code.artwork.deletedAt ||
|
||||
code.artwork.status !== ArtworkStatus.ACTIVE ||
|
||||
code.artwork.auditStatus !== AuditStatus.APPROVED
|
||||
) {
|
||||
return { status: 'INVALID' as const };
|
||||
}
|
||||
if (!code.outbound) return { items: [], total: 0, page, pageSize, hasMore: false };
|
||||
const [items, total] = await this.prisma.$transaction([
|
||||
this.prisma.outboundImage.findMany({
|
||||
where: { outboundRecordId: code.outbound.id },
|
||||
orderBy: [{ sortOrder: 'asc' }, { id: 'asc' }],
|
||||
skip: (page - 1) * pageSize,
|
||||
take: pageSize,
|
||||
}),
|
||||
this.prisma.outboundImage.count({ where: { outboundRecordId: code.outbound.id } }),
|
||||
]);
|
||||
return {
|
||||
items: items.map((image) => this.mapOutboundImage(image)),
|
||||
total,
|
||||
page,
|
||||
pageSize,
|
||||
hasMore: page * pageSize < total,
|
||||
};
|
||||
}
|
||||
|
||||
private async resolveExplorerUrl(
|
||||
proof: { txHash: string | null; chainName: string | null; explorerUrl: string | null } | null,
|
||||
): Promise<string | null> {
|
||||
|
||||
Reference in New Issue
Block a user