Updates to use new backend API for adding/removing users from groups.

This commit is contained in:
mike12345567 2022-09-21 16:58:47 +01:00
parent a1c7e60dfa
commit 120ea88aff
2 changed files with 115 additions and 78 deletions

View File

@ -1,26 +1,13 @@
import { writable, get } from "svelte/store"
import { API } from "api"
import { auth, users } from "stores/portal"
import { auth } from "stores/portal"
export function createGroupsStore() {
const store = writable([])
const actions = {
init: async () => {
// only init if these is a groups license, just to be sure but the feature will be blocked
// on the backend anyway
if (get(auth).groupsEnabled) {
const groups = await API.getGroups()
store.set(groups)
}
},
save: async group => {
const response = await API.saveGroup(group)
group._id = response._id
group._rev = response._rev
const updateStore = group => {
store.update(state => {
const currentIdx = state.findIndex(gr => gr._id === response._id)
const currentIdx = state.findIndex(gr => gr._id === group._id)
if (currentIdx >= 0) {
state.splice(currentIdx, 1, group)
} else {
@ -28,6 +15,30 @@ export function createGroupsStore() {
}
return state
})
}
const getGroup = async groupId => {
const group = await API.getGroup(groupId)
updateStore(group)
}
const actions = {
init: async () => {
// only init if there is a groups license, just to be sure but the feature will be blocked
// on the backend anyway
if (get(auth).groupsEnabled) {
const groups = await API.getGroups()
store.set(groups)
}
},
get: getGroup,
save: async group => {
const response = await API.saveGroup(group)
group._id = response._id
group._rev = response._rev
updateStore(group)
return group
},
@ -43,40 +54,15 @@ export function createGroupsStore() {
},
addUser: async (groupId, userId) => {
// Sanity check
const user = await users.get(userId)
const group = get(store).find(x => x._id === groupId)
if (!group?._id || !user?._id) {
return
}
// Check we haven't already been added
if (group.users?.find(x => x._id === userId)) {
return
}
// Update user
let userGroups = user.userGroups || []
userGroups.push(groupId)
await users.save({
...user,
userGroups,
})
await API.addUsersToGroup(groupId, userId)
// refresh the group enrichment
await getGroup(groupId)
},
removeUser: async (groupId, userId) => {
// Sanity check
const user = await users.get(userId)
const group = get(store).find(x => x._id === groupId)
if (!group?._id || !user?._id) {
return
}
// Update user
await users.save({
...user,
userGroups: user.userGroups.filter(x => x !== groupId),
})
await API.removeUsersFromGroup(groupId, userId)
// refresh the group enrichment
await getGroup(groupId)
},
}

View File

@ -1,7 +1,21 @@
export const buildGroupsEndpoints = API => ({
export const buildGroupsEndpoints = API => {
// underlying functionality of adding/removing users/apps to groups
async function updateGroupResource(groupId, resource, operation, ids) {
if (!Array.isArray(ids)) {
ids = [ids]
}
return await API.post({
url: `/api/global/groups/${groupId}/${resource}`,
body: {
[operation]: ids,
},
})
}
return {
/**
* Creates a user group.
* @param user the new group to create
* @param group the new group to create
*/
saveGroup: async group => {
return await API.post({
@ -10,7 +24,7 @@ export const buildGroupsEndpoints = API => ({
})
},
/**
* Gets all of the user groups
* Gets all the user groups
*/
getGroups: async () => {
return await API.get({
@ -37,4 +51,41 @@ export const buildGroupsEndpoints = API => ({
url: `/api/global/groups/${id}/${rev}`,
})
},
})
/**
* Adds users to a group
* @param groupId The group to update
* @param userIds The user IDs to be added
*/
addUsersToGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "add", userIds)
},
/**
* Removes users from a group
* @param groupId The group to update
* @param userIds The user IDs to be removed
*/
removeUsersFromGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "remove", userIds)
},
/**
* Adds apps to a group
* @param groupId The group to update
* @param appIds The app IDs to be added
*/
addAppsToGroup: async (groupId, appIds) => {
return updateGroupResource(groupId, "apps", "add", appIds)
},
/**
* Removes apps from a group
* @param groupId The group to update
* @param appIds The app IDs to be removed
*/
removeAppsFromGroup: async (groupId, appIds) => {
return updateGroupResource(groupId, "apps", "remove", appIds)
},
}
}