Mohammed Foud
commited on
Commit
·
abc397d
1
Parent(s):
c721ef0
all
Browse files
src/bots/services/BalanceUpdateService.ts
CHANGED
|
@@ -39,12 +39,13 @@ export class BalanceUpdateService {
|
|
| 39 |
.on(
|
| 40 |
'postgres_changes',
|
| 41 |
{
|
| 42 |
-
event: '
|
| 43 |
schema: 'public',
|
| 44 |
-
table: 'users_bot_telegram'
|
|
|
|
| 45 |
},
|
| 46 |
async (payload: any) => {
|
| 47 |
-
logger.info('Received
|
| 48 |
try {
|
| 49 |
await this.handleBalanceUpdate(payload);
|
| 50 |
} catch (error) {
|
|
@@ -53,7 +54,7 @@ export class BalanceUpdateService {
|
|
| 53 |
}
|
| 54 |
)
|
| 55 |
.subscribe((status: string) => {
|
| 56 |
-
logger.info('
|
| 57 |
});
|
| 58 |
|
| 59 |
logger.info('Balance update subscription established');
|
|
@@ -63,50 +64,47 @@ export class BalanceUpdateService {
|
|
| 63 |
}
|
| 64 |
|
| 65 |
private async handleBalanceUpdate(payload: any) {
|
| 66 |
-
logger.info('Processing balance update payload:', payload);
|
| 67 |
|
| 68 |
if (!this.bot) {
|
| 69 |
logger.error('Bot instance not set in BalanceUpdateService');
|
| 70 |
return;
|
| 71 |
}
|
| 72 |
|
| 73 |
-
|
| 74 |
-
|
| 75 |
-
|
| 76 |
-
|
| 77 |
-
|
| 78 |
-
|
| 79 |
-
return;
|
| 80 |
-
}
|
| 81 |
|
| 82 |
-
|
| 83 |
-
|
| 84 |
-
|
| 85 |
-
|
| 86 |
-
|
| 87 |
-
|
| 88 |
-
|
| 89 |
-
|
| 90 |
-
|
| 91 |
-
|
| 92 |
-
|
| 93 |
-
|
| 94 |
-
|
| 95 |
-
|
| 96 |
-
|
| 97 |
-
|
| 98 |
-
|
| 99 |
-
|
| 100 |
-
|
| 101 |
-
|
| 102 |
-
|
| 103 |
-
|
| 104 |
-
|
| 105 |
-
|
| 106 |
-
}
|
| 107 |
-
} else {
|
| 108 |
-
logger.info(`No balance change detected for user ${telegramId}`);
|
| 109 |
}
|
|
|
|
|
|
|
| 110 |
}
|
| 111 |
}
|
| 112 |
|
|
|
|
| 39 |
.on(
|
| 40 |
'postgres_changes',
|
| 41 |
{
|
| 42 |
+
event: 'UPDATE',
|
| 43 |
schema: 'public',
|
| 44 |
+
table: 'users_bot_telegram',
|
| 45 |
+
filter: 'balance=neq.old.balance' // Only trigger when balance column changes
|
| 46 |
},
|
| 47 |
async (payload: any) => {
|
| 48 |
+
logger.info('Received balance change:', JSON.stringify(payload, null, 2));
|
| 49 |
try {
|
| 50 |
await this.handleBalanceUpdate(payload);
|
| 51 |
} catch (error) {
|
|
|
|
| 54 |
}
|
| 55 |
)
|
| 56 |
.subscribe((status: string) => {
|
| 57 |
+
logger.info('Balance subscription status:', status);
|
| 58 |
});
|
| 59 |
|
| 60 |
logger.info('Balance update subscription established');
|
|
|
|
| 64 |
}
|
| 65 |
|
| 66 |
private async handleBalanceUpdate(payload: any) {
|
| 67 |
+
logger.info('Processing balance update payload:', JSON.stringify(payload, null, 2));
|
| 68 |
|
| 69 |
if (!this.bot) {
|
| 70 |
logger.error('Bot instance not set in BalanceUpdateService');
|
| 71 |
return;
|
| 72 |
}
|
| 73 |
|
| 74 |
+
const { old: oldRecord, new: newRecord } = payload;
|
| 75 |
+
|
| 76 |
+
if (!oldRecord || !newRecord) {
|
| 77 |
+
logger.error('Invalid payload format:', payload);
|
| 78 |
+
return;
|
| 79 |
+
}
|
|
|
|
|
|
|
| 80 |
|
| 81 |
+
const telegramId = newRecord.telegram_id;
|
| 82 |
+
const oldBalance = parseFloat(oldRecord.balance);
|
| 83 |
+
const newBalance = parseFloat(newRecord.balance);
|
| 84 |
+
|
| 85 |
+
logger.info(`Balance change detected for user ${telegramId}: ${oldBalance} -> ${newBalance}`);
|
| 86 |
+
|
| 87 |
+
// Only send notification if balance actually changed
|
| 88 |
+
if (oldBalance !== newBalance) {
|
| 89 |
+
try {
|
| 90 |
+
const balanceChange = newBalance - oldBalance;
|
| 91 |
+
const changeType = balanceChange > 0 ? 'increase' : 'decrease';
|
| 92 |
+
|
| 93 |
+
const message = messageManager.getMessage('balance_update_notification')
|
| 94 |
+
.replace('{old_balance}', oldBalance.toString())
|
| 95 |
+
.replace('{new_balance}', newBalance.toString())
|
| 96 |
+
.replace('{change_type}', changeType)
|
| 97 |
+
.replace('{change_amount}', Math.abs(balanceChange).toString());
|
| 98 |
+
|
| 99 |
+
logger.info(`Sending balance update message to user ${telegramId}:`, message);
|
| 100 |
+
|
| 101 |
+
await this.bot.telegram.sendMessage(telegramId, message, { parse_mode: 'HTML' });
|
| 102 |
+
logger.info(`Balance update notification sent to user ${telegramId}`);
|
| 103 |
+
} catch (error) {
|
| 104 |
+
logger.error(`Failed to send balance update notification to user ${telegramId}:`, error);
|
|
|
|
|
|
|
|
|
|
| 105 |
}
|
| 106 |
+
} else {
|
| 107 |
+
logger.info(`No balance change detected for user ${telegramId}`);
|
| 108 |
}
|
| 109 |
}
|
| 110 |
|