Skip to content

Commit

Permalink
Merge pull request #482 from ELEVATE-Project/1310_login_activity_expiry
Browse files Browse the repository at this point in the history
changes added to keep current session on top of first page
  • Loading branch information
rakeshSgr authored Apr 4, 2024
2 parents 504b68a + ef218ce commit 7c658ea
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 7 deletions.
4 changes: 3 additions & 1 deletion src/controllers/v1/account.js
Original file line number Diff line number Diff line change
Expand Up @@ -284,7 +284,9 @@ module.exports = class Account {
req.decodedToken.id,
filter,
req.pageSize,
req.pageNo
req.pageNo,
req.decodedToken.session_id,
req.query && req.query.period ? req.query.period : ''
)
return userSessionDetails
} catch (error) {
Expand Down
33 changes: 27 additions & 6 deletions src/services/user-sessions.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ const httpStatusCode = require('@generics/http-status')
const responses = require('@helpers/responses')
const common = require('@constants/common')
const jwt = require('jsonwebtoken')
const moment = require('moment')
const { Op } = require('sequelize')

// create user-session
module.exports = class UserSessionsHelper {
Expand Down Expand Up @@ -84,10 +86,11 @@ module.exports = class UserSessionsHelper {
* @param {string} status - The status of the user sessions (e.g., 'ACTIVE', '').
* @param {number} limit - The maximum number of user sessions to retrieve per page.
* @param {number} page - The page number for pagination.
* @param {number} currentSessionId - The id of current session.
* @returns {Promise<Object>} - A promise that resolves to the user session details.
*/

static async list(userId, status, limit, page) {
static async list(userId, status, limit, page, currentSessionId, period = '') {
try {
const filter = {
user_id: userId,
Expand All @@ -99,10 +102,19 @@ module.exports = class UserSessionsHelper {
filter.ended_at = null
}

// If front end passes a period
if (period != '') {
const periodInSeconds = await utilsHelper.convertDurationToSeconds(period)
const currentTimeEpoch = moment().unix()
const threshold = currentTimeEpoch - periodInSeconds
filter.started_at = { [Op.gte]: threshold }
}

// create userSession
const userSessions = await userSessionsQueries.findAll(filter)
const activeSessions = []
const inActiveSessions = []
const currentSession = []
for (const session of userSessions) {
const id = session.id.toString() // Convert ID to string
const redisData = await utilsHelper.redisGet(id)
Expand All @@ -127,7 +139,11 @@ module.exports = class UserSessionsHelper {
login_time: session.started_at,
logout_time: session.ended_at,
}
activeSessions.push(responseObj)
if (responseObj.id == currentSessionId) {
currentSession.push(responseObj)
} else {
activeSessions.push(responseObj)
}
} else if (status === '') {
const responseObj = {
id: session.id,
Expand All @@ -136,13 +152,18 @@ module.exports = class UserSessionsHelper {
login_time: session.started_at,
logout_time: session.ended_at,
}
responseObj.status === common.ACTIVE_STATUS
? activeSessions.push(responseObj)
: inActiveSessions.push(responseObj)
// get current session data
if (responseObj.id == currentSessionId) {
currentSession.push(responseObj)
} else {
responseObj.status === common.ACTIVE_STATUS
? activeSessions.push(responseObj)
: inActiveSessions.push(responseObj)
}
}
}

const result = [...activeSessions, ...inActiveSessions]
const result = [...currentSession, ...activeSessions, ...inActiveSessions]

// Paginate the result array
// The response is accumulated from two places. db and redis. So pagination is not possible on the fly
Expand Down

0 comments on commit 7c658ea

Please sign in to comment.