-
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 remote-tracking branch 'upstream/develop' into dev-develop
Merge from upstream develop to local develop
- Loading branch information
Showing
9 changed files
with
374 additions
and
29 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
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
63 changes: 63 additions & 0 deletions
63
src/database/migrations/20240313130050-add-admin-permissions-for-related-orgs-apis.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,63 @@ | ||
'use strict' | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
module.exports = { | ||
async up(queryInterface, Sequelize) { | ||
try { | ||
const permissionsData = [ | ||
{ | ||
code: 'organization_data_update', | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/update/*', | ||
status: 'ACTIVE', | ||
}, | ||
{ | ||
code: 'organization_append_relatedOrg', | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/addRelatedOrg/*', | ||
status: 'ACTIVE', | ||
}, | ||
{ | ||
code: 'organization_remove_relatedOrg', | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/removeRelatedOrg/*', | ||
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 the batch insert | ||
await queryInterface.bulkDelete('permissions', { | ||
code: { | ||
[Sequelize.Op.in]: [ | ||
'organization_data_update', | ||
'organization_append_relatedOrg', | ||
'organization_remove_relatedOrg', | ||
], | ||
}, | ||
}) | ||
} catch (error) { | ||
console.error('Error rolling back migration:', error) | ||
throw error | ||
} | ||
}, | ||
} |
118 changes: 118 additions & 0 deletions
118
...base/migrations/20240313134927-add-admin-role-permission-mapping-for-related-orgs-apis.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,118 @@ | ||
'use strict' | ||
|
||
/** @type {import('sequelize-cli').Migration} */ | ||
|
||
require('module-alias/register') | ||
require('dotenv').config() | ||
const common = require('@constants/common') | ||
const Permissions = require('@database/models/index').Permission | ||
const rolePermission = require('@database/models/index').RolePermission | ||
|
||
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 = { | ||
async up(queryInterface, Sequelize) { | ||
try { | ||
const rolePermissionsData = await Promise.all([ | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
permission_id: await getPermissionId('organization', ['POST'], 'user/v1/organization/update/*'), | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/update/*', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
permission_id: await getPermissionId( | ||
'organization', | ||
['POST'], | ||
'user/v1/organization/addRelatedOrg/*' | ||
), | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/addRelatedOrg/*', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
permission_id: await getPermissionId( | ||
'organization', | ||
['POST'], | ||
'user/v1/organization/removeRelatedOrg/*' | ||
), | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/removeRelatedOrg/*', | ||
}, | ||
]) | ||
|
||
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 | ||
} | ||
}, | ||
|
||
async down(queryInterface, Sequelize) { | ||
try { | ||
// Array of objects representing data to be deleted | ||
const dataToDelete = [ | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/update/*', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/addRelatedOrg/*', | ||
}, | ||
{ | ||
role_title: common.ADMIN_ROLE, | ||
module: 'organization', | ||
request_type: ['POST'], | ||
api_path: 'user/v1/organization/removeRelatedOrg/*', | ||
}, | ||
] | ||
|
||
// Delete records based on each object's criteria | ||
for (const item of dataToDelete) { | ||
const permissionId = await getPermissionId(item.module, item.request_type, item.api_path) | ||
|
||
await queryInterface.bulkDelete('role_permission_mapping', { | ||
role_title: item.role_title, | ||
permission_id: permissionId, | ||
module: item.module, | ||
api_path: item.api_path, | ||
}) | ||
} | ||
} catch (error) { | ||
console.error('Error rolling back migration:', error) | ||
throw error | ||
} | ||
}, | ||
} |
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
Oops, something went wrong.