Files

78 lines
2.5 KiB
TypeScript
Raw Permalink Normal View History

import { getRuntimeEnv } from "../../../lib/runtime-env";
import { runInBackground } from "../../../lib/background";
const env = getRuntimeEnv();
2026-07-30 12:06:41 +08:00
import { backfillAccountProfiles } from "../../../lib/account-enrichment-service";
import { runDueScheduledCollections } from "../../../lib/collection-service";
import {
resolveCollectionMcpConfig,
type CollectionMcpBindings,
} from "../../../lib/mcp-collection-client";
import {
ensureSchema,
getDashboardData,
getRawDb,
seedIfEmpty,
} from "../../../lib/mvp-db";
import { ensurePartnersForPoolAccounts } from "../../../lib/resource-write";
import { authForbidden, getRequestPrincipal } from "../../../lib/user-auth";
import { getOwnerScope, ownerScopeUserid } from "../../../lib/owner-scope";
2026-07-30 12:06:41 +08:00
export async function GET(request: Request) {
const principal = await getRequestPrincipal(request);
if (!principal) return authForbidden();
2026-07-30 12:06:41 +08:00
try {
await ensureSchema();
await seedIfEmpty();
// 资源池 KOC 默认加入合作方:在返回面板数据前补齐,失败不阻塞看板
await ensurePartnersForPoolAccounts().catch((error) => {
console.error(
"KOC pool partner sync failed",
error instanceof Error ? error.message : "unknown error",
);
});
2026-07-30 12:06:41 +08:00
const db = getRawDb();
const mcpConfig = resolveCollectionMcpConfig(
env as unknown as CollectionMcpBindings,
);
const collectionCatchup = runDueScheduledCollections(
db,
Date.now(),
mcpConfig,
"catchup",
).catch((error) => {
console.error(
"KOC collection catchup failed",
error instanceof Error ? error.message : "unknown error",
);
});
const accountBackfill = backfillAccountProfiles(
db,
mcpConfig,
10,
)
.then((result) => {
console.info("KOC account backfill completed", result);
})
.catch((error) => {
console.error(
"KOC account backfill failed",
error instanceof Error ? error.message : "unknown error",
);
});
const catchup = Promise.all([
collectionCatchup,
accountBackfill,
]);
runInBackground(catchup, "bootstrap catchup");
const scope = await getOwnerScope(request);
const dashboard = await getDashboardData(ownerScopeUserid(scope));
return Response.json(dashboard);
2026-07-30 12:06:41 +08:00
} catch (error) {
return Response.json(
{ error: error instanceof Error ? error.message : "加载失败" },
{ status: 500 },
);
}
}