feat(v2.4): 切换根数据领域主写
This commit is contained in:
110
apps/web/lib/domain-api.ts
Normal file
110
apps/web/lib/domain-api.ts
Normal file
@@ -0,0 +1,110 @@
|
||||
import { api } from './api';
|
||||
|
||||
export interface RootProject {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
createdAt: string;
|
||||
}
|
||||
|
||||
export interface RootVersion {
|
||||
id: string;
|
||||
productId?: string;
|
||||
projectId?: string | null;
|
||||
name: string;
|
||||
status?: string;
|
||||
releaseDate: string | null;
|
||||
createdAt: string;
|
||||
currentStage?: string | null;
|
||||
startDate?: string | null;
|
||||
expectedReleaseDate?: string | null;
|
||||
members?: unknown[];
|
||||
progress?: unknown[];
|
||||
priority?: string;
|
||||
links?: unknown;
|
||||
}
|
||||
|
||||
export interface RootProduct {
|
||||
id: string;
|
||||
name: string;
|
||||
description: string;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
projects: RootProject[];
|
||||
versions: RootVersion[];
|
||||
_count?: { requirements: number; projects: number; versions?: number };
|
||||
}
|
||||
|
||||
export async function createProductRoot(data: { name: string; description?: string }): Promise<RootProduct> {
|
||||
const product = await api.post<Omit<RootProduct, 'projects' | 'versions'>>('/products', data);
|
||||
return normalizeProductRoot({ ...product, projects: [], versions: [] });
|
||||
}
|
||||
|
||||
export async function updateProductRoot(
|
||||
productId: string,
|
||||
data: { name?: string; description?: string },
|
||||
): Promise<Partial<RootProduct>> {
|
||||
return api.patch<Partial<RootProduct>>(`/products/${productId}`, data);
|
||||
}
|
||||
|
||||
export async function deleteProductRoot(productId: string): Promise<void> {
|
||||
await api.delete(`/products/${productId}`);
|
||||
}
|
||||
|
||||
export async function createProjectByProductId(
|
||||
productId: string,
|
||||
data: { name: string; description?: string },
|
||||
): Promise<RootProject> {
|
||||
return api.post<RootProject>(`/products/${productId}/projects`, data);
|
||||
}
|
||||
|
||||
export async function updateProjectByProductId(
|
||||
productId: string,
|
||||
projectId: string,
|
||||
data: { name?: string; description?: string },
|
||||
): Promise<RootProject> {
|
||||
return api.patch<RootProject>(`/products/${productId}/projects/${projectId}`, data);
|
||||
}
|
||||
|
||||
export async function deleteProjectByProductId(productId: string, projectId: string): Promise<void> {
|
||||
await api.delete(`/products/${productId}/projects/${projectId}`);
|
||||
}
|
||||
|
||||
export async function createVersionByProductId(
|
||||
productId: string,
|
||||
data: { name: string; status?: string; projectId?: string },
|
||||
): Promise<RootVersion> {
|
||||
return normalizeVersionRoot(await api.post<RootVersion>(`/products/${productId}/versions`, data));
|
||||
}
|
||||
|
||||
export async function updateVersionByProductId(
|
||||
productId: string,
|
||||
versionId: string,
|
||||
data: Record<string, unknown>,
|
||||
): Promise<RootVersion> {
|
||||
return normalizeVersionRoot(await api.patch<RootVersion>(`/products/${productId}/versions/${versionId}`, data));
|
||||
}
|
||||
|
||||
export async function deleteVersionByProductId(productId: string, versionId: string): Promise<void> {
|
||||
await api.delete(`/products/${productId}/versions/${versionId}`);
|
||||
}
|
||||
|
||||
function normalizeProductRoot(product: RootProduct): RootProduct {
|
||||
return {
|
||||
...product,
|
||||
projects: product.projects ?? [],
|
||||
versions: (product.versions ?? []).map(normalizeVersionRoot),
|
||||
_count: product._count ?? { requirements: 0, projects: product.projects?.length ?? 0, versions: product.versions?.length ?? 0 },
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeVersionRoot(version: RootVersion): RootVersion {
|
||||
return {
|
||||
...version,
|
||||
status: version.status ?? 'planned',
|
||||
releaseDate: version.releaseDate ?? null,
|
||||
members: Array.isArray(version.members) ? version.members : [],
|
||||
progress: Array.isArray(version.progress) ? version.progress : [],
|
||||
links: version.links && typeof version.links === 'object' ? version.links : {},
|
||||
};
|
||||
}
|
||||
61
apps/web/lib/product-domain-write-source.test.ts
Normal file
61
apps/web/lib/product-domain-write-source.test.ts
Normal file
@@ -0,0 +1,61 @@
|
||||
import assert from 'node:assert/strict';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import test from 'node:test';
|
||||
|
||||
const source = () => readFileSync(join(process.cwd(), 'stores/useProductStore.ts'), 'utf8');
|
||||
|
||||
function storeMethodBody(text: string, name: string) {
|
||||
const start = text.indexOf(`\n ${name}: async`);
|
||||
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('product root store imports domain root write helpers', () => {
|
||||
assert.match(source(), /from '@\/lib\/domain-api'/);
|
||||
});
|
||||
|
||||
test('project mutations use domain APIs instead of products-overview AppData saves', () => {
|
||||
const text = source();
|
||||
const createProject = storeMethodBody(text, 'createProject');
|
||||
const updateProject = storeMethodBody(text, 'updateProject');
|
||||
const deleteProject = storeMethodBody(text, 'deleteProject');
|
||||
|
||||
assert.match(createProject, /createProjectByProductId\(productId, data\)/);
|
||||
assert.match(updateProject, /updateProjectByProductId\(productId, projectId, data\)/);
|
||||
assert.match(deleteProject, /deleteProjectByProductId\(productId, projectId\)/);
|
||||
assert.doesNotMatch(createProject, /saveStoredOverview\(updated\)/);
|
||||
assert.doesNotMatch(updateProject, /saveStoredOverview\(updated\)/);
|
||||
assert.doesNotMatch(deleteProject, /saveStoredOverview\(updated\)/);
|
||||
});
|
||||
|
||||
test('version mutations use domain APIs instead of products-overview AppData saves', () => {
|
||||
const text = source();
|
||||
const createVersion = storeMethodBody(text, 'createVersion');
|
||||
const updateVersion = storeMethodBody(text, 'updateVersion');
|
||||
const deleteVersion = storeMethodBody(text, 'deleteVersion');
|
||||
|
||||
assert.match(createVersion, /createVersionByProductId\(productId, data\)/);
|
||||
assert.match(updateVersion, /updateVersionByProductId\(productId, versionId, data\)/);
|
||||
assert.match(deleteVersion, /deleteVersionByProductId\(productId, versionId\)/);
|
||||
assert.doesNotMatch(createVersion, /saveStoredOverview\(updated\)/);
|
||||
assert.doesNotMatch(updateVersion, /saveStoredOverview\(updated\)/);
|
||||
assert.doesNotMatch(deleteVersion, /saveStoredOverview\(updated\)/);
|
||||
});
|
||||
Reference in New Issue
Block a user