Skip to content

PLAT-2125 #134

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

Merged
merged 5 commits into from
Jan 25, 2023
Merged
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
28 changes: 18 additions & 10 deletions src/services/GroupService.js
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,7 @@ async function updateGroup(currentUser, groupId, data) {

// update the cache
const cache = await helper.getCacheInstance()
cache.set(group.id, updateGroup)
cache.set(group.id, updatedGroup)

return updatedGroup
} catch (error) {
Expand Down Expand Up @@ -375,7 +375,7 @@ async function getGroup(currentUser, groupId, criteria) {
)}`
)

if (_.has(criteria, 'skipCache')) {
if (!_.has(criteria, 'skipCache')) {
criteria.skipCache = false
}

Expand Down Expand Up @@ -425,8 +425,14 @@ async function getGroup(currentUser, groupId, criteria) {
)
}
}

const session = helper.createDBSession()

let session
const getSession = () => {
if (!session) {
session = helper.createDBSession()
}
return session
}
const cache = await helper.getCacheInstance()

let groupToReturn
Expand All @@ -445,21 +451,21 @@ async function getGroup(currentUser, groupId, criteria) {
const cachedGroupMembers = cache.get(`${groupId}-members`)

if (!_.includes(cachedGroupMembers, currentUser.userId)) {
await helper.ensureGroupMember(session, group.id, currentUser.userId)
await helper.ensureGroupMember(getSession(), group.id, currentUser.userId)

cachedGroupMembers.push(currentUser.userId)
cache.set(`${groupId}-members`, cachedGroupMembers)
}
}
} else {
groupToReturn = await helper.ensureExists(session, 'Group', groupId, isAdmin)
groupToReturn = await helper.ensureExists(getSession(), 'Group', groupId, isAdmin)
cache.set(groupId, groupToReturn)

if (!isAdmin) delete groupToReturn.status

// if the group is private, the user needs to be a member of the group, or an admin
if (groupToReturn.privateGroup && currentUser !== 'M2M' && !helper.hasAdminRole(currentUser)) {
await helper.ensureGroupMember(session, groupToReturn.id, currentUser.userId)
await helper.ensureGroupMember(getSession(), groupToReturn.id, currentUser.userId)

cache.set(`${groupId}-members`, [currentUser.userId])
}
Expand Down Expand Up @@ -489,7 +495,7 @@ async function getGroup(currentUser, groupId, criteria) {
const flattenGroupIdTree = []

// find child groups
groupToExpand.subGroups = await helper.getChildGroups(session, groupToExpand.id)
groupToExpand.subGroups = await helper.getChildGroups(getSession(), groupToExpand.id)
// add child groups to pending if needed
if (!criteria.oneLevel) {
_.forEach(groupToExpand.subGroups, (g) => {
Expand All @@ -502,7 +508,7 @@ async function getGroup(currentUser, groupId, criteria) {
}
} else if (criteria.includeParentGroup && !groupToReturn.parentGroups) {
// find parent groups
groupToExpand.parentGroups = await helper.getParentGroups(session, groupToExpand.id)
groupToExpand.parentGroups = await helper.getParentGroups(getSession(), groupToExpand.id)
// add parent groups to pending if needed
if (!criteria.oneLevel) {
_.forEach(groupToExpand.parentGroups, (g) => pending.push(g))
Expand All @@ -529,7 +535,9 @@ async function getGroup(currentUser, groupId, criteria) {
throw error
} finally {
logger.debug('Session Close')
await session.close()
if (session) {
await session.close()
}
}
}

Expand Down