@@ -10,9 +10,10 @@ import { CONSTANTS } from "../../../constants/constants.js"
1010*/
1111
1212class SessionService {
13- constructor ( activeSessions , config , redisConnection ) {
13+ constructor ( activeSessions , config , logger , redisConnection ) {
1414 this . activeSessions = activeSessions
1515 this . config = config
16+ this . logger = logger
1617 this . redisConnection = redisConnection
1718 }
1819
@@ -21,21 +22,25 @@ class SessionService {
2122 }
2223
2324 addUserDeviceConnection ( socket , organizationId , userId , deviceId ) {
24- const activeConnections = this . activeSessions . DEVICES [ userId ]
2525 const socketsToClose = [ ]
2626
27+ let activeConnections = this . getUserDevices ( userId )
28+ const filterNotSameSocket = activeConnections . filter ( connection => connection . socket !== socket )
29+ this . activeSessions . DEVICES [ userId ] = filterNotSameSocket
30+ activeConnections = this . getUserDevices ( userId )
31+
2732 const connection = { socket : socket , deviceId, organizationId }
2833
2934 if ( activeConnections ) {
30- const devices = activeConnections . filter ( ( connection ) => {
35+ const otherDeviceConnections = activeConnections . filter ( ( connection ) => {
3136 if ( connection . deviceId !== deviceId ) {
3237 return true
3338 } else {
3439 socketsToClose . push ( connection . socket )
3540 return false
3641 }
3742 } )
38- this . activeSessions . DEVICES [ userId ] = [ ...devices , connection ]
43+ this . activeSessions . DEVICES [ userId ] = [ ...otherDeviceConnections , connection ]
3944 } else {
4045 this . activeSessions . DEVICES [ userId ] = [ connection ]
4146 }
@@ -102,9 +107,7 @@ class SessionService {
102107
103108 async listUserDevice ( organizationId , userId ) {
104109 if ( this . config . get ( "app.isStandAloneNode" ) ) {
105- return this . getUserDevices ( userId )
106- . map ( ( connection ) => connection ?. deviceId )
107- . filter ( ( deviceId ) => deviceId !== CONSTANTS . HTTP_DEVICE_ID )
110+ return this . listUserDeviceLocal ( userId )
108111 }
109112
110113 const userKey = this . #usersSetCacheKey( organizationId , userId )
@@ -113,6 +116,12 @@ class SessionService {
113116 return deviceIds ?? [ ]
114117 }
115118
119+ listUserDeviceLocal ( userId ) {
120+ return this . getUserDevices ( userId )
121+ . map ( ( connection ) => connection ?. deviceId )
122+ . filter ( ( deviceId ) => deviceId !== CONSTANTS . HTTP_DEVICE_ID )
123+ }
124+
116125 async deleteUserDevices ( organizationId , userId ) {
117126 const userKey = this . #usersSetCacheKey( organizationId , userId )
118127
@@ -312,10 +321,21 @@ class SessionService {
312321 }
313322
314323 async removeUserSession ( socket , userId , deviceId ) {
324+ this . logger . debug ( "[removeUserSession][args]: %o" , { socket : socket ?. isAlive , userId, deviceId } )
325+
315326 userId = userId ?? this . getSessionUserId ( socket )
316327 deviceId = deviceId ?? this . getDeviceId ( socket , userId )
317328 const orgId = this . getSession ( socket ) ?. organizationId
318329
330+ this . logger . debug ( "[removeUserSession][vars]: %o [session]: %o [device]: %s" , { orgId, userId, deviceId } , this . getSession ( socket ) , this . getDeviceId ( socket , userId ) )
331+
332+ const devicesBefore = this . getUserDevices ( userId ) . map ( ( connection ) => {
333+ const { socket, ...connectionData } = connection
334+ return { ...connectionData , socket : socket ?. clientId }
335+ } )
336+
337+ this . logger . debug ( "[removeUserSession][devices][before]: %o %s" , devicesBefore , devicesBefore ?. length )
338+
319339 const leftActiveConnections = this . getUserDevices ( userId ) . filter ( ( { deviceId : activeDeviceId } ) => activeDeviceId !== deviceId )
320340
321341 if ( leftActiveConnections ?. length ) {
@@ -325,6 +345,14 @@ class SessionService {
325345 }
326346 this . activeSessions . SESSIONS . delete ( socket )
327347
348+
349+ const devicesAfter = this . getUserDevices ( userId ) . map ( ( connection ) => {
350+ const { socket, ...connectionData } = connection
351+ return { ...connectionData , socket : socket ?. clientId }
352+ } )
353+
354+ this . logger . debug ( "[removeUserSession][devices][after]: %o %s" , devicesAfter , devicesAfter ?. length )
355+
328356 if ( ! deviceId ) {
329357 return
330358 }
@@ -393,7 +421,7 @@ class SessionService {
393421 ( session ) =>
394422 session ?. organizationId === organizationId &&
395423 session ?. extraParams [ CONSTANTS . SESSION_DEVICE_ID_KEY ] !== CONSTANTS . HTTP_DEVICE_ID &&
396- session ?. userId
424+ session ?. userId && this . listUserDeviceLocal ( session ?. userId ) ?. length
397425 )
398426 . map ( ( session ) => session . userId )
399427 . sort ( ( userIdA , userIdB ) => userIdA - userIdB )
0 commit comments