Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

changes added to keep current session on top of first page #482

Merged
merged 2 commits into from
Apr 4, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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