diff --git a/src/index.ts b/src/index.ts
index 12b5746..85b8dab 100644
--- a/src/index.ts
+++ b/src/index.ts
@@ -106,22 +106,21 @@ Documentation: https://github.com/your-repo
// 매칭 성공 시 사용자에게 알림
if (matched && env.BOT_TOKEN) {
- const user = await env.DB.prepare(
- 'SELECT telegram_id FROM users WHERE id = ?'
- ).bind(matched.userId).first<{ telegram_id: string }>();
-
- if (user) {
- // 업데이트된 잔액 조회
- const deposit = await env.DB.prepare(
- 'SELECT balance FROM user_deposits WHERE user_id = ?'
- ).bind(matched.userId).first<{ balance: number }>();
+ // 병렬화: JOIN으로 단일 쿼리 (1회 네트워크 왕복)
+ const result = await env.DB.prepare(
+ `SELECT u.telegram_id, COALESCE(d.balance, 0) as balance
+ FROM users u
+ LEFT JOIN user_deposits d ON u.id = d.user_id
+ WHERE u.id = ?`
+ ).bind(matched.userId).first<{ telegram_id: string; balance: number }>();
+ if (result) {
await sendMessage(
env.BOT_TOKEN,
- parseInt(user.telegram_id),
+ parseInt(result.telegram_id),
`✅ 입금 확인 완료!\n\n` +
`입금액: ${matched.amount.toLocaleString()}원\n` +
- `현재 잔액: ${(deposit?.balance || 0).toLocaleString()}원\n\n` +
+ `현재 잔액: ${result.balance.toLocaleString()}원\n\n` +
`감사합니다! 🎉`
);
}
@@ -177,27 +176,34 @@ Documentation: https://github.com/your-repo
console.log(`[Cron] 만료된 거래 ${expiredTxs.results.length}건 발견`);
- for (const tx of expiredTxs.results) {
- // 상태를 cancelled로 변경
- await env.DB.prepare(
- "UPDATE deposit_transactions SET status = 'cancelled', description = '입금 대기 만료 (24시간)' WHERE id = ?"
- ).bind(tx.id).run();
+ // 단일 UPDATE 쿼리로 일괄 처리
+ const ids = expiredTxs.results.map(tx => tx.id);
+ await env.DB.prepare(
+ `UPDATE deposit_transactions
+ SET status = 'cancelled', description = '입금 대기 만료 (24시간)'
+ WHERE id IN (${ids.map(() => '?').join(',')})`
+ ).bind(...ids).run();
- // 사용자에게 알림
- await sendMessage(
+ console.log(`[Cron] UPDATE 완료: ${ids.length}건`);
+
+ // 알림 병렬 처리 (개별 실패가 전체를 중단시키지 않도록 .catch() 추가)
+ const notificationPromises = expiredTxs.results.map(tx =>
+ sendMessage(
env.BOT_TOKEN,
parseInt(tx.telegram_id),
- `⏰ 입금 대기 만료\n\n` +
- `입금자: ${tx.depositor_name}\n` +
- `금액: ${tx.amount.toLocaleString()}원\n\n` +
- `24시간 이내에 입금이 확인되지 않아 자동 취소되었습니다.\n` +
- `다시 입금하시려면 입금 후 알려주세요.`
- );
+ `⏰ 입금 대기 자동 취소\n\n` +
+ `거래 #${tx.id}이 24시간 내 확인되지 않아 자동 취소되었습니다.\n` +
+ `• 입금액: ${tx.amount.toLocaleString()}원\n` +
+ `• 입금자: ${tx.depositor_name}\n\n` +
+ `실제 입금하셨다면 다시 신고해주세요.`
+ ).catch(err => {
+ console.error(`[Cron] 알림 전송 실패 (거래 #${tx.id}, 사용자 ${tx.telegram_id}):`, err);
+ return null; // 실패한 알림은 null로 처리
+ })
+ );
- console.log(`[Cron] 거래 #${tx.id} 만료 처리 완료`);
- }
-
- console.log('[Cron] 만료된 입금 대기 정리 완료');
+ await Promise.all(notificationPromises);
+ console.log(`[Cron] ${expiredTxs.results.length}건 만료 처리 완료 (알림 전송 완료)`);
} catch (error) {
console.error('[Cron] 오류:', error);
}