-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #473 from ELEVATE-Project/1172_user_sessions_backu…
…p_one [1171- STORY ]- User Session Implementation added.
- Loading branch information
Showing
19 changed files
with
1,077 additions
and
113 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,4 @@ | ||
FROM node:16 | ||
FROM node:20 | ||
|
||
#Set working directory | ||
WORKDIR /var/src/ | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
54 changes: 54 additions & 0 deletions
54
src/database/migrations/20240326110128-create-user-sessions-table.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
'use strict' | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
await queryInterface.createTable('user_sessions', { | ||
id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
primaryKey: true, | ||
autoIncrement: true, | ||
}, | ||
user_id: { | ||
type: Sequelize.INTEGER, | ||
allowNull: false, | ||
}, | ||
started_at: { | ||
type: Sequelize.BIGINT, | ||
allowNull: false, | ||
}, | ||
ended_at: { | ||
type: Sequelize.BIGINT, | ||
allowNull: true, | ||
}, | ||
token: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
device_info: { | ||
type: Sequelize.JSONB, | ||
allowNull: true, | ||
}, | ||
refresh_token: { | ||
type: Sequelize.TEXT, | ||
allowNull: true, | ||
}, | ||
created_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
updated_at: { | ||
allowNull: false, | ||
type: Sequelize.DATE, | ||
}, | ||
deleted_at: { | ||
type: Sequelize.DATE, | ||
}, | ||
}) | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
await queryInterface.dropTable('user_sessions') | ||
}, | ||
} |
48 changes: 48 additions & 0 deletions
48
src/database/migrations/20240328084048-update-session-permissions.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
'use strict' | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
try { | ||
const permissionsData = [ | ||
{ | ||
code: 'get_user_sessions', | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
status: 'ACTIVE', | ||
}, | ||
{ | ||
code: 'validate_user_sessions', | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
status: 'ACTIVE', | ||
}, | ||
] | ||
|
||
// Batch insert permissions | ||
await queryInterface.bulkInsert( | ||
'permissions', | ||
permissionsData.map((permission) => ({ | ||
...permission, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
})) | ||
) | ||
} catch (error) { | ||
console.error('Error in migration:', error) | ||
throw error | ||
} | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
try { | ||
// Rollback migration by deleting all permissions | ||
await queryInterface.bulkDelete('permissions', null, {}) | ||
} catch (error) { | ||
console.error('Error in rollback migration:', error) | ||
throw error | ||
} | ||
}, | ||
} |
139 changes: 139 additions & 0 deletions
139
src/database/migrations/20240328084246-update-session-role-permission.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
'use strict' | ||
|
||
require('module-alias/register') | ||
require('dotenv').config() | ||
const common = require('@constants/common') | ||
const Permissions = require('@database/models/index').Permission | ||
|
||
const getPermissionId = async (module, request_type, api_path) => { | ||
try { | ||
const permission = await Permissions.findOne({ | ||
where: { module, request_type, api_path }, | ||
}) | ||
if (!permission) { | ||
throw new Error( | ||
`Permission not found for module: ${module}, request_type: ${request_type}, api_path: ${api_path}` | ||
) | ||
} | ||
return permission.id | ||
} catch (error) { | ||
throw new Error(`Error while fetching permission: ${error.message}`) | ||
} | ||
} | ||
|
||
module.exports = { | ||
up: async (queryInterface, Sequelize) => { | ||
try { | ||
const rolePermissionsData = await Promise.all([ | ||
{ | ||
role_title: common.MENTOR_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
{ | ||
role_title: common.ORG_ADMIN_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
{ | ||
role_title: common.USER_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
{ | ||
role_title: common.SESSION_MANAGER_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
{ | ||
role_title: common.MENTEE_ROLE, | ||
permission_id: await getPermissionId('account', ['GET'], '/user/v1/account/sessions'), | ||
module: 'account', | ||
request_type: ['GET'], | ||
api_path: '/user/v1/account/sessions', | ||
}, | ||
|
||
{ | ||
role_title: common.MENTOR_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
{ | ||
role_title: common.MENTEE_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
{ | ||
role_title: common.ORG_ADMIN_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
{ | ||
role_title: common.USER_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
{ | ||
role_title: common.SESSION_MANAGER_ROLE, | ||
permission_id: await getPermissionId('account', ['POST'], '/user/v1/account/validateUserSession'), | ||
module: 'account', | ||
request_type: ['POST'], | ||
api_path: '/user/v1/account/validateUserSession', | ||
}, | ||
]) | ||
|
||
await queryInterface.bulkInsert( | ||
'role_permission_mapping', | ||
rolePermissionsData.map((data) => ({ | ||
...data, | ||
created_at: new Date(), | ||
updated_at: new Date(), | ||
created_by: 0, | ||
})) | ||
) | ||
} catch (error) { | ||
console.log(error) | ||
console.error(`Migration error: ${error.message}`) | ||
throw error | ||
} | ||
}, | ||
|
||
down: async (queryInterface, Sequelize) => { | ||
try { | ||
await queryInterface.bulkDelete('role_permission_mapping', null, {}) | ||
} catch (error) { | ||
console.error(`Rollback migration error: ${error.message}`) | ||
throw error | ||
} | ||
}, | ||
} |
Oops, something went wrong.