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();
|
||||
}
|
||||
|
||||
67
apps/web/lib/requirement-domain-write-source.test.ts
Normal file
67
apps/web/lib/requirement-domain-write-source.test.ts
Normal file
@@ -0,0 +1,67 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import test from 'node:test';
|
||||
|
||||
const storeSource = () => readFileSync(join(process.cwd(), 'stores/useRequirementStore.ts'), 'utf8');
|
||||
const pageSource = () => readFileSync(join(process.cwd(), 'app/requirements/page.tsx'), 'utf8');
|
||||
|
||||
function storeMethodBody(text: string, name: string) {
|
||||
const start = text.indexOf(` ${name}:`);
|
||||
assert.notEqual(start, -1, `missing store method ${name}`);
|
||||
|
||||
let depth = 0;
|
||||
let sawFirstBrace = false;
|
||||
for (let i = start; i < text.length; i += 1) {
|
||||
const char = text[i];
|
||||
if (char === '{') {
|
||||
depth += 1;
|
||||
sawFirstBrace = true;
|
||||
}
|
||||
if (char === '}') {
|
||||
depth -= 1;
|
||||
if (sawFirstBrace && depth === 0) {
|
||||
return text.slice(start, i + 1);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
throw new Error(`could not extract store method ${name}`);
|
||||
}
|
||||
|
||||
test('requirement store imports domain requirement write helpers', () => {
|
||||
const text = storeSource();
|
||||
|
||||
assert.match(text, /from '@\/lib\/domain-api'/);
|
||||
assert.match(text, /createRequirementByProductId/);
|
||||
assert.match(text, /updateRequirementByProductId/);
|
||||
assert.match(text, /deleteRequirementByProductId/);
|
||||
});
|
||||
|
||||
test('requirement mutations use domain APIs instead of AppData requirements saves', () => {
|
||||
const text = storeSource();
|
||||
const createRequirement = storeMethodBody(text, 'createRequirement');
|
||||
const updateRequirement = storeMethodBody(text, 'updateRequirement');
|
||||
const deleteRequirement = storeMethodBody(text, 'deleteRequirement');
|
||||
|
||||
assert.match(createRequirement, /createRequirementByProductId\(newReq\.productId,/);
|
||||
assert.match(updateRequirement, /updateRequirementByProductId\(productId, id,/);
|
||||
assert.match(deleteRequirement, /deleteRequirementByProductId\(productId, id\)/);
|
||||
assert.doesNotMatch(createRequirement, /saveServerData\('requirements'/);
|
||||
assert.doesNotMatch(updateRequirement, /saveServerData\('requirements'/);
|
||||
assert.doesNotMatch(deleteRequirement, /saveServerData\('requirements'/);
|
||||
});
|
||||
|
||||
test('requirement pool uses server pagination without full AppData load for scoped list queries', () => {
|
||||
const text = pageSource();
|
||||
|
||||
assert.match(text, /loadV22RequirementsPage\(v22RequirementQuery\)/);
|
||||
assert.match(text, /if \(v22RequirementQuery && !v22RequirementsFailed\) return;\s+fetchRequirements\(\);/);
|
||||
});
|
||||
|
||||
test('relation-backed requirement rows remain mutable without AppData identity', () => {
|
||||
const text = pageSource();
|
||||
|
||||
assert.match(text, /const canMutateRequirement = \(req: Requirement\) => Boolean\(req\.productId\);/);
|
||||
assert.doesNotMatch(text, /appDataRequirementIds\.has\(req\.id\)/);
|
||||
});
|
||||
Reference in New Issue
Block a user