diff --git a/src/services/mentees.js b/src/services/mentees.js index 4eff0f51e..424a9dbd0 100644 --- a/src/services/mentees.js +++ b/src/services/mentees.js @@ -893,16 +893,16 @@ module.exports = class MenteesHelper { } } - static async getOrganizationIdBasedOnPolicy(userId, organization_id, filter_type) { + static async getOrganizationIdBasedOnPolicy(userId, organization_id, filterType) { try { - let organization_ids = [] - + let organizationIds = [] + filterType = filterType.toLowerCase() const attributes = - filter_type.toLowerCase() == common.MENTEE_ROLE + filterType == common.MENTEE_ROLE ? ['organization_id', 'session_visibility_policy'] : ['organization_id', 'external_mentor_visibility_policy'] - const orgPolicies = await organisationExtensionQueries.findOne( + const orgExtension = await organisationExtensionQueries.findOne( { organization_id }, { attributes: attributes, @@ -910,39 +910,98 @@ module.exports = class MenteesHelper { ) const visibilityPolicy = - filter_type.toLowerCase() == common.MENTEE_ROLE - ? orgPolicies.session_visibility_policy - : orgPolicies.external_mentor_visibility_policy + filterType == common.MENTEE_ROLE + ? orgExtension.session_visibility_policy + : orgExtension.external_mentor_visibility_policy - if (orgPolicies?.organization_id) { + if (orgExtension?.organization_id) { if (visibilityPolicy === common.CURRENT) { - organization_ids.push(orgPolicies.organization_id) + organizationIds.push(orgExtension.organization_id) } else if (visibilityPolicy === common.ASSOCIATED || visibilityPolicy === common.ALL) { - organization_ids.push(orgPolicies.organization_id) + organizationIds.push(orgExtension.organization_id) let relatedOrgs = [] - let userOrgDetails = await userRequests.fetchDefaultOrgDetails(orgPolicies.organization_id) + let userOrgDetails = await userRequests.fetchDefaultOrgDetails(orgExtension.organization_id) if (userOrgDetails.success && userOrgDetails.data?.result?.related_orgs?.length > 0) { relatedOrgs = userOrgDetails.data.result.related_orgs } if (visibilityPolicy === common.ASSOCIATED) { - organization_ids.push(...relatedOrgs) + const associatedAdditionalFilter = + filterType == common.MENTEE_ROLE + ? { + external_session_visibility_policy: { + [Op.ne]: 'CURRENT', + }, + } + : { + mentor_visibility_policy: { + [Op.ne]: 'CURRENT', + }, + } + + const organizationExtension = await organisationExtensionQueries.findAll( + { + [Op.and]: [ + { + organization_id: { + [Op.in]: [...relatedOrgs], + }, + }, + associatedAdditionalFilter, + ], + }, + { + attributes: ['organization_id'], + } + ) + organizationIds.push(orgExtension.organization_id) + if (organizationExtension) { + const organizationIdsFromOrgExtension = organizationExtension.map( + (orgExt) => orgExt.organization_id + ) + organizationIds.push(...organizationIdsFromOrgExtension) + } } else { + // filter out the organizations + // CASE 1 : in case of mentee listing filterout organizations with external_session_visibility_policy = ALL + // CASE 2 : in case of mentor listing filterout organizations with mentor_visibility_policy = ALL const filterQuery = - filter_type.toLowerCase() == common.MENTEE_ROLE + filterType == common.MENTEE_ROLE ? { external_session_visibility_policy: common.ALL, } : { mentor_visibility_policy: common.ALL, } + + // this filter is applied for the below condition + // SM session_visibility_policy (in case of mentee list) or external_mentor_visibility policy (in case of mentor list) = ALL + // and CASE 1 (mentee list) : Mentees is related to the SM org but external_session_visibility is CURRENT (exclude these mentees) + // CASE 2 : (mentor list) : Mentors is related to SM Org but mentor_visibility set to CURRENT (exclude these mentors) + const additionalFilter = + filterType == common.MENTEE_ROLE + ? { + external_session_visibility_policy: { + [Op.ne]: 'CURRENT', + }, + } + : { + mentor_visibility_policy: { + [Op.ne]: 'CURRENT', + }, + } const organizationExtension = await organisationExtensionQueries.findAll( { [Op.or]: [ filterQuery, { - organization_id: { - [Op.in]: [...relatedOrgs, orgPolicies.organization_id], - }, + [Op.and]: [ + { + organization_id: { + [Op.in]: [...relatedOrgs], + }, + }, + additionalFilter, + ], }, ], }, @@ -950,9 +1009,12 @@ module.exports = class MenteesHelper { attributes: ['organization_id'], } ) + organizationIds.push(orgExtension.organization_id) if (organizationExtension) { - const organizationIds = organizationExtension.map((orgExt) => orgExt.organization_id) - organization_ids.push(...organizationIds) + const organizationIdsFromOrgExtension = organizationExtension.map( + (orgExt) => orgExt.organization_id + ) + organizationIds.push(...organizationIdsFromOrgExtension) } } } @@ -960,7 +1022,7 @@ module.exports = class MenteesHelper { return { success: true, - result: organization_ids, + result: organizationIds, } } catch (error) { return {