fix(wallet): 删除与过期删除 API Key 时同步禁用关联钱包 (#315)

- 在 delete_user_api_key 和 delete_standalone_api_key 事务中,先将关联 wallets.status 更新为 disabled
- 在过期 Key 自动清理流程中,删除前同步禁用对应钱包
- 保留 wallet 与交易流水,避免出现 orphaned 且 active 的钱包状态
This commit is contained in:
AAEE86
2026-04-20 14:03:17 +08:00
committed by GitHub
parent 13bf222b8d
commit b2ff7d2c28
3 changed files with 38 additions and 7 deletions

View File

@@ -656,6 +656,14 @@ SET api_key_id = NULL
WHERE api_key_id = $1
"#;
const DISABLE_WALLET_BY_API_KEY_ID_SQL: &str = r#"
UPDATE wallets
SET status = 'disabled',
updated_at = NOW()
WHERE api_key_id = $1
AND status <> 'disabled'
"#;
const DELETE_USER_API_KEY_SQL: &str = r#"
DELETE FROM api_keys
WHERE user_id = $1
@@ -1268,6 +1276,11 @@ impl AuthApiKeyWriteRepository for SqlxAuthApiKeySnapshotReadRepository {
.execute(&mut *tx)
.await
.map_postgres_err()?;
sqlx::query(DISABLE_WALLET_BY_API_KEY_ID_SQL)
.bind(api_key_id)
.execute(&mut *tx)
.await
.map_postgres_err()?;
let result = sqlx::query(DELETE_USER_API_KEY_SQL)
.bind(user_id)
.bind(api_key_id)
@@ -1290,6 +1303,11 @@ impl AuthApiKeyWriteRepository for SqlxAuthApiKeySnapshotReadRepository {
.execute(&mut *tx)
.await
.map_postgres_err()?;
sqlx::query(DISABLE_WALLET_BY_API_KEY_ID_SQL)
.bind(api_key_id)
.execute(&mut *tx)
.await
.map_postgres_err()?;
let result = sqlx::query(DELETE_STANDALONE_API_KEY_SQL)
.bind(api_key_id)
.execute(&mut *tx)