Files
tap/lib/demand.ts
T

33 lines
4.2 KiB
TypeScript
Raw Normal View History

import { z } from 'zod';
import type { Attachment } from './attachments.ts';
import { workflowTask, pendingChange, type ChangeRecord, type Delivery, type DeliveryPlan, type TestingRound } from './workflow.ts';
import { reviewStage, type DemandAccess, type DemandReview } from './demand-review.ts';
export const statuses = ['待评审','已排期','开发中','待验收','待发布','已上线','已完成'] as const;
export const fieldSchema = z.object({
title:z.string().trim().max(100),project:z.string().trim().max(100),module:z.string().trim().max(100),type:z.enum(['新增功能','功能优化','缺陷修复','配置/数据变更']),proposer:z.string().trim().max(100),department:z.string().trim().max(100),source:z.string().trim().max(200),owner:z.string().trim().max(100),
background:z.string().trim().max(6000),impact:z.string().trim().max(3000),goal:z.string().trim().max(3000),expectedDate:z.string().max(10),noFixedDate:z.boolean(),dateReason:z.string().trim().max(1000),
scope:z.string().trim().max(6000),outOfScope:z.string().trim().max(3000),criteria:z.string().trim().max(6000),priority:z.enum(['P0','P1','P2']),priorityReason:z.string().trim().max(1000),dataSource:z.string().trim().max(3000),dataContact:z.string().trim().max(300),noData:z.boolean(),risk:z.string().trim().max(3000),pending:z.string().trim().max(3000),references:z.string().trim().max(5000),version:z.string().trim().max(100),
});
export type Fields=z.infer<typeof fieldSchema>;
export type History={time:string;actor:string;action:string;detail:string};
export type AcceptanceItem={id:string;feature:string;scenario:string;expected:string};
export type Demand={attachments?:Attachment[];review?:DemandReview;access?:DemandAccess;id:string;revision:number;fields:Fields;status:string;createdAt:string;updatedAt:string;reviewReady:boolean;blocked?:string;history:History[];changes:ChangeRecord[];acceptance?:TestingRound;acceptanceRecords?:{time:string;actor:string;version:string;testVersion:string;round?:number;results:string[];notes:string;passed:boolean;items?: (AcceptanceItem & {result:string})[];environment?:string}[];plan?:DeliveryPlan;delivery?:Delivery};
export const emptyFields:Fields={title:'',project:'',module:'',type:'新增功能',proposer:'',department:'',source:'',owner:'',background:'',impact:'',goal:'',expectedDate:'',noFixedDate:false,dateReason:'',scope:'',outOfScope:'',criteria:'',priority:'P2',priorityReason:'',dataSource:'',dataContact:'',noData:false,risk:'',pending:'',references:'',version:'v1.0'};
export function missingFields(f:Fields,review=false):string[]{
const required: [keyof Fields,string][]=[['title','需求标题'],['project','所属项目'],['module','业务模块'],['proposer','提出人'],['department','所属部门'],['source','需求来源'],['owner','需求负责人'],['background','业务背景'],['impact','影响对象与依据'],['goal','预期目标']];
const missing=required.filter(([k])=>!f[k]).map(([,label])=>label);
if(!f.noFixedDate){if(!/^\d{4}-\d{2}-\d{2}$/.test(f.expectedDate)||Number.isNaN(Date.parse(f.expectedDate))||new Date(f.expectedDate).toISOString().slice(0,10)!==f.expectedDate)missing.push('有效的期望上线日期');if(!f.dateReason)missing.push('期望日期依据');}
if(review){const rs:[keyof Fields,string][]=[['criteria','验收标准'],['priorityReason','优先级依据'],['risk','依赖与风险'],['pending','待确认事项'],['version','文档版本']];missing.push(...rs.filter(([k])=>!f[k]).map(([,l])=>l));if(!f.noData){if(!f.dataSource)missing.push('数据来源');if(!f.dataContact)missing.push('数据提供人');}}
return missing;
}
export function needsAction(d:Demand){
if(pendingChange(d)||d.blocked)return Boolean(workflowTask(d));
if(workflowTask(d))return true;
const business=d.access?.business??true;
if(d.status==='待验收')return business;
if(d.status!=='待评审')return false;
const stage=reviewStage(d);
return Boolean(business&&['资料待补充','待业务确认方案'].includes(stage)||d.access?.developer&&['待开发提出方案','待开发修改方案'].includes(stage));
}
export function isActive(d:Demand){return !['草稿','已完成','已取消'].includes(d.status);}
export function isMyDemand(d:Demand){return d.access===undefined||d.access.business||d.access.developer;}