feat: 实现出库导入、区块链存证、查询日志、看板与系统设置
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,58 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { InjectQueue } from '@nestjs/bullmq';
|
||||
import { Queue } from 'bullmq';
|
||||
import { ErrorCode, ProofStatus, QUEUE_NAMES } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { BlockchainService } from './blockchain.service';
|
||||
|
||||
@Injectable()
|
||||
export class AttestationService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly blockchain: BlockchainService,
|
||||
@InjectQueue(QUEUE_NAMES.ONCHAIN) private readonly queue: Queue,
|
||||
) {}
|
||||
|
||||
async trigger(traceCodeId: bigint) {
|
||||
const code = await this.prisma.traceCode.findUnique({
|
||||
where: { id: traceCodeId },
|
||||
include: {
|
||||
artwork: { include: { traceNodes: true, certificates: true } },
|
||||
},
|
||||
});
|
||||
if (!code) throw new BusinessException(ErrorCode.TRACE_CODE_NOT_FOUND, '追溯码不存在', 404);
|
||||
if (!code.artworkId || !code.artwork) {
|
||||
throw new BusinessException(ErrorCode.BAD_REQUEST, '追溯码未绑定作品,无法上链');
|
||||
}
|
||||
|
||||
const summary = {
|
||||
shortCode: code.shortCode,
|
||||
code: code.code,
|
||||
title: code.artwork.title,
|
||||
artist: code.artwork.artist,
|
||||
dynamicFields: code.artwork.dynamicFields,
|
||||
sku: code.sku,
|
||||
archiveNo: code.archiveNo,
|
||||
nodes: code.artwork.traceNodes.map((n) => ({ type: n.nodeType, title: n.title })),
|
||||
certificates: code.artwork.certificates.map((c) => c.name),
|
||||
};
|
||||
const dataHash = this.blockchain.hashData(summary);
|
||||
const proof = await this.prisma.blockchainProof.create({
|
||||
data: { traceCodeId, dataHash, status: ProofStatus.PENDING },
|
||||
});
|
||||
await this.queue.add('onchain', { proofId: proof.id });
|
||||
return proof;
|
||||
}
|
||||
|
||||
async retry(proofId: number) {
|
||||
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
|
||||
if (!proof) throw new BusinessException(ErrorCode.NOT_FOUND, '存证记录不存在', 404);
|
||||
await this.prisma.blockchainProof.update({
|
||||
where: { id: proofId },
|
||||
data: { status: ProofStatus.PENDING, onchainAt: null },
|
||||
});
|
||||
await this.queue.add('onchain', { proofId });
|
||||
return { success: true };
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user