Auto-create wallet on first balance check and show deposit info

- billing-agent: auto-create wallet if not exists, include deposit
  account info when balance is 0
- wallet-tool: auto-create wallet on balance check instead of error

Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
kappa
2026-02-12 09:33:03 +09:00
parent b9484908c7
commit 4b3cb21a15
2 changed files with 37 additions and 6 deletions

View File

@@ -122,7 +122,7 @@ export class BillingAgent extends BaseAgent<BillingSession> {
switch (walletArgs.action) { switch (walletArgs.action) {
case 'balance': case 'balance':
return this.handleBalance(userId, db); return this.handleBalance(userId, db, env);
case 'account': case 'account':
return this.handleAccount(env); return this.handleAccount(env);
case 'request': case 'request':
@@ -136,7 +136,7 @@ export class BillingAgent extends BaseAgent<BillingSession> {
} }
} }
private async handleBalance(userId: string, db: D1Database): Promise<string> { private async handleBalance(userId: string, db: D1Database, env: Env): Promise<string> {
try { try {
const wallet = await db.prepare( const wallet = await db.prepare(
`SELECT balance, currency, updated_at FROM wallets `SELECT balance, currency, updated_at FROM wallets
@@ -144,24 +144,47 @@ export class BillingAgent extends BaseAgent<BillingSession> {
).bind(userId).first(); ).bind(userId).first();
if (!wallet) { 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({ return JSON.stringify({
balance: 0, balance: 0,
currency: 'KRW', currency: 'KRW',
message: '지갑이 아직 생성되지 않았습니다.', message: '지갑이 생성되었습니다. 현재 잔액은 0원입니다.',
deposit_account: this.getDepositAccountInfo(env),
}); });
} }
return JSON.stringify({ const result: Record<string, unknown> = {
balance: wallet.balance, balance: wallet.balance,
currency: wallet.currency, currency: wallet.currency,
last_updated: wallet.updated_at, last_updated: wallet.updated_at,
}); };
// 잔액 0이면 입금 계좌 안내도 포함
if (wallet.balance === 0) {
result.deposit_account = this.getDepositAccountInfo(env);
}
return JSON.stringify(result);
} catch (error) { } catch (error) {
logger.error('잔액 조회 오류', error as Error, { userId }); logger.error('잔액 조회 오류', error as Error, { userId });
return JSON.stringify({ error: '잔액 정보를 조회할 수 없습니다.' }); 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 { private handleAccount(env: Env): string {
const bankName = env.DEPOSIT_BANK_NAME || '(설정 필요)'; const bankName = env.DEPOSIT_BANK_NAME || '(설정 필요)';
const bankAccount = env.DEPOSIT_BANK_ACCOUNT || '(설정 필요)'; const bankAccount = env.DEPOSIT_BANK_ACCOUNT || '(설정 필요)';

View File

@@ -82,7 +82,15 @@ async function getBalance(db?: D1Database, userId?: string): Promise<string> {
.first<{ balance: number; currency: string }>(); .first<{ balance: number; currency: string }>();
if (!wallet) { 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}`; return `현재 잔액: ${wallet.balance.toLocaleString()}${wallet.currency}`;