@@ -4,19 +4,26 @@ import { mainLogger } from '@server/sv_logger';
44import { UserService } from '../user/user.service' ;
55import { Broadcasts } from '@typings/Events' ;
66import { TransactionDB } from '../transaction/transaction.db' ;
7- import { Account } from '@server/../../typings/Account' ;
7+ import { Account , AccountType } from '@server/../../typings/Account' ;
88import { Cash } from '@server/../../typings/Cash' ;
9+ import { AccountService } from '../account/account.service' ;
910
1011const logger = mainLogger . child ( { module : 'broadcastService' } ) ;
1112
1213@singleton ( )
1314export class BroadcastService {
1415 _transactionDB : TransactionDB ;
1516 _userService : UserService ;
17+ _accountService : AccountService ;
1618
17- constructor ( transactionDB : TransactionDB , userService : UserService ) {
19+ constructor (
20+ transactionDB : TransactionDB ,
21+ userService : UserService ,
22+ accountService : AccountService ,
23+ ) {
1824 this . _transactionDB = transactionDB ;
1925 this . _userService = userService ;
26+ this . _accountService = accountService ;
2027 }
2128
2229 async broadcastUpdatedAccount ( account : Account ) {
@@ -64,6 +71,46 @@ export class BroadcastService {
6471 }
6572 }
6673
74+ async broadcastNewAccountBalance ( account : Account ) {
75+ logger . silly ( 'Broadcasting new balance for account ..' ) ;
76+
77+ const isShared = account . type === AccountType . Shared ;
78+ const identifier = account . ownerIdentifier ;
79+ const user = this . _userService . getUserByIdentifier ( identifier ) ;
80+ const onlineUsers = this . _userService . getAllUsers ( ) ;
81+
82+ if ( isShared ) {
83+ const users = await this . _accountService . getUsersFromShared ( {
84+ data : { accountId : account . id } ,
85+ source : 0 ,
86+ } ) ;
87+
88+ const identifiers = users . map ( ( user ) => user . userIdentifier ) ;
89+
90+ onlineUsers . forEach ( ( user ) => {
91+ if ( ! identifiers . includes ( user . getIdentifier ( ) ) ) {
92+ return ;
93+ }
94+
95+ emitNet ( Broadcasts . NewAccountBalance , user . getSource ( ) , account ) ;
96+
97+ logger . silly ( 'Broadcasted new balance for shared account:' ) ;
98+ logger . silly ( { identifier, source : user . getSource ( ) , balance : account . balance } ) ;
99+ } ) ;
100+ return ;
101+ }
102+
103+ if ( ! user ) {
104+ /* User is probably offline */
105+ return ;
106+ }
107+
108+ emitNet ( Broadcasts . NewAccountBalance , user ?. getSource ( ) , account ) ;
109+
110+ logger . silly ( 'Broadcasted new balance for personal account:' ) ;
111+ logger . silly ( { identifier, source : user . getSource ( ) , balance : account . balance } ) ;
112+ }
113+
67114 async broadcastNewDefaultAccountBalance ( account : Account ) {
68115 /* Do not broadcast updated values for none default account */
69116 if ( ! account . isDefault ) {
0 commit comments