feat(v2.4): 切换需求池领域主写
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
import { api } from './api';
|
||||
import type { Priority } from './derive';
|
||||
import type { Requirement, RequirementStatus, SourceType } from './requirement';
|
||||
|
||||
export interface RootProject {
|
||||
id: string;
|
||||
@@ -89,6 +91,62 @@ export async function deleteVersionByProductId(productId: string, versionId: str
|
||||
await api.delete(`/products/${productId}/versions/${versionId}`);
|
||||
}
|
||||
|
||||
interface DomainRequirementRow {
|
||||
id: string;
|
||||
productId: string;
|
||||
projectId?: string | null;
|
||||
versionId?: string | null;
|
||||
code: string;
|
||||
title: string;
|
||||
description?: string | null;
|
||||
status?: string | null;
|
||||
priority?: string | number | null;
|
||||
type?: string | null;
|
||||
sourceType?: string | null;
|
||||
sourceTarget?: string | null;
|
||||
platform?: string | null;
|
||||
creatorId?: string | null;
|
||||
creatorName?: string | null;
|
||||
creator?: { id?: string | null; name?: string | null } | null;
|
||||
createdAt?: string | Date | null;
|
||||
}
|
||||
|
||||
export async function createRequirementByProductId(
|
||||
productId: string,
|
||||
data: Partial<Requirement>,
|
||||
): Promise<Requirement> {
|
||||
return normalizeRequirement(await api.post<DomainRequirementRow>(
|
||||
`/products/${productId}/requirements`,
|
||||
toRequirementPayload(data),
|
||||
));
|
||||
}
|
||||
|
||||
export async function updateRequirementByProductId(
|
||||
productId: string,
|
||||
requirementId: string,
|
||||
data: Partial<Requirement>,
|
||||
): Promise<Requirement> {
|
||||
return normalizeRequirement(await api.patch<DomainRequirementRow>(
|
||||
`/products/${productId}/requirements/${requirementId}`,
|
||||
toRequirementPayload(data),
|
||||
));
|
||||
}
|
||||
|
||||
export async function updateRequirementStatusByProductId(
|
||||
productId: string,
|
||||
requirementId: string,
|
||||
status: RequirementStatus,
|
||||
): Promise<Requirement> {
|
||||
return normalizeRequirement(await api.patch<DomainRequirementRow>(
|
||||
`/products/${productId}/requirements/${requirementId}/status`,
|
||||
{ status },
|
||||
));
|
||||
}
|
||||
|
||||
export async function deleteRequirementByProductId(productId: string, requirementId: string): Promise<void> {
|
||||
await api.delete(`/products/${productId}/requirements/${requirementId}`);
|
||||
}
|
||||
|
||||
function normalizeProductRoot(product: RootProduct): RootProduct {
|
||||
return {
|
||||
...product,
|
||||
@@ -108,3 +166,74 @@ function normalizeVersionRoot(version: RootVersion): RootVersion {
|
||||
links: version.links && typeof version.links === 'object' ? version.links : {},
|
||||
};
|
||||
}
|
||||
|
||||
function toRequirementPayload(data: Partial<Requirement>) {
|
||||
return {
|
||||
...(data.code !== undefined && { code: data.code }),
|
||||
...(data.title !== undefined && { title: data.title }),
|
||||
...(data.description !== undefined && { description: data.description }),
|
||||
...(data.projectId !== undefined && { projectId: data.projectId }),
|
||||
...(data.versionId !== undefined && { versionId: data.versionId ?? null }),
|
||||
...(data.typeId !== undefined && { type: data.typeId }),
|
||||
...(data.sourceType !== undefined && { sourceType: data.sourceType }),
|
||||
...(data.sourceTarget !== undefined && { sourceTarget: data.sourceTarget }),
|
||||
...(data.platforms !== undefined && { platform: data.platforms.join(',') }),
|
||||
...(data.priority !== undefined && { priority: priorityToNumber(data.priority) }),
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeRequirement(row: DomainRequirementRow): Requirement {
|
||||
return {
|
||||
id: row.id,
|
||||
code: row.code,
|
||||
title: row.title,
|
||||
description: row.description ?? '',
|
||||
productId: row.productId,
|
||||
projectId: row.projectId ?? '',
|
||||
versionId: row.versionId ?? undefined,
|
||||
sourceType: toSourceType(row.sourceType),
|
||||
sourceTarget: row.sourceTarget ?? '',
|
||||
platforms: splitCsv(row.platform),
|
||||
typeId: row.type ?? '',
|
||||
status: toRequirementStatus(row.status),
|
||||
priority: toPriority(row.priority),
|
||||
effort: 'M',
|
||||
creator: row.creator?.name ?? row.creatorName ?? row.creatorId ?? '',
|
||||
createdAt: isoString(row.createdAt),
|
||||
};
|
||||
}
|
||||
|
||||
function toSourceType(value: string | null | undefined): SourceType {
|
||||
const allowed = new Set<SourceType>(['customer', 'internal', 'operation', 'aftersale', 'market', 'competitor', 'management']);
|
||||
return allowed.has(value as SourceType) ? value as SourceType : 'internal';
|
||||
}
|
||||
|
||||
function toRequirementStatus(value: string | null | undefined): RequirementStatus {
|
||||
const allowed = new Set<RequirementStatus>(['pending_review', 'adopted', 'rejected', 'planned', 'developing', 'testing', 'released', 'closed']);
|
||||
return allowed.has(value as RequirementStatus) ? value as RequirementStatus : 'pending_review';
|
||||
}
|
||||
|
||||
function toPriority(value: string | number | null | undefined): Priority {
|
||||
if (typeof value === 'string' && /^P[0-4]$/.test(value)) return value as Priority;
|
||||
const parsed = Number(value ?? 2);
|
||||
const normalized = Number.isFinite(parsed) ? Math.max(0, Math.min(4, Math.floor(parsed))) : 2;
|
||||
return `P${normalized}` as Priority;
|
||||
}
|
||||
|
||||
function priorityToNumber(value: Priority | undefined): number | undefined {
|
||||
if (!value) return undefined;
|
||||
const match = /^P([0-4])$/.exec(value);
|
||||
return match ? Number(match[1]) : undefined;
|
||||
}
|
||||
|
||||
function splitCsv(value: string | null | undefined): string[] {
|
||||
if (!value) return [];
|
||||
return value.split(',').map((item) => item.trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
function isoString(value: string | Date | null | undefined): string {
|
||||
if (!value) return new Date().toISOString();
|
||||
if (value instanceof Date) return value.toISOString();
|
||||
const time = new Date(value).getTime();
|
||||
return Number.isFinite(time) ? new Date(time).toISOString() : new Date().toISOString();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user