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 };
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,26 @@
|
||||
import { Controller, Param, ParseIntPipe, Post } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { RoleType } from '@artveris/shared';
|
||||
import { AttestationService } from './attestation.service';
|
||||
import { Roles } from '../common/decorators/roles.decorator';
|
||||
|
||||
@ApiTags('区块链存证')
|
||||
@ApiBearerAuth()
|
||||
@Controller()
|
||||
export class BlockchainController {
|
||||
constructor(private readonly attestation: AttestationService) {}
|
||||
|
||||
@Post('trace-codes/:id/onchain')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '为追溯码触发数据存证上链' })
|
||||
onchain(@Param('id') id: string) {
|
||||
return this.attestation.trigger(BigInt(id));
|
||||
}
|
||||
|
||||
@Post('blockchain-proofs/:id/retry')
|
||||
@Roles(RoleType.OPERATOR)
|
||||
@ApiOperation({ summary: '重试上链' })
|
||||
retry(@Param('id', ParseIntPipe) id: number) {
|
||||
return this.attestation.retry(id);
|
||||
}
|
||||
}
|
||||
@@ -1,9 +1,16 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { BullModule } from '@nestjs/bullmq';
|
||||
import { QUEUE_NAMES } from '@artveris/shared';
|
||||
import { BlockchainService } from './blockchain.service';
|
||||
import { AttestationService } from './attestation.service';
|
||||
import { BlockchainController } from './blockchain.controller';
|
||||
import { OnchainProcessor } from './onchain.processor';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
providers: [BlockchainService],
|
||||
exports: [BlockchainService],
|
||||
imports: [BullModule.registerQueue({ name: QUEUE_NAMES.ONCHAIN })],
|
||||
controllers: [BlockchainController],
|
||||
providers: [BlockchainService, AttestationService, OnchainProcessor],
|
||||
exports: [BlockchainService, AttestationService],
|
||||
})
|
||||
export class BlockchainModule {}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { Processor, WorkerHost } from '@nestjs/bullmq';
|
||||
import { Logger } from '@nestjs/common';
|
||||
import { Job } from 'bullmq';
|
||||
import { ProofStatus, QUEUE_NAMES } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { BlockchainService } from './blockchain.service';
|
||||
|
||||
interface OnchainJobData {
|
||||
proofId: number;
|
||||
}
|
||||
|
||||
@Processor(QUEUE_NAMES.ONCHAIN)
|
||||
export class OnchainProcessor extends WorkerHost {
|
||||
private readonly logger = new Logger(OnchainProcessor.name);
|
||||
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly blockchain: BlockchainService,
|
||||
) {
|
||||
super();
|
||||
}
|
||||
|
||||
async process(job: Job<OnchainJobData>): Promise<void> {
|
||||
const { proofId } = job.data;
|
||||
const proof = await this.prisma.blockchainProof.findUnique({ where: { id: proofId } });
|
||||
if (!proof) return;
|
||||
try {
|
||||
const result = await this.blockchain.attest({ dataHash: proof.dataHash });
|
||||
await this.prisma.blockchainProof.update({
|
||||
where: { id: proofId },
|
||||
data: {
|
||||
status: ProofStatus.ONCHAIN,
|
||||
txHash: result.txHash,
|
||||
blockHash: result.blockHash,
|
||||
blockHeight: BigInt(result.blockHeight),
|
||||
chainName: result.chainName,
|
||||
explorerUrl: result.explorerUrl,
|
||||
onchainAt: result.onchainAt,
|
||||
rawResponse: result.raw as object,
|
||||
},
|
||||
});
|
||||
this.logger.log(`存证 ${proofId} 上链成功`);
|
||||
} catch (e) {
|
||||
await this.prisma.blockchainProof.update({
|
||||
where: { id: proofId },
|
||||
data: { status: ProofStatus.FAILED },
|
||||
});
|
||||
this.logger.error(`存证 ${proofId} 上链失败: ${(e as Error).message}`);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user