feat: add optimistic locking and improve type safety
- Implement optimistic locking for deposit balance updates - Prevent race conditions in concurrent deposit requests - Add automatic retry with exponential backoff (max 3 attempts) - Add version column to user_deposits table - Improve type safety across codebase - Add explicit types for Namecheap API responses - Add typed function arguments (ManageDepositArgs, etc.) - Remove `any` types from deposit-agent and tool files - Add reconciliation job for balance integrity verification - Compare user_deposits.balance vs SUM(confirmed transactions) - Alert admin on discrepancy detection - Set up test environment with Vitest + Miniflare - Add 50+ test cases for deposit system - Add helper functions for test data creation - Update documentation - Add migration guide for version columns - Document optimistic locking patterns Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
This commit is contained in:
25
src/index.ts
25
src/index.ts
@@ -5,6 +5,7 @@ import { handleApiRequest } from './routes/api';
|
||||
import { handleHealthCheck } from './routes/health';
|
||||
import { parseBankSMS } from './services/bank-sms-parser';
|
||||
import { matchPendingDeposit } from './services/deposit-matcher';
|
||||
import { reconcileDeposits, formatReconciliationReport } from './utils/reconciliation';
|
||||
|
||||
export default {
|
||||
// HTTP 요청 핸들러
|
||||
@@ -229,5 +230,29 @@ Documentation: https://github.com/your-repo
|
||||
} catch (error) {
|
||||
console.error('[Cron] 오류:', error);
|
||||
}
|
||||
|
||||
// 예치금 정합성 검증 (Reconciliation)
|
||||
console.log('[Cron] 예치금 정합성 검증 시작');
|
||||
try {
|
||||
const report = await reconcileDeposits(env.DB);
|
||||
|
||||
if (report.inconsistencies > 0) {
|
||||
// 관리자 알림 전송
|
||||
const adminId = env.DEPOSIT_ADMIN_ID;
|
||||
if (adminId) {
|
||||
const message = formatReconciliationReport(report);
|
||||
await sendMessage(env.BOT_TOKEN, parseInt(adminId), message).catch(err => {
|
||||
console.error('[Cron] 정합성 검증 알림 전송 실패:', err);
|
||||
});
|
||||
} else {
|
||||
console.warn('[Cron] DEPOSIT_ADMIN_ID 미설정 - 알림 전송 불가');
|
||||
}
|
||||
}
|
||||
|
||||
console.log(`[Cron] 정합성 검증 완료: ${report.totalUsers}명 검증, ${report.inconsistencies}건 불일치`);
|
||||
} catch (error) {
|
||||
console.error('[Cron] 정합성 검증 실패:', error);
|
||||
// 정합성 검증 실패가 전체 Cron을 중단시키지 않도록 에러를 catch만 하고 계속 진행
|
||||
}
|
||||
},
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user