refactor(data): 收口关系表运行时数据源
Some checks failed
Deploy Production / Build, push, deploy, verify (push) Has been cancelled
Some checks failed
Deploy Production / Build, push, deploy, verify (push) Has been cancelled
- 移除已迁移业务 AppData 运行时 fallback,改走领域 API 和关系表快读 - 补齐需求产品负责人、版本计划任务 JSON 和成员 username 回填迁移 - 统一治理字典入口,并补充 AI provider、数据源契约和领域服务测试 Co-Authored-By: Codex GPT-5 <codex@openai.com>
This commit is contained in:
@@ -1,6 +1,7 @@
|
||||
'use client';
|
||||
import { create } from 'zustand';
|
||||
import type { VersionPlan, PlanType } from '@/lib/version-plan';
|
||||
import type { VersionPlan } from '@/lib/version-plan';
|
||||
import { mergeVersionPlanPatch } from '@/lib/version-plan';
|
||||
import {
|
||||
completeVersionPlanByVersionId,
|
||||
createVersionPlanByVersionId,
|
||||
@@ -9,12 +10,11 @@ import {
|
||||
updateVersionPlanByVersionId,
|
||||
} from '@/lib/domain-api';
|
||||
import { scheduleSaveWithOptimisticRollback } from '@/lib/optimistic-persistence';
|
||||
import { loadServerData, saveServerData, SERVER_DATA_CACHE_MS } from '@/lib/server-data';
|
||||
import { SERVER_DATA_CACHE_MS, SERVER_DATA_SAVE_ERROR_EVENT } from '@/lib/server-data';
|
||||
import { getPlanCompletionState } from '@/lib/version-plan-workflow';
|
||||
import type { PlanResultPayload } from '@/lib/version-plan-workflow';
|
||||
import {
|
||||
makeVersionPlanCompletedActivity,
|
||||
makeVersionPlanCreatedActivity,
|
||||
makeVersionPlanRequirementProgressActivity,
|
||||
makeVersionPlanResearchDirectionProgressActivity,
|
||||
makeVersionPlanStartedActivity,
|
||||
@@ -26,13 +26,6 @@ import { useWorkActivityStore } from './useWorkActivityStore';
|
||||
const MOCK_PLANS: VersionPlan[] = [];
|
||||
let lastPlansFetchAt = 0;
|
||||
|
||||
async function loadStored(): Promise<VersionPlan[] | null> {
|
||||
try {
|
||||
return await loadServerData<VersionPlan[]>('version-plans');
|
||||
} catch {}
|
||||
return null;
|
||||
}
|
||||
|
||||
interface VersionPlanState {
|
||||
plans: VersionPlan[];
|
||||
loaded: boolean;
|
||||
@@ -48,18 +41,31 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
loaded: false,
|
||||
|
||||
fetchPlans: async (options) => {
|
||||
if (!options?.force && get().loaded && Date.now() - lastPlansFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
if (!options?.versionId && !options?.force && get().loaded && Date.now() - lastPlansFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
const cached = options?.versionId
|
||||
? await listVersionPlansByVersionId(options.versionId).catch(loadStored)
|
||||
: await loadStored();
|
||||
if (!options?.force && get().loaded && Date.now() - lastPlansFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
? await listVersionPlansByVersionId(options.versionId).catch(() => [])
|
||||
: get().plans;
|
||||
if (!options?.versionId && !options?.force && get().loaded && Date.now() - lastPlansFetchAt < SERVER_DATA_CACHE_MS) return;
|
||||
lastPlansFetchAt = Date.now();
|
||||
set({ plans: cached ?? MOCK_PLANS, loaded: true });
|
||||
set({
|
||||
plans: options?.versionId
|
||||
? mergeVersionPlansForVersion(get().plans, options.versionId, cached ?? MOCK_PLANS)
|
||||
: cached ?? MOCK_PLANS,
|
||||
loaded: true,
|
||||
});
|
||||
},
|
||||
|
||||
createPlan: (data) => {
|
||||
const previous = get().plans;
|
||||
const plan: VersionPlan = { ...data, id: `plan-${Date.now()}`, createdAt: new Date().toISOString().slice(0, 10) };
|
||||
const now = new Date().toISOString();
|
||||
const autoStarted = shouldAutoStartCreatedPlan(data, now);
|
||||
const plan: VersionPlan = {
|
||||
...data,
|
||||
status: autoStarted ? 'in_progress' : data.status,
|
||||
actualStartAt: autoStarted ? data.actualStartAt ?? now : data.actualStartAt,
|
||||
id: `plan-${Date.now()}`,
|
||||
createdAt: now.slice(0, 10),
|
||||
};
|
||||
const plans = [...previous, plan];
|
||||
set({ plans, loaded: true });
|
||||
scheduleSaveWithOptimisticRollback({
|
||||
@@ -68,14 +74,14 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
const result = await createVersionPlanByVersionId(plan.versionId, plan);
|
||||
set({
|
||||
plans: get().plans.map((item) => (
|
||||
item.id === plan.id ? { ...plan, ...result.item, tasks: plan.tasks } : item
|
||||
item.id === plan.id ? mergeCreatedVersionPlan(plan, item, result.item) : item
|
||||
)),
|
||||
loaded: true,
|
||||
});
|
||||
appendDomainActivities(result.activities);
|
||||
} catch {
|
||||
await saveVersionPlansFallback(plans);
|
||||
useWorkActivityStore.getState().addActivity(makeVersionPlanCreatedActivity(plan, plan.addedBy || plan.owner));
|
||||
} catch (error) {
|
||||
notifyVersionPlanSaveFailure(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
expected: plans,
|
||||
@@ -88,9 +94,10 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
const previous = get().plans;
|
||||
const now = new Date().toISOString();
|
||||
const activities: WorkActivityDraft[] = [];
|
||||
let apiPatch: Partial<VersionPlan> | null = null;
|
||||
const plans = previous.map((p) => {
|
||||
if (p.id !== id) return p;
|
||||
const patch = { ...data };
|
||||
const patch = { ...mergeVersionPlanPatch(p, data) };
|
||||
if (patch.status === 'in_progress' && !p.actualStartAt) {
|
||||
(patch as any).actualStartAt = now;
|
||||
}
|
||||
@@ -112,6 +119,7 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
const directionActivity = makeVersionPlanResearchDirectionProgressActivity(next, log);
|
||||
if (directionActivity) activities.push(directionActivity);
|
||||
}
|
||||
apiPatch = patch;
|
||||
return next;
|
||||
});
|
||||
set({ plans, loaded: true });
|
||||
@@ -120,14 +128,14 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
try {
|
||||
const versionId = previous.find((plan) => plan.id === id)?.versionId;
|
||||
if (!versionId) throw new Error('missing versionId');
|
||||
const result = await updateVersionPlanByVersionId(versionId, id, data);
|
||||
const result = await updateVersionPlanByVersionId(versionId, id, apiPatch ?? data);
|
||||
appendDomainActivities(result.activities);
|
||||
if (result.activities.length === 0) {
|
||||
activities.forEach((activity) => useWorkActivityStore.getState().addActivity(activity));
|
||||
}
|
||||
} catch {
|
||||
await saveVersionPlansFallback(plans);
|
||||
activities.forEach((activity) => useWorkActivityStore.getState().addActivity(activity));
|
||||
} catch (error) {
|
||||
notifyVersionPlanSaveFailure(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
expected: plans,
|
||||
@@ -159,8 +167,9 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
if (!versionId) throw new Error('missing versionId');
|
||||
const apiResult = await completeVersionPlanByVersionId(versionId, id, result);
|
||||
appendDomainActivities(apiResult.activities);
|
||||
} catch {
|
||||
await saveVersionPlansFallback(plans);
|
||||
} catch (error) {
|
||||
notifyVersionPlanSaveFailure(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
expected: plans,
|
||||
@@ -180,8 +189,9 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
const versionId = previous.find((plan) => plan.id === id)?.versionId;
|
||||
if (!versionId) throw new Error('missing versionId');
|
||||
await deleteVersionPlanByVersionId(versionId, id);
|
||||
} catch {
|
||||
await saveVersionPlansFallback(plans);
|
||||
} catch (error) {
|
||||
notifyVersionPlanSaveFailure(error);
|
||||
throw error;
|
||||
}
|
||||
},
|
||||
expected: plans,
|
||||
@@ -191,10 +201,6 @@ export const useVersionPlanStore = create<VersionPlanState>((set, get) => ({
|
||||
},
|
||||
}));
|
||||
|
||||
async function saveVersionPlansFallback(plans: VersionPlan[]) {
|
||||
await saveServerData('version-plans', plans);
|
||||
}
|
||||
|
||||
function appendDomainActivities(activities: WorkActivity[]) {
|
||||
if (activities.length === 0) return;
|
||||
useWorkActivityStore.setState((state) => ({
|
||||
@@ -202,3 +208,42 @@ function appendDomainActivities(activities: WorkActivity[]) {
|
||||
loaded: true,
|
||||
}));
|
||||
}
|
||||
|
||||
function mergeVersionPlansForVersion(current: VersionPlan[], versionId: string, scopedPlans: VersionPlan[]) {
|
||||
return [
|
||||
...current.filter((plan) => plan.versionId !== versionId),
|
||||
...scopedPlans,
|
||||
];
|
||||
}
|
||||
|
||||
function shouldAutoStartCreatedPlan(data: Omit<VersionPlan, 'id' | 'createdAt'>, nowIso: string): boolean {
|
||||
if (data.status !== 'pending' || !data.startTime || data.actualStartAt) return false;
|
||||
return new Date(data.startTime).getTime() <= new Date(nowIso).getTime();
|
||||
}
|
||||
|
||||
function mergeCreatedVersionPlan(initial: VersionPlan, current: VersionPlan, persisted: VersionPlan): VersionPlan {
|
||||
const localChanged = current.status !== initial.status
|
||||
|| current.actualStartAt !== initial.actualStartAt
|
||||
|| current.completedAt !== initial.completedAt;
|
||||
|
||||
return {
|
||||
...current,
|
||||
...persisted,
|
||||
status: localChanged ? current.status : persisted.status,
|
||||
actualStartAt: current.actualStartAt ?? persisted.actualStartAt,
|
||||
completedAt: current.completedAt ?? persisted.completedAt,
|
||||
tasks: current.tasks ?? initial.tasks ?? persisted.tasks,
|
||||
};
|
||||
}
|
||||
|
||||
function notifyVersionPlanSaveFailure(error: unknown) {
|
||||
if (typeof window === 'undefined') return;
|
||||
window.dispatchEvent(
|
||||
new CustomEvent(SERVER_DATA_SAVE_ERROR_EVENT, {
|
||||
detail: {
|
||||
key: 'version-plans',
|
||||
message: error instanceof Error && error.message ? error.message : '版本计划保存失败,请检查 API 服务',
|
||||
},
|
||||
}),
|
||||
);
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user