Skip to content

Commit 3ebb56e

Browse files
Merge remote-tracking branch 'upstream/develop' into dev-develop
Merge from upstream develop to local develop
2 parents 9c89e62 + ba97360 commit 3ebb56e

File tree

9 files changed

+374
-29
lines changed

9 files changed

+374
-29
lines changed

src/.env.sample

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -164,3 +164,6 @@ EVENT_ORG_LISTENER_URLS='http://interface:3567/mentoring/v1/organization/eventLi
164164
EVENT_ENABLE_ORG_EVENTS=true
165165
#Generic Email template for new users
166166
GENERIC_INVITATION_EMAIL_TEMPLATE_CODE=generic_invite
167+
168+
# Allowed host by CORS
169+
ALLOWED_HOST = "http://examplDomain.com"

src/app.js

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,12 @@ app.use(bodyParser.json({ limit: '50MB' }))
6565

6666
app.use(express.static('public'))
6767

68+
// Middleware to set Access-Control-Allow-Origin header
69+
app.use((req, res, next) => {
70+
res.setHeader('Access-Control-Allow-Origin', process.env.ALLOWED_HOST)
71+
next()
72+
})
73+
6874
/* Logs request info if environment is configured to enable log */
6975
app.all('*', (req, res, next) => {
7076
logger.info('***User Service Request Log***', {

src/controllers/v1/organization.js

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -160,4 +160,27 @@ module.exports = class Organization {
160160
return error
161161
}
162162
}
163+
164+
async addRelatedOrg(req) {
165+
try {
166+
const result = await orgService.addRelatedOrg(
167+
req.params.id ? req.params.id : '',
168+
req.body.related_orgs ? req.body.related_orgs : []
169+
)
170+
return result
171+
} catch (error) {
172+
return error
173+
}
174+
}
175+
async removeRelatedOrg(req) {
176+
try {
177+
const result = await orgService.removeRelatedOrg(
178+
req.params.id ? req.params.id : '',
179+
req.body.related_orgs ? req.body.related_orgs : []
180+
)
181+
return result
182+
} catch (error) {
183+
return error
184+
}
185+
}
163186
}
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
'use strict'
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
module.exports = {
5+
async up(queryInterface, Sequelize) {
6+
try {
7+
const permissionsData = [
8+
{
9+
code: 'organization_data_update',
10+
module: 'organization',
11+
request_type: ['POST'],
12+
api_path: 'user/v1/organization/update/*',
13+
status: 'ACTIVE',
14+
},
15+
{
16+
code: 'organization_append_relatedOrg',
17+
module: 'organization',
18+
request_type: ['POST'],
19+
api_path: 'user/v1/organization/addRelatedOrg/*',
20+
status: 'ACTIVE',
21+
},
22+
{
23+
code: 'organization_remove_relatedOrg',
24+
module: 'organization',
25+
request_type: ['POST'],
26+
api_path: 'user/v1/organization/removeRelatedOrg/*',
27+
status: 'ACTIVE',
28+
},
29+
]
30+
31+
// Batch insert permissions
32+
await queryInterface.bulkInsert(
33+
'permissions',
34+
permissionsData.map((permission) => ({
35+
...permission,
36+
created_at: new Date(),
37+
updated_at: new Date(),
38+
}))
39+
)
40+
} catch (error) {
41+
console.error('Error in migration:', error)
42+
throw error
43+
}
44+
},
45+
46+
async down(queryInterface, Sequelize) {
47+
try {
48+
// Rollback the batch insert
49+
await queryInterface.bulkDelete('permissions', {
50+
code: {
51+
[Sequelize.Op.in]: [
52+
'organization_data_update',
53+
'organization_append_relatedOrg',
54+
'organization_remove_relatedOrg',
55+
],
56+
},
57+
})
58+
} catch (error) {
59+
console.error('Error rolling back migration:', error)
60+
throw error
61+
}
62+
},
63+
}
Lines changed: 118 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,118 @@
1+
'use strict'
2+
3+
/** @type {import('sequelize-cli').Migration} */
4+
5+
require('module-alias/register')
6+
require('dotenv').config()
7+
const common = require('@constants/common')
8+
const Permissions = require('@database/models/index').Permission
9+
const rolePermission = require('@database/models/index').RolePermission
10+
11+
const getPermissionId = async (module, request_type, api_path) => {
12+
try {
13+
const permission = await Permissions.findOne({
14+
where: { module, request_type, api_path },
15+
})
16+
if (!permission) {
17+
throw new Error(
18+
`Permission not found for module: ${module}, request_type: ${request_type}, api_path: ${api_path}`
19+
)
20+
}
21+
return permission.id
22+
} catch (error) {
23+
throw new Error(`Error while fetching permission: ${error.message}`)
24+
}
25+
}
26+
27+
module.exports = {
28+
async up(queryInterface, Sequelize) {
29+
try {
30+
const rolePermissionsData = await Promise.all([
31+
{
32+
role_title: common.ADMIN_ROLE,
33+
permission_id: await getPermissionId('organization', ['POST'], 'user/v1/organization/update/*'),
34+
module: 'organization',
35+
request_type: ['POST'],
36+
api_path: 'user/v1/organization/update/*',
37+
},
38+
{
39+
role_title: common.ADMIN_ROLE,
40+
permission_id: await getPermissionId(
41+
'organization',
42+
['POST'],
43+
'user/v1/organization/addRelatedOrg/*'
44+
),
45+
module: 'organization',
46+
request_type: ['POST'],
47+
api_path: 'user/v1/organization/addRelatedOrg/*',
48+
},
49+
{
50+
role_title: common.ADMIN_ROLE,
51+
permission_id: await getPermissionId(
52+
'organization',
53+
['POST'],
54+
'user/v1/organization/removeRelatedOrg/*'
55+
),
56+
module: 'organization',
57+
request_type: ['POST'],
58+
api_path: 'user/v1/organization/removeRelatedOrg/*',
59+
},
60+
])
61+
62+
await queryInterface.bulkInsert(
63+
'role_permission_mapping',
64+
rolePermissionsData.map((data) => ({
65+
...data,
66+
created_at: new Date(),
67+
updated_at: new Date(),
68+
created_by: 0,
69+
}))
70+
)
71+
} catch (error) {
72+
console.log(error)
73+
console.error(`Migration error: ${error.message}`)
74+
throw error
75+
}
76+
},
77+
78+
async down(queryInterface, Sequelize) {
79+
try {
80+
// Array of objects representing data to be deleted
81+
const dataToDelete = [
82+
{
83+
role_title: common.ADMIN_ROLE,
84+
module: 'organization',
85+
request_type: ['POST'],
86+
api_path: 'user/v1/organization/update/*',
87+
},
88+
{
89+
role_title: common.ADMIN_ROLE,
90+
module: 'organization',
91+
request_type: ['POST'],
92+
api_path: 'user/v1/organization/addRelatedOrg/*',
93+
},
94+
{
95+
role_title: common.ADMIN_ROLE,
96+
module: 'organization',
97+
request_type: ['POST'],
98+
api_path: 'user/v1/organization/removeRelatedOrg/*',
99+
},
100+
]
101+
102+
// Delete records based on each object's criteria
103+
for (const item of dataToDelete) {
104+
const permissionId = await getPermissionId(item.module, item.request_type, item.api_path)
105+
106+
await queryInterface.bulkDelete('role_permission_mapping', {
107+
role_title: item.role_title,
108+
permission_id: permissionId,
109+
module: item.module,
110+
api_path: item.api_path,
111+
})
112+
}
113+
} catch (error) {
114+
console.error('Error rolling back migration:', error)
115+
throw error
116+
}
117+
},
118+
}

src/envVariables.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -232,6 +232,11 @@ let enviromentVariables = {
232232
optional: true,
233233
default: 'generic_invite',
234234
},
235+
ALLOWED_HOST: {
236+
message: 'Required CORS allowed host',
237+
optional: true,
238+
default: '*',
239+
},
235240
}
236241

237242
let success = true

src/locales/en.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,5 +112,6 @@
112112
"ROLE_NOT_DELETED": "Roles not deleted",
113113
"ROLES_HAS_EMPTY_LIST": "Empty roles list",
114114
"COLUMN_DOES_NOT_EXISTS": "Role column does not exists",
115-
"PERMISSION_DENIED": "You do not have the required permissions to access this resource. Please contact your administrator for assistance."
115+
"PERMISSION_DENIED": "You do not have the required permissions to access this resource. Please contact your administrator for assistance.",
116+
"RELATED_ORG_REMOVAL_FAILED": "Requested organization not related the organization. Please check the values."
116117
}

src/services/account.js

Lines changed: 23 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -895,8 +895,30 @@ module.exports = class AccountHelper {
895895
delete user.password
896896
delete user.otpInfo
897897

898+
let defaultOrg = await organizationQueries.findOne(
899+
{ code: process.env.DEFAULT_ORGANISATION_CODE },
900+
{ attributes: ['id'] }
901+
)
902+
let defaultOrgId = defaultOrg.id
903+
const modelName = await userQueries.getModelName()
904+
905+
let validationData = await entityTypeQueries.findUserEntityTypesAndEntities({
906+
status: 'ACTIVE',
907+
organization_id: {
908+
[Op.in]: [user.organization_id, defaultOrgId],
909+
},
910+
model_names: { [Op.contains]: [modelName] },
911+
})
912+
913+
const prunedEntities = removeDefaultOrgEntityTypes(validationData, user.organization_id)
914+
user = utils.processDbResponse(user, prunedEntities)
915+
898916
// Check if user and user.image exist, then fetch a downloadable URL for the image
899-
if (user && user.image) user.image = await utils.getDownloadableUrl(user.image)
917+
if (user && user.image) {
918+
user.image = await utils.getDownloadableUrl(user.image)
919+
}
920+
user.email = plaintextEmailId
921+
900922
const result = { access_token: accessToken, refresh_token: refreshToken, user }
901923
return responses.successResponse({
902924
statusCode: httpStatusCode.ok,

0 commit comments

Comments
 (0)