feat: 实现存储抽象、任务队列与区块链/归属地 Mock 适配
Co-authored-by: Cursor <cursoragent@cursor.com>
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
import { ApiPropertyOptional } from '@nestjs/swagger';
|
||||
import { IsEnum, IsOptional } from 'class-validator';
|
||||
import { JobStatus, JobType } from '@artveris/shared';
|
||||
import { PaginationDto } from '../../common/dto/pagination.dto';
|
||||
|
||||
export class QueryJobDto extends PaginationDto {
|
||||
@ApiPropertyOptional({ enum: JobType })
|
||||
@IsOptional()
|
||||
@IsEnum(JobType)
|
||||
type?: JobType;
|
||||
|
||||
@ApiPropertyOptional({ enum: JobStatus })
|
||||
@IsOptional()
|
||||
@IsEnum(JobStatus)
|
||||
status?: JobStatus;
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
import { Controller, Get, Param, Query } from '@nestjs/common';
|
||||
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
|
||||
import { JobsService } from './jobs.service';
|
||||
import { QueryJobDto } from './dto/query-job.dto';
|
||||
|
||||
@ApiTags('异步任务')
|
||||
@ApiBearerAuth()
|
||||
@Controller('jobs')
|
||||
export class JobsController {
|
||||
constructor(private readonly jobsService: JobsService) {}
|
||||
|
||||
@Get()
|
||||
@ApiOperation({ summary: '任务列表' })
|
||||
list(@Query() query: QueryJobDto) {
|
||||
return this.jobsService.list(query);
|
||||
}
|
||||
|
||||
@Get(':id')
|
||||
@ApiOperation({ summary: '任务进度详情' })
|
||||
get(@Param('id') id: string) {
|
||||
return this.jobsService.get(id);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { Global, Module } from '@nestjs/common';
|
||||
import { JobsService } from './jobs.service';
|
||||
import { JobsController } from './jobs.controller';
|
||||
|
||||
@Global()
|
||||
@Module({
|
||||
controllers: [JobsController],
|
||||
providers: [JobsService],
|
||||
exports: [JobsService],
|
||||
})
|
||||
export class JobsModule {}
|
||||
@@ -0,0 +1,63 @@
|
||||
import { Injectable } from '@nestjs/common';
|
||||
import { Prisma } from '@prisma/client';
|
||||
import { ErrorCode, JobStatus, JobType } from '@artveris/shared';
|
||||
import { PrismaService } from '../prisma/prisma.service';
|
||||
import { BusinessException } from '../common/exceptions/business.exception';
|
||||
import { buildPage } from '../common/utils/pagination.util';
|
||||
import { QueryJobDto } from './dto/query-job.dto';
|
||||
|
||||
@Injectable()
|
||||
export class JobsService {
|
||||
constructor(private readonly prisma: PrismaService) {}
|
||||
|
||||
create(type: JobType, total: number, createdBy?: number, payload?: Prisma.InputJsonValue) {
|
||||
return this.prisma.job.create({
|
||||
data: { type, total, createdBy, payload, status: JobStatus.PENDING },
|
||||
});
|
||||
}
|
||||
|
||||
start(id: string) {
|
||||
return this.prisma.job.update({ where: { id }, data: { status: JobStatus.PROCESSING } });
|
||||
}
|
||||
|
||||
progress(id: string, successCount: number, failCount: number) {
|
||||
return this.prisma.job.update({ where: { id }, data: { successCount, failCount } });
|
||||
}
|
||||
|
||||
finish(
|
||||
id: string,
|
||||
data: {
|
||||
status: JobStatus;
|
||||
successCount?: number;
|
||||
failCount?: number;
|
||||
resultFileUrl?: string | null;
|
||||
error?: string | null;
|
||||
},
|
||||
) {
|
||||
return this.prisma.job.update({ where: { id }, data });
|
||||
}
|
||||
|
||||
async get(id: string) {
|
||||
const job = await this.prisma.job.findUnique({ where: { id } });
|
||||
if (!job) {
|
||||
throw new BusinessException(ErrorCode.JOB_NOT_FOUND, '任务不存在', 404);
|
||||
}
|
||||
return job;
|
||||
}
|
||||
|
||||
async list(query: QueryJobDto) {
|
||||
const where: Prisma.JobWhereInput = {};
|
||||
if (query.type) where.type = query.type;
|
||||
if (query.status) where.status = query.status;
|
||||
const [list, total] = await this.prisma.$transaction([
|
||||
this.prisma.job.findMany({
|
||||
where,
|
||||
orderBy: { createdAt: 'desc' },
|
||||
skip: query.skip,
|
||||
take: query.take,
|
||||
}),
|
||||
this.prisma.job.count({ where }),
|
||||
]);
|
||||
return buildPage(list, total, query.page, query.pageSize);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user