feat(api): scheduled user sync with mock source (T30 semantics)
P4a Task 7 —— cn 侧用户定期同步(mock 源先行,G4 真实接口到位后仅替换
UserSourcePort provider,引擎/契约/表不变)。
数据模型(db-cn 迁移 20260920114149,utf8mb4_0900_bin 逐表校对):
- user_sync_runs:批次账本(trigger cron|manual、state running|success|
partial|failed、cursor 水位、counts JSON、起止时间与错误摘要);
- user_sync_errors:单批次坏行明细(runId 索引);
- external_user_mappings:本地↔外部 ID 映射(@@unique([sourceSystem,
externalUserId]),sourceVersion 逐行高水位);
- user_sync_state:单行状态(lastRunAt/lastSuccessAt/cursor/runCount)。
同步引擎(UserSyncEngine):
- Redis SETNX 分布式互斥(TTL 10min,锁值=runId,Lua CAS 释放);
定时 tick 与手动触发共用,运行中批次复用 runId(202);
- 游标增量 + 周期全量对账(RECONCILE_EVERY_N_RUNS 默认 10);
- 逐页单事务提交(run counts+cursor / state cursor),分页中断→failed
且水位不前进、会话不动;
- 红线遵守:sync 不写 password_hash/role/album_members/localLocked;
空/部分页不误伤停用;上游恢复不解除本地锁定;
- 新用户按 pending 建(随机不可用哨兵哈希,激活+重置后方可登录);
- 停用即撤销该用户全部会话;本地预建同名账号走关联而非重建。
Admin API(/admin/user-sync):GET status|runs|errors?runId=、POST runs
(202 {runId});runs 为 startedAt+ID 倒序游标分页。
auth.service:登录对哨兵/损坏哈希加固(等代价 dummy verify,
401 而非 500,时序平坦)。
测试:user-sync.e2e-spec 12 用例(首轮全量/改名幂等/停用撤会话/
恢复不解锁/新增/空页/分页中断/部分无效/手动互斥/增量+对账/API 形状
与权限/pending 登录门);api e2e 22 套件 239 用例全绿。
Co-Authored-By: Claude Code <noreply@anthropic.com>
This commit is contained in:
@@ -277,3 +277,58 @@ model usage_alerts {
|
||||
resolvedAt DateTime? @db.DateTime(6) // 非空=已解除(阈值上调/用量回落/维度停用)
|
||||
@@index([resolvedAt])
|
||||
}
|
||||
// —— 用户同步域(P4a Task 7,A29/T30 语义,mock 源先行)——
|
||||
// 三条红线(技术方案 3.1A):同步绝不写 password_hash/role/album_members/
|
||||
// localLocked;空响应/部分页绝不停用任何人;上游恢复(disabled→active)
|
||||
// 不解除本地锁定。外部身份经 external_user_mappings 隔离(稳定外部 ID 锚点,
|
||||
// 不在 users 上加列)。
|
||||
// · user_sync_runs:批次账本。cursor = 已提交源水位(VarChar 承载 G4 真实
|
||||
// 源可能出现的非数值游标;mock 源恒为数值串,API 层投影为契约 int),
|
||||
// 逐页提交(分页中断时停在最后成功页);counts Json = {added,updated,
|
||||
// disabled,failed};state partial=有坏行但其余照常应用。
|
||||
// · user_sync_errors:坏行明细(partial/failed 批次的证据链)。
|
||||
// · external_user_mappings:本地用户 ↔ 外部身份。userId 为扁平字符串外键
|
||||
// (同相册域约定:跨域删除语义由应用层决策,且减轻 TRUNCATE 隔离负担)。
|
||||
// · user_sync_state:单行引擎状态(lastRunAt/lastSuccessAt/已提交水位/
|
||||
// runCount——runCount 驱动「每 N 批强制全量对账」节奏)。
|
||||
model user_sync_runs {
|
||||
id String @id @default(cuid())
|
||||
trigger String @db.VarChar(16) // cron|manual(MySQL 保留字,Prisma 恒反引号)
|
||||
state String @default("running") @db.VarChar(16) // running|success|partial|failed
|
||||
cursor String? @db.VarChar(128) // 已提交源水位;未提交为 null
|
||||
counts Json // {added,updated,disabled,failed}
|
||||
startedAt DateTime @default(now()) @db.DateTime(6)
|
||||
finishedAt DateTime? @db.DateTime(6)
|
||||
error String? @db.VarChar(1024) // 批次级失败摘要(分页中断等)
|
||||
|
||||
@@index([startedAt])
|
||||
}
|
||||
model user_sync_errors {
|
||||
id String @id @default(cuid())
|
||||
runId String @db.VarChar(64)
|
||||
externalUserId String? @db.VarChar(64) // 坏行可能连外部 ID 都不合规 → 可空
|
||||
code String @db.VarChar(64) // invalid_record|apply_failed|...
|
||||
detail String @db.VarChar(1024)
|
||||
createdAt DateTime @default(now()) @db.DateTime(6)
|
||||
|
||||
@@index([runId])
|
||||
}
|
||||
model external_user_mappings {
|
||||
id String @id @default(cuid())
|
||||
userId String @db.VarChar(64) // 扁平字符串外键(无 relation/FK,同相册域)
|
||||
sourceSystem String @default("mock") @db.VarChar(32) // G4 后换真实源标识
|
||||
externalUserId String @db.VarChar(64)
|
||||
sourceVersion Int @default(0) // 幂等比较位:仅更高版本应用
|
||||
createdAt DateTime @default(now()) @db.DateTime(6)
|
||||
|
||||
@@unique([sourceSystem, externalUserId])
|
||||
@@index([userId])
|
||||
}
|
||||
model user_sync_state {
|
||||
id String @id // 固定单行 id(USER_SYNC_STATE_SINGLETON_ID;迁移种子)
|
||||
lastRunAt DateTime? @db.DateTime(6)
|
||||
lastSuccessAt DateTime? @db.DateTime(6) // 仅 state=success 批次推进(partial 不算完全成功)
|
||||
cursor String? @db.VarChar(128) // 已提交源水位(与 runs.cursor 同形)
|
||||
runCount Int @default(0) // 已启动批次数(含失败),驱动周期全量对账
|
||||
updatedAt DateTime @updatedAt @db.DateTime(6)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user