Mohammed Foud commited on
Commit
abc397d
·
1 Parent(s): c721ef0
src/bots/services/BalanceUpdateService.ts CHANGED
@@ -39,12 +39,13 @@ export class BalanceUpdateService {
39
  .on(
40
  'postgres_changes',
41
  {
42
- event: '*', // Listen to all events (INSERT, UPDATE, DELETE)
43
  schema: 'public',
44
- table: 'users_bot_telegram'
 
45
  },
46
  async (payload: any) => {
47
- logger.info('Received database change:', payload);
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('Subscription status:', status);
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
- // Handle different event types
74
- if (payload.eventType === 'UPDATE') {
75
- const { old: oldRecord, new: newRecord } = payload;
76
-
77
- if (!oldRecord || !newRecord) {
78
- logger.error('Invalid payload format:', payload);
79
- return;
80
- }
81
 
82
- const telegramId = newRecord.telegram_id;
83
- const oldBalance = parseFloat(oldRecord.balance);
84
- const newBalance = parseFloat(newRecord.balance);
85
-
86
- logger.info(`Balance change detected for user ${telegramId}: ${oldBalance} -> ${newBalance}`);
87
-
88
- // Only send notification if balance actually changed
89
- if (oldBalance !== newBalance) {
90
- try {
91
- const balanceChange = newBalance - oldBalance;
92
- const changeType = balanceChange > 0 ? 'increase' : 'decrease';
93
-
94
- const message = messageManager.getMessage('balance_update_notification')
95
- .replace('{old_balance}', oldBalance.toString())
96
- .replace('{new_balance}', newBalance.toString())
97
- .replace('{change_type}', changeType)
98
- .replace('{change_amount}', Math.abs(balanceChange).toString());
99
-
100
- logger.info(`Sending balance update message to user ${telegramId}:`, message);
101
-
102
- await this.bot.telegram.sendMessage(telegramId, message, { parse_mode: 'HTML' });
103
- logger.info(`Balance update notification sent to user ${telegramId}`);
104
- } catch (error) {
105
- logger.error(`Failed to send balance update notification to user ${telegramId}:`, error);
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