diff --git a/src/agents/billing-agent.ts b/src/agents/billing-agent.ts index 99f111f..10c6ee5 100644 --- a/src/agents/billing-agent.ts +++ b/src/agents/billing-agent.ts @@ -122,7 +122,7 @@ export class BillingAgent extends BaseAgent { switch (walletArgs.action) { case 'balance': - return this.handleBalance(userId, db); + return this.handleBalance(userId, db, env); case 'account': return this.handleAccount(env); case 'request': @@ -136,7 +136,7 @@ export class BillingAgent extends BaseAgent { } } - private async handleBalance(userId: string, db: D1Database): Promise { + private async handleBalance(userId: string, db: D1Database, env: Env): Promise { try { const wallet = await db.prepare( `SELECT balance, currency, updated_at FROM wallets @@ -144,24 +144,47 @@ export class BillingAgent extends BaseAgent { ).bind(userId).first(); if (!wallet) { + // Auto-create wallet + await db.prepare( + `INSERT INTO wallets (user_id, balance, currency) + VALUES ((SELECT id FROM users WHERE telegram_id = ?), 0, 'KRW') + ON CONFLICT (user_id) DO NOTHING` + ).bind(userId).run(); + return JSON.stringify({ balance: 0, currency: 'KRW', - message: '지갑이 아직 생성되지 않았습니다.', + message: '지갑이 생성되었습니다. 현재 잔액은 0원입니다.', + deposit_account: this.getDepositAccountInfo(env), }); } - return JSON.stringify({ + const result: Record = { balance: wallet.balance, currency: wallet.currency, last_updated: wallet.updated_at, - }); + }; + + // 잔액 0이면 입금 계좌 안내도 포함 + if (wallet.balance === 0) { + result.deposit_account = this.getDepositAccountInfo(env); + } + + return JSON.stringify(result); } catch (error) { logger.error('잔액 조회 오류', error as Error, { userId }); return JSON.stringify({ error: '잔액 정보를 조회할 수 없습니다.' }); } } + private getDepositAccountInfo(env: Env) { + return { + bank_name: env.DEPOSIT_BANK_NAME || '(설정 필요)', + account_number: env.DEPOSIT_BANK_ACCOUNT || '(설정 필요)', + account_holder: env.DEPOSIT_BANK_HOLDER || '(설정 필요)', + }; + } + private handleAccount(env: Env): string { const bankName = env.DEPOSIT_BANK_NAME || '(설정 필요)'; const bankAccount = env.DEPOSIT_BANK_ACCOUNT || '(설정 필요)'; diff --git a/src/tools/wallet-tool.ts b/src/tools/wallet-tool.ts index 513aac4..c1a6c0a 100644 --- a/src/tools/wallet-tool.ts +++ b/src/tools/wallet-tool.ts @@ -82,7 +82,15 @@ async function getBalance(db?: D1Database, userId?: string): Promise { .first<{ balance: number; currency: string }>(); if (!wallet) { - return '예치금 계정이 없습니다. 입금 요청을 하시면 자동으로 생성됩니다.'; + await db + .prepare( + `INSERT INTO wallets (user_id, balance, currency) + VALUES (?, 0, 'KRW') + ON CONFLICT (user_id) DO NOTHING` + ) + .bind(user.id) + .run(); + return '지갑이 생성되었습니다. 현재 잔액: 0원(KRW)'; } return `현재 잔액: ${wallet.balance.toLocaleString()}${wallet.currency}`;