101 lines
3.7 KiB
TypeScript
101 lines
3.7 KiB
TypeScript
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: {
|
|
batch: true,
|
|
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,
|
|
archiveNo: code.artwork.archiveNo,
|
|
batchNo: code.batch.batchNo,
|
|
outbound: {
|
|
outTime: code.outTime,
|
|
outImage: code.outImage,
|
|
outImages: code.outImages,
|
|
},
|
|
dynamicFields: code.artwork.dynamicFields,
|
|
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,
|
|
rawResponse: { requestMeta: summary },
|
|
},
|
|
});
|
|
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,
|
|
txHash: null,
|
|
blockHash: null,
|
|
blockHeight: null,
|
|
chainName: null,
|
|
explorerUrl: null,
|
|
onchainAt: null,
|
|
rawResponse: {
|
|
...(proof.rawResponse && typeof proof.rawResponse === 'object' ? (proof.rawResponse as object) : {}),
|
|
retriedAt: new Date().toISOString(),
|
|
},
|
|
},
|
|
});
|
|
await this.queue.add('onchain', { proofId });
|
|
return { success: true };
|
|
}
|
|
|
|
async confirm(proofId: number) {
|
|
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
|
|
if (!proof) throw new BusinessException(ErrorCode.NOT_FOUND, '存证记录不存在', 404);
|
|
if (proof.status === ProofStatus.ONCHAIN) return { success: true };
|
|
if (!proof.txHash) throw new BusinessException(ErrorCode.BAD_REQUEST, '缺少交易哈希,无法重新确认');
|
|
await this.prisma.blockchainProof.update({
|
|
where: { id: proofId },
|
|
data: {
|
|
status: ProofStatus.CONFIRMING,
|
|
rawResponse: {
|
|
...(proof.rawResponse && typeof proof.rawResponse === 'object' ? (proof.rawResponse as object) : {}),
|
|
manualConfirmQueuedAt: new Date().toISOString(),
|
|
},
|
|
},
|
|
});
|
|
await this.queue.add('confirm-onchain', { proofId, action: 'confirm', confirmAttempt: 1, manual: true });
|
|
return { success: true };
|
|
}
|
|
}
|