feat(v2.4): 切换测试与缺陷主写
This commit is contained in:
49
apps/server/src/modules/bug/bug.controller.ts
Normal file
49
apps/server/src/modules/bug/bug.controller.ts
Normal file
@@ -0,0 +1,49 @@
|
||||
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
|
||||
import { CreateBugDto } from './dto/create-bug.dto';
|
||||
import { UpdateBugDto } from './dto/update-bug.dto';
|
||||
import { BugService } from './bug.service';
|
||||
|
||||
@Controller('versions/:versionId/bugs')
|
||||
export class BugController {
|
||||
constructor(private readonly bugService: BugService) {}
|
||||
|
||||
@Post()
|
||||
create(@Param('versionId') versionId: string, @Body() dto: CreateBugDto) {
|
||||
return this.bugService.create(versionId, dto);
|
||||
}
|
||||
|
||||
@Get()
|
||||
findAll(@Param('versionId') versionId: string) {
|
||||
return this.bugService.findAll(versionId);
|
||||
}
|
||||
|
||||
@Patch(':id')
|
||||
update(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateBugDto) {
|
||||
return this.bugService.update(versionId, id, dto);
|
||||
}
|
||||
|
||||
@Patch(':id/status')
|
||||
updateStatus(
|
||||
@Param('versionId') versionId: string,
|
||||
@Param('id') id: string,
|
||||
@Body('status') status: string,
|
||||
@Body() body: { operator?: string; resolution?: string },
|
||||
) {
|
||||
return this.bugService.updateStatus(versionId, id, status, body);
|
||||
}
|
||||
|
||||
@Patch(':id/transfer')
|
||||
transfer(
|
||||
@Param('versionId') versionId: string,
|
||||
@Param('id') id: string,
|
||||
@Body('assigneeId') assigneeId: string,
|
||||
@Body('operator') operator?: string,
|
||||
) {
|
||||
return this.bugService.transfer(versionId, id, assigneeId, operator);
|
||||
}
|
||||
|
||||
@Delete(':id')
|
||||
remove(@Param('versionId') versionId: string, @Param('id') id: string) {
|
||||
return this.bugService.remove(versionId, id);
|
||||
}
|
||||
}
|
||||
12
apps/server/src/modules/bug/bug.module.ts
Normal file
12
apps/server/src/modules/bug/bug.module.ts
Normal file
@@ -0,0 +1,12 @@
|
||||
import { Module } from '@nestjs/common';
|
||||
import { WorkActivityModule } from '../work-activity/work-activity.module';
|
||||
import { BugController } from './bug.controller';
|
||||
import { BugService } from './bug.service';
|
||||
|
||||
@Module({
|
||||
imports: [WorkActivityModule],
|
||||
controllers: [BugController],
|
||||
providers: [BugService],
|
||||
exports: [BugService],
|
||||
})
|
||||
export class BugModule {}
|
||||
153
apps/server/src/modules/bug/bug.service.spec.ts
Normal file
153
apps/server/src/modules/bug/bug.service.spec.ts
Normal file
@@ -0,0 +1,153 @@
|
||||
import { NotFoundException } from '@nestjs/common';
|
||||
import { BugService } from './bug.service';
|
||||
|
||||
describe('BugService domain writes', () => {
|
||||
const makeService = () => {
|
||||
const workActivity = {
|
||||
record: jest.fn().mockResolvedValue({ id: 'activity-1' }),
|
||||
};
|
||||
const prisma = {
|
||||
version: {
|
||||
findUnique: jest.fn(),
|
||||
},
|
||||
bug: {
|
||||
create: jest.fn(),
|
||||
delete: jest.fn(),
|
||||
findFirst: jest.fn(),
|
||||
findMany: jest.fn(),
|
||||
update: jest.fn(),
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
prisma,
|
||||
workActivity,
|
||||
service: new BugService(prisma as any, workActivity as any),
|
||||
};
|
||||
};
|
||||
|
||||
it('creates bugs directly under a version partition', async () => {
|
||||
const { prisma, workActivity, service } = makeService();
|
||||
prisma.version.findUnique.mockResolvedValue({ id: 'version-1', productId: 'product-1', projectId: 'project-1' });
|
||||
prisma.bug.create.mockResolvedValue({ id: 'bug-1', versionId: 'version-1', title: '登录报错' });
|
||||
|
||||
await service.create('version-1', {
|
||||
bugNo: 'BUG-001',
|
||||
testCaseId: 'tc-1',
|
||||
title: '登录报错',
|
||||
description: '登录按钮无响应',
|
||||
severity: 'critical',
|
||||
priority: 'P0',
|
||||
assigneeId: 'dev-1',
|
||||
reportedBy: 'tester-1',
|
||||
plannedFixAt: '2026-07-08T18:00:00.000Z',
|
||||
} as any);
|
||||
|
||||
expect(prisma.bug.create).toHaveBeenCalledWith({
|
||||
data: expect.objectContaining({
|
||||
versionId: 'version-1',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
testCaseId: 'tc-1',
|
||||
testCaseVersionId: 'version-1',
|
||||
code: 'BUG-001',
|
||||
title: '登录报错',
|
||||
severity: 'critical',
|
||||
priority: 0,
|
||||
assigneeId: 'dev-1',
|
||||
reporterId: 'tester-1',
|
||||
plannedFixAt: new Date('2026-07-08T18:00:00.000Z'),
|
||||
status: 'open',
|
||||
}),
|
||||
});
|
||||
expect(workActivity.record).toHaveBeenCalledWith(expect.objectContaining({
|
||||
versionId: 'version-1',
|
||||
sourceType: 'bug',
|
||||
sourceId: 'bug-1',
|
||||
action: 'bug_created',
|
||||
}));
|
||||
});
|
||||
|
||||
it('changes status and records closing evidence inside the version partition', async () => {
|
||||
const { prisma, workActivity, service } = makeService();
|
||||
prisma.bug.findFirst.mockResolvedValue({
|
||||
id: 'bug-1',
|
||||
versionId: 'version-1',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
status: 'verifying',
|
||||
title: '登录报错',
|
||||
assigneeId: 'dev-1',
|
||||
reporterId: 'tester-1',
|
||||
});
|
||||
prisma.bug.update.mockResolvedValue({
|
||||
id: 'bug-1',
|
||||
versionId: 'version-1',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
status: 'closed',
|
||||
title: '登录报错',
|
||||
assigneeId: 'dev-1',
|
||||
reporterId: 'tester-1',
|
||||
});
|
||||
|
||||
await service.updateStatus('version-1', 'bug-1', 'closed', { operator: 'tester-1', resolution: '复测通过' });
|
||||
|
||||
expect(prisma.bug.update).toHaveBeenCalledWith({
|
||||
where: { id_versionId: { id: 'bug-1', versionId: 'version-1' } },
|
||||
data: expect.objectContaining({
|
||||
status: 'closed',
|
||||
closedAt: expect.any(Date),
|
||||
resolution: '复测通过',
|
||||
}),
|
||||
});
|
||||
expect(workActivity.record).toHaveBeenCalledWith(expect.objectContaining({
|
||||
versionId: 'version-1',
|
||||
sourceType: 'bug',
|
||||
sourceId: 'bug-1',
|
||||
action: 'bug_closed',
|
||||
}));
|
||||
});
|
||||
|
||||
it('transfers bugs by id plus version id and records activity evidence', async () => {
|
||||
const { prisma, workActivity, service } = makeService();
|
||||
prisma.bug.findFirst.mockResolvedValue({
|
||||
id: 'bug-1',
|
||||
versionId: 'version-1',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
status: 'open',
|
||||
title: '登录报错',
|
||||
assigneeId: 'dev-1',
|
||||
reporterId: 'tester-1',
|
||||
});
|
||||
prisma.bug.update.mockResolvedValue({
|
||||
id: 'bug-1',
|
||||
versionId: 'version-1',
|
||||
productId: 'product-1',
|
||||
projectId: 'project-1',
|
||||
title: '登录报错',
|
||||
assigneeId: 'dev-2',
|
||||
reporterId: 'tester-1',
|
||||
});
|
||||
|
||||
await service.transfer('version-1', 'bug-1', 'dev-2', 'tester-1');
|
||||
|
||||
expect(prisma.bug.update).toHaveBeenCalledWith({
|
||||
where: { id_versionId: { id: 'bug-1', versionId: 'version-1' } },
|
||||
data: { assigneeId: 'dev-2' },
|
||||
});
|
||||
expect(workActivity.record).toHaveBeenCalledWith(expect.objectContaining({
|
||||
action: 'bug_transferred',
|
||||
metadata: expect.objectContaining({ fromAssigneeId: 'dev-1', toAssigneeId: 'dev-2' }),
|
||||
}));
|
||||
});
|
||||
|
||||
it('rejects updates outside the version partition', async () => {
|
||||
const { prisma, service } = makeService();
|
||||
prisma.bug.findFirst.mockResolvedValue(null);
|
||||
|
||||
await expect(service.update('version-1', 'missing-bug', { title: 'Ghost' })).rejects.toBeInstanceOf(NotFoundException);
|
||||
expect(prisma.bug.update).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
176
apps/server/src/modules/bug/bug.service.ts
Normal file
176
apps/server/src/modules/bug/bug.service.ts
Normal file
@@ -0,0 +1,176 @@
|
||||
import { BadRequestException, Injectable, NotFoundException } from '@nestjs/common';
|
||||
import { PrismaService } from '../../prisma/prisma.service';
|
||||
import { WorkActivityService } from '../work-activity/work-activity.service';
|
||||
import { CreateBugDto } from './dto/create-bug.dto';
|
||||
import { UpdateBugDto } from './dto/update-bug.dto';
|
||||
|
||||
@Injectable()
|
||||
export class BugService {
|
||||
constructor(
|
||||
private readonly prisma: PrismaService,
|
||||
private readonly workActivity: WorkActivityService,
|
||||
) {}
|
||||
|
||||
async create(versionId: string, dto: CreateBugDto) {
|
||||
const version = await this.ensureVersion(versionId);
|
||||
const item = await this.prisma.bug.create({
|
||||
data: {
|
||||
...this.toBugData(dto),
|
||||
versionId,
|
||||
productId: version.productId,
|
||||
projectId: version.projectId,
|
||||
testCaseVersionId: dto.testCaseVersionId ?? (dto.testCaseId ? versionId : null),
|
||||
code: dto.code?.trim() || dto.bugNo?.trim() || createFallbackCode('BUG'),
|
||||
title: dto.title,
|
||||
status: dto.status ?? 'open',
|
||||
},
|
||||
});
|
||||
const activity = await this.recordBugActivity(item, 'bug_created', 'creation', `新建 Bug:${item.title}`, {
|
||||
operator: dto.reporterId ?? dto.reportedBy,
|
||||
});
|
||||
return { item, activities: [activity] };
|
||||
}
|
||||
|
||||
findAll(versionId: string) {
|
||||
return this.prisma.bug.findMany({
|
||||
where: { versionId },
|
||||
orderBy: [{ status: 'asc' }, { updatedAt: 'desc' }],
|
||||
});
|
||||
}
|
||||
|
||||
async update(versionId: string, id: string, dto: UpdateBugDto) {
|
||||
await this.ensureBugInVersion(versionId, id);
|
||||
const item = await this.prisma.bug.update({
|
||||
where: { id_versionId: { id, versionId } },
|
||||
data: this.toBugData(dto),
|
||||
});
|
||||
return { item, activities: [] };
|
||||
}
|
||||
|
||||
async updateStatus(
|
||||
versionId: string,
|
||||
id: string,
|
||||
status: string,
|
||||
options: { operator?: string; resolution?: string } = {},
|
||||
) {
|
||||
const current = await this.ensureBugInVersion(versionId, id);
|
||||
const data: Record<string, unknown> = { status };
|
||||
if (status === 'fixed' && !current.resolvedAt) data.resolvedAt = new Date();
|
||||
if (status === 'closed' && !current.closedAt) data.closedAt = new Date();
|
||||
if (options.resolution?.trim()) data.resolution = options.resolution.trim();
|
||||
const item = await this.prisma.bug.update({
|
||||
where: { id_versionId: { id, versionId } },
|
||||
data,
|
||||
});
|
||||
const activity = await this.recordStatusActivity(item, current.status, status, options.operator);
|
||||
return { item, activities: activity ? [activity] : [] };
|
||||
}
|
||||
|
||||
async transfer(versionId: string, id: string, assigneeId: string, operator?: string) {
|
||||
const current = await this.ensureBugInVersion(versionId, id);
|
||||
const item = await this.prisma.bug.update({
|
||||
where: { id_versionId: { id, versionId } },
|
||||
data: { assigneeId },
|
||||
});
|
||||
const activity = await this.recordBugActivity(
|
||||
item,
|
||||
'bug_transferred',
|
||||
'progress',
|
||||
`转交 Bug:${item.title} → ${assigneeId}`,
|
||||
{ fromAssigneeId: current.assigneeId, toAssigneeId: assigneeId, operator },
|
||||
);
|
||||
return { item, activities: [activity] };
|
||||
}
|
||||
|
||||
async remove(versionId: string, id: string) {
|
||||
await this.ensureBugInVersion(versionId, id);
|
||||
return this.prisma.bug.delete({ where: { id_versionId: { id, versionId } } });
|
||||
}
|
||||
|
||||
private toBugData(dto: Partial<CreateBugDto>) {
|
||||
return {
|
||||
...(dto.testCaseId !== undefined && { testCaseId: emptyToNull(dto.testCaseId) }),
|
||||
...(dto.testCaseVersionId !== undefined && { testCaseVersionId: emptyToNull(dto.testCaseVersionId) }),
|
||||
...(dto.code !== undefined || dto.bugNo !== undefined ? { code: dto.code?.trim() || dto.bugNo?.trim() } : {}),
|
||||
...(dto.title !== undefined && { title: dto.title }),
|
||||
...(dto.description !== undefined && { description: dto.description ?? '' }),
|
||||
...(dto.status !== undefined && { status: dto.status }),
|
||||
...(dto.severity !== undefined && { severity: dto.severity ?? 'minor' }),
|
||||
...(dto.priority !== undefined && { priority: parsePriority(dto.priority) ?? 0 }),
|
||||
...(dto.assigneeId !== undefined && { assigneeId: emptyToNull(dto.assigneeId) }),
|
||||
...(dto.reporterId !== undefined || dto.reportedBy !== undefined ? { reporterId: emptyToNull(dto.reporterId ?? dto.reportedBy) } : {}),
|
||||
...(dto.plannedFixAt !== undefined && { plannedFixAt: parseOptionalDate(dto.plannedFixAt) }),
|
||||
...(dto.resolvedAt !== undefined && { resolvedAt: parseOptionalDate(dto.resolvedAt) }),
|
||||
...(dto.closedAt !== undefined && { closedAt: parseOptionalDate(dto.closedAt) }),
|
||||
...(dto.resolution !== undefined && { resolution: emptyToNull(dto.resolution) }),
|
||||
};
|
||||
}
|
||||
|
||||
private async ensureVersion(versionId: string) {
|
||||
const version = await this.prisma.version.findUnique({ where: { id: versionId } });
|
||||
if (!version) throw new NotFoundException('版本不存在');
|
||||
if (!version.projectId) throw new BadRequestException('Bug 所属版本必须归属于项目');
|
||||
return { ...version, projectId: version.projectId };
|
||||
}
|
||||
|
||||
private async ensureBugInVersion(versionId: string, id: string) {
|
||||
const bug = await this.prisma.bug.findFirst({ where: { id, versionId } });
|
||||
if (!bug) throw new NotFoundException('Bug 不存在');
|
||||
return bug;
|
||||
}
|
||||
|
||||
private recordStatusActivity(bug: any, fromStatus: string, toStatus: string, operator?: string) {
|
||||
if (toStatus === 'fixing') {
|
||||
return this.recordBugActivity(bug, 'bug_fixing', 'progress', `开始修复 Bug:${bug.title}`, { fromStatus, toStatus, operator });
|
||||
}
|
||||
if (toStatus === 'fixed') {
|
||||
return this.recordBugActivity(bug, 'bug_fixed', 'delivery', `已修复 Bug:${bug.title}`, { fromStatus, toStatus, operator });
|
||||
}
|
||||
if (toStatus === 'closed') {
|
||||
return this.recordBugActivity(bug, 'bug_closed', 'delivery', `已关闭 Bug:${bug.title}`, { fromStatus, toStatus, operator });
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
private recordBugActivity(bug: any, action: string, category: string, summary: string, metadata: Record<string, unknown> = {}) {
|
||||
return this.workActivity.record({
|
||||
versionId: bug.versionId,
|
||||
productId: bug.productId,
|
||||
projectId: bug.projectId,
|
||||
actorId: metadata.operator as string | undefined ?? bug.assigneeId ?? bug.reporterId,
|
||||
sourceType: 'bug',
|
||||
sourceId: bug.id,
|
||||
action,
|
||||
category,
|
||||
title: bug.title,
|
||||
summary,
|
||||
metadata,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
function createFallbackCode(prefix: string) {
|
||||
return `${prefix}-${Date.now().toString(36).toUpperCase()}`;
|
||||
}
|
||||
|
||||
function emptyToNull(value: string | null | undefined): string | null {
|
||||
if (value === null) return null;
|
||||
if (value === undefined) return null;
|
||||
const trimmed = value.trim();
|
||||
return trimmed ? trimmed : null;
|
||||
}
|
||||
|
||||
function parseOptionalDate(value: string | null | undefined): Date | null {
|
||||
if (!value) return null;
|
||||
const date = new Date(value);
|
||||
return Number.isFinite(date.getTime()) ? date : null;
|
||||
}
|
||||
|
||||
function parsePriority(value: string | number | null | undefined): number | undefined {
|
||||
if (value === null || value === undefined || value === '') return undefined;
|
||||
if (typeof value === 'number') return Number.isFinite(value) ? Math.max(0, Math.min(4, Math.floor(value))) : undefined;
|
||||
const match = /^P([0-4])$/i.exec(value.trim());
|
||||
if (match) return Number(match[1]);
|
||||
const parsed = Number(value);
|
||||
return Number.isFinite(parsed) ? Math.max(0, Math.min(4, Math.floor(parsed))) : undefined;
|
||||
}
|
||||
65
apps/server/src/modules/bug/dto/create-bug.dto.ts
Normal file
65
apps/server/src/modules/bug/dto/create-bug.dto.ts
Normal file
@@ -0,0 +1,65 @@
|
||||
import { IsOptional, IsString } from 'class-validator';
|
||||
|
||||
export class CreateBugDto {
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
testCaseId?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
testCaseVersionId?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
code?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
bugNo?: string;
|
||||
|
||||
@IsString()
|
||||
title!: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
description?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
status?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
severity?: string;
|
||||
|
||||
@IsOptional()
|
||||
priority?: string | number;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
assigneeId?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
reporterId?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
reportedBy?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
plannedFixAt?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
resolvedAt?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
closedAt?: string;
|
||||
|
||||
@IsString()
|
||||
@IsOptional()
|
||||
resolution?: string;
|
||||
}
|
||||
4
apps/server/src/modules/bug/dto/update-bug.dto.ts
Normal file
4
apps/server/src/modules/bug/dto/update-bug.dto.ts
Normal file
@@ -0,0 +1,4 @@
|
||||
import { PartialType } from '@nestjs/mapped-types';
|
||||
import { CreateBugDto } from './create-bug.dto';
|
||||
|
||||
export class UpdateBugDto extends PartialType(CreateBugDto) {}
|
||||
Reference in New Issue
Block a user