Files
tap/lib/server/demand-review.ts
T

81 lines
6.4 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
import { z } from 'zod';
import { currentProposal, invalidateProposal, type ReviewPerson } from '../demand-review.ts';
import type { Demand } from '../demand.ts';
import { AuthError, type AuthUser } from './auth.ts';
import { getDatabase } from './database.ts';
import { pendingChange } from '../workflow.ts';
import { canTakeOverDevelopment } from './default-developer.ts';
const proposalSchema = z.object({
scope: z.string().trim().min(1).max(6000),
outOfScope: z.string().trim().min(1).max(3000),
});
const person = (user: AuthUser): ReviewPerson => ({ userId: user.userId, username: user.username, displayName: user.displayName });
function changeDeveloper(d: Demand, developer: ReviewPerson, now: string): void {
invalidateProposal(d, now, '开发责任人已变更,需重新提出方案并确认。');
d.status = '待评审';
if (d.acceptance) {
d.acceptance.withdrawnAt = now;
const round = d.delivery?.testing?.find(item => item.round === d.acceptance?.round);
if (round) round.withdrawnAt = now;
}
d.review = { developer, proposals: d.review?.proposals ?? [] };
}
export function applyReviewAction(d: Demand, ownerId: string, user: AuthUser, body: Record<string, unknown>, now: string): { label: string; detail: string } {
if (body.action === 'take-over-development') {
if (!canTakeOverDevelopment(d, user)) throw new AuthError('仅启用的管理员可接手已停用或不可用的开发账号所负责的需求。', 403);
if (pendingChange(d)) throw new AuthError('请先由业务方处理或撤回待确认变更,再接手开发职责。', 409);
if (body.confirm !== true) throw new AuthError('请确认接手开发职责,并重新评审和排期。');
const previous = d.review!.developer;
changeDeveloper(d, person(user), now);
return { label: '接手开发职责', detail: `${previous.displayName}(${previous.username}) → ${user.displayName}(${user.username});原开发账号已停用或不可用。历史方案、计划和验收记录保留,需重新评审、确认计划并提测验收。` };
}
if ((d.status !== '待评审' && (body.action !== 'assign-developer' || ['草稿','已取消'].includes(d.status))) || !d.reviewReady) throw new AuthError('请先补齐业务材料并提交评审。', 409);
const business = ownerId === user.userId;
if (body.action === 'assign-developer') {
if (!business) throw new AuthError('只有需求登记账号可以指定开发责任人。', 403);
if(pendingChange(d))throw new AuthError('请先处理或撤回待确认变更,再更换开发责任人。',409);
if (typeof body.username !== 'string' || !body.username.trim() || body.username.length > 64) throw new AuthError('请填写开发责任人的系统账号。');
const account = getDatabase().prepare('SELECT id, username, display_name FROM accounts WHERE username = ? AND disabled = 0')
.bind(body.username.trim().toLowerCase()).first<{ id: string; username: string; display_name: string }>();
if (!account) throw new AuthError('未找到启用的账号,请核对开发责任人的系统账号。');
if (account.id === ownerId) throw new AuthError('开发责任人与业务确认人须使用不同账号。');
if (account.id === d.review?.developer.userId) throw new AuthError('该账号已是本需求的开发责任人。');
const previous = d.review?.developer;
changeDeveloper(d, { userId: account.id, username: account.username, displayName: account.display_name }, now);
return { label: previous ? '更换开发责任人' : '指定开发责任人', detail: `${previous ? previous.displayName + ' → ' : ''}${account.display_name}(${account.username})。该账号负责后续方案与交付;更换后需重新评审并确认计划。` };
}
if (body.action === 'propose-solution') {
if (d.review?.developer.userId !== user.userId) throw new AuthError('只有当前开发责任人可以提交方案。', 403);
const parsed = proposalSchema.safeParse(body);
if (!parsed.success) throw new AuthError('请填写实施范围与功能说明(6000 字以内)和范围外事项(3000 字以内,无则填“无”)。');
const previous = currentProposal(d);
if (previous && !previous.invalidated && previous.decision?.result !== 'changes_requested') throw new AuthError('当前方案已提交,请等待业务方确认或退回修改。', 409);
const version = (previous?.version ?? 0) + 1;
d.review.proposals.push({ ...parsed.data, version, developer: person(user), submittedAt: now, businessFields: structuredClone(d.fields), businessAttachments: structuredClone(d.attachments ?? []) });
d.fields.scope = parsed.data.scope;
d.fields.outOfScope = parsed.data.outOfScope;
return { label: '提交评审方案', detail: `方案 v${version} 已由 ${user.displayName} 提交,等待业务方确认。` };
}
if (body.action === 'confirm-solution' || body.action === 'return-solution') {
if (!business) throw new AuthError('只有需求登记账号可以代表业务方确认或退回方案。', 403);
const proposal = currentProposal(d);
if (!proposal || proposal.invalidated || proposal.developer.userId !== d.review?.developer.userId || body.proposalVersion !== proposal.version) throw new AuthError('方案已变化,请刷新后核对当前版本。', 409);
const confirming = body.action === 'confirm-solution';
if (proposal.decision && (confirming || proposal.decision.result !== 'confirmed')) throw new AuthError('该版本已处理,请刷新查看最新结果。', 409);
if (confirming && body.confirm !== true) throw new AuthError('请明确勾选同意按当前方案实施。');
const comment = typeof body.comment === 'string' ? body.comment.trim() : '';
if ((!confirming && !comment) || comment.length > 3000) throw new AuthError('请填写修改意见(3000 字以内)。');
// A request to revise an already confirmed version invalidates it without overwriting its original confirmation.
if (proposal.decision?.result === 'confirmed') {
invalidateProposal(d, now, `业务方要求调整:${comment}`);
} else {
proposal.decision = { result: confirming ? 'confirmed' : 'changes_requested', actor: person(user), time: now, comment };
}
return { label: confirming ? '业务方确认方案' : '退回修改方案', detail: `方案 v${proposal.version};${user.displayName} ${confirming ? '同意按此版本实施。' : '要求修改:'}${comment}` };
}
throw new AuthError('不支持的评审操作。');
}