feat(v2.4): 切换计划与开发任务主写

This commit is contained in:
2026-07-08 12:34:33 +08:00
parent 5f5a211c99
commit 1bf630a7bc
21 changed files with 1612 additions and 28 deletions

View File

@@ -0,0 +1,59 @@
import { IsArray, IsIn, IsOptional, IsString } from 'class-validator';
export class CreateVersionPlanDto {
@IsIn(['research', 'product', 'ui'])
type!: 'research' | 'product' | 'ui';
@IsString()
title!: string;
@IsString()
@IsOptional()
status?: string;
@IsString()
@IsOptional()
owner?: string;
@IsString()
@IsOptional()
ownerId?: string;
@IsString()
@IsOptional()
startTime?: string;
@IsString()
@IsOptional()
endTime?: string;
@IsString()
@IsOptional()
expectedStartAt?: string;
@IsString()
@IsOptional()
expectedEndAt?: string;
@IsString()
@IsOptional()
actualStartAt?: string;
@IsString()
@IsOptional()
completedAt?: string;
@IsString()
@IsOptional()
resultUrl?: string;
@IsArray()
@IsOptional()
linkedRequirementIds?: string[];
@IsOptional()
requirementCoverage?: unknown[];
@IsOptional()
logs?: unknown[];
}

View File

@@ -0,0 +1,4 @@
import { PartialType } from '@nestjs/mapped-types';
import { CreateVersionPlanDto } from './create-version-plan.dto';
export class UpdateVersionPlanDto extends PartialType(CreateVersionPlanDto) {}

View File

@@ -0,0 +1,34 @@
import { Body, Controller, Delete, Get, Param, Patch, Post } from '@nestjs/common';
import { CreateVersionPlanDto } from './dto/create-version-plan.dto';
import { UpdateVersionPlanDto } from './dto/update-version-plan.dto';
import { VersionPlanService } from './version-plan.service';
@Controller('versions/:versionId/plans')
export class VersionPlanController {
constructor(private readonly versionPlanService: VersionPlanService) {}
@Post()
create(@Param('versionId') versionId: string, @Body() dto: CreateVersionPlanDto) {
return this.versionPlanService.create(versionId, dto);
}
@Get()
findAll(@Param('versionId') versionId: string) {
return this.versionPlanService.findAll(versionId);
}
@Patch(':id')
update(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateVersionPlanDto) {
return this.versionPlanService.update(versionId, id, dto);
}
@Patch(':id/complete')
complete(@Param('versionId') versionId: string, @Param('id') id: string, @Body() dto: UpdateVersionPlanDto) {
return this.versionPlanService.complete(versionId, id, dto);
}
@Delete(':id')
remove(@Param('versionId') versionId: string, @Param('id') id: string) {
return this.versionPlanService.remove(versionId, id);
}
}

View File

@@ -0,0 +1,12 @@
import { Module } from '@nestjs/common';
import { WorkActivityModule } from '../work-activity/work-activity.module';
import { VersionPlanController } from './version-plan.controller';
import { VersionPlanService } from './version-plan.service';
@Module({
imports: [WorkActivityModule],
controllers: [VersionPlanController],
providers: [VersionPlanService],
exports: [VersionPlanService],
})
export class VersionPlanModule {}

View File

@@ -0,0 +1,109 @@
import { NotFoundException } from '@nestjs/common';
import { VersionPlanService } from './version-plan.service';
describe('VersionPlanService domain writes', () => {
const makeService = () => {
const workActivity = {
record: jest.fn().mockResolvedValue({ id: 'activity-1' }),
};
const prisma = {
version: {
findUnique: jest.fn(),
},
versionPlan: {
create: jest.fn(),
delete: jest.fn(),
findFirst: jest.fn(),
findMany: jest.fn(),
update: jest.fn(),
},
};
return {
prisma,
workActivity,
service: new VersionPlanService(prisma as any, workActivity as any),
};
};
it('creates version plans 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.versionPlan.create.mockResolvedValue({ id: 'plan-1', versionId: 'version-1', title: '产品方案' });
await service.create('version-1', {
type: 'product',
title: '产品方案',
owner: 'member-1',
startTime: '2026-07-08T09:00:00.000Z',
endTime: '2026-07-08T18:00:00.000Z',
linkedRequirementIds: ['req-1'],
} as any);
expect(prisma.versionPlan.create).toHaveBeenCalledWith({
data: expect.objectContaining({
versionId: 'version-1',
productId: 'product-1',
projectId: 'project-1',
type: 'product',
title: '产品方案',
ownerId: 'member-1',
expectedStartAt: new Date('2026-07-08T09:00:00.000Z'),
expectedEndAt: new Date('2026-07-08T18:00:00.000Z'),
requirementCoverage: [{ requirementId: 'req-1', status: 'not_started' }],
}),
});
expect(workActivity.record).toHaveBeenCalledWith(expect.objectContaining({
versionId: 'version-1',
sourceType: 'version_plan',
sourceId: 'plan-1',
action: 'version_plan_created',
}));
});
it('updates plan status inside version scope and records activity evidence', async () => {
const { prisma, workActivity, service } = makeService();
prisma.versionPlan.findFirst.mockResolvedValue({
id: 'plan-1',
versionId: 'version-1',
productId: 'product-1',
projectId: 'project-1',
status: 'pending',
title: '产品方案',
ownerId: 'member-1',
});
prisma.versionPlan.update.mockResolvedValue({
id: 'plan-1',
versionId: 'version-1',
productId: 'product-1',
projectId: 'project-1',
status: 'in_progress',
title: '产品方案',
ownerId: 'member-1',
});
await service.update('version-1', 'plan-1', { status: 'in_progress' });
expect(prisma.versionPlan.update).toHaveBeenCalledWith({
where: { id: 'plan-1' },
data: expect.objectContaining({
status: 'in_progress',
actualStartAt: expect.any(Date),
}),
});
expect(workActivity.record).toHaveBeenCalledWith(expect.objectContaining({
versionId: 'version-1',
sourceType: 'version_plan',
sourceId: 'plan-1',
action: 'version_plan_started',
}));
});
it('rejects plan updates outside the version partition', async () => {
const { prisma, service } = makeService();
prisma.versionPlan.findFirst.mockResolvedValue(null);
await expect(service.update('version-1', 'missing-plan', { title: 'Ghost' })).rejects.toBeInstanceOf(NotFoundException);
expect(prisma.versionPlan.update).not.toHaveBeenCalled();
});
});

View File

@@ -0,0 +1,152 @@
import { Injectable, NotFoundException } from '@nestjs/common';
import type { Prisma } from '@prisma/client';
import { PrismaService } from '../../prisma/prisma.service';
import { WorkActivityService } from '../work-activity/work-activity.service';
import { CreateVersionPlanDto } from './dto/create-version-plan.dto';
import { UpdateVersionPlanDto } from './dto/update-version-plan.dto';
@Injectable()
export class VersionPlanService {
constructor(
private readonly prisma: PrismaService,
private readonly workActivity: WorkActivityService,
) {}
async create(versionId: string, dto: CreateVersionPlanDto) {
const version = await this.ensureVersion(versionId);
const item = await this.prisma.versionPlan.create({
data: {
...this.toPlanData(dto),
versionId,
productId: version.productId,
projectId: version.projectId,
type: dto.type,
title: dto.title,
status: dto.status ?? 'pending',
},
});
const activity = await this.recordPlanActivity(item, 'version_plan_created', 'creation', `新建计划:${item.title}`);
return { item, activities: [activity] };
}
findAll(versionId: string) {
return this.prisma.versionPlan.findMany({
where: { versionId },
orderBy: [{ type: 'asc' }, { createdAt: 'desc' }],
});
}
async update(versionId: string, id: string, dto: UpdateVersionPlanDto) {
const current = await this.ensurePlanInVersion(versionId, id);
const data = this.toPlanData(dto);
if (dto.status === 'in_progress' && !current.actualStartAt) {
data.actualStartAt = new Date();
}
if (dto.status === 'completed' && !current.completedAt) {
data.completedAt = new Date();
}
const item = await this.prisma.versionPlan.update({
where: { id },
data,
});
const activity = current.status !== item.status ? await this.recordStatusActivity(item, current.status, item.status) : undefined;
return { item, activities: activity ? [activity] : [] };
}
async complete(versionId: string, id: string, dto: UpdateVersionPlanDto) {
return this.update(versionId, id, {
...dto,
status: 'completed',
completedAt: dto.completedAt ?? new Date().toISOString(),
});
}
async remove(versionId: string, id: string) {
await this.ensurePlanInVersion(versionId, id);
return this.prisma.versionPlan.delete({ where: { id } });
}
private toPlanData(dto: Partial<CreateVersionPlanDto>) {
return {
...(dto.type !== undefined && { type: dto.type }),
...(dto.title !== undefined && { title: dto.title }),
...(dto.status !== undefined && { status: dto.status }),
...(dto.owner !== undefined || dto.ownerId !== undefined ? { ownerId: emptyToNull(dto.ownerId ?? dto.owner) } : {}),
...(dto.startTime !== undefined || dto.expectedStartAt !== undefined
? { expectedStartAt: parseOptionalDate(dto.expectedStartAt ?? dto.startTime) }
: {}),
...(dto.endTime !== undefined || dto.expectedEndAt !== undefined
? { expectedEndAt: parseOptionalDate(dto.expectedEndAt ?? dto.endTime) }
: {}),
...(dto.actualStartAt !== undefined && { actualStartAt: parseOptionalDate(dto.actualStartAt) }),
...(dto.completedAt !== undefined && { completedAt: parseOptionalDate(dto.completedAt) }),
...(dto.resultUrl !== undefined && { resultUrl: emptyToNull(dto.resultUrl) }),
...(dto.requirementCoverage !== undefined || dto.linkedRequirementIds !== undefined
? { requirementCoverage: toJsonInput(dto.requirementCoverage ?? buildRequirementCoverage(dto.linkedRequirementIds)) }
: {}),
...(dto.logs !== undefined && { logs: toJsonInput(dto.logs) }),
};
}
private async ensureVersion(versionId: string) {
const version = await this.prisma.version.findUnique({ where: { id: versionId } });
if (!version) throw new NotFoundException('版本不存在');
return version;
}
private async ensurePlanInVersion(versionId: string, id: string) {
const plan = await this.prisma.versionPlan.findFirst({ where: { id, versionId } });
if (!plan) throw new NotFoundException('计划不存在');
return plan;
}
private recordStatusActivity(plan: any, fromStatus: string, toStatus: string) {
if (toStatus === 'in_progress') {
return this.recordPlanActivity(plan, 'version_plan_started', 'progress', `开始计划:${plan.title}`, { fromStatus, toStatus });
}
if (toStatus === 'completed') {
return this.recordPlanActivity(plan, 'version_plan_completed', 'delivery', `完成计划:${plan.title}`, { fromStatus, toStatus });
}
return undefined;
}
private recordPlanActivity(plan: any, action: string, category: string, summary: string, metadata: Record<string, unknown> = {}) {
return this.workActivity.record({
versionId: plan.versionId,
productId: plan.productId,
projectId: plan.projectId,
actorId: plan.ownerId,
sourceType: 'version_plan',
sourceId: plan.id,
action,
category,
title: plan.title,
summary,
metadata,
});
}
}
function buildRequirementCoverage(requirementIds: string[] | undefined) {
return (requirementIds ?? []).map((requirementId) => ({
requirementId,
status: 'not_started',
}));
}
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 toJsonInput(value: unknown): Prisma.InputJsonValue {
return value as Prisma.InputJsonValue;
}