Updates to use new backend API for adding/removing users from groups.
This commit is contained in:
parent
a1c7e60dfa
commit
120ea88aff
|
@ -1,13 +1,30 @@
|
|||
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 updateStore = group => {
|
||||
store.update(state => {
|
||||
const currentIdx = state.findIndex(gr => gr._id === group._id)
|
||||
if (currentIdx >= 0) {
|
||||
state.splice(currentIdx, 1, group)
|
||||
} else {
|
||||
state.push(group)
|
||||
}
|
||||
return state
|
||||
})
|
||||
}
|
||||
|
||||
const getGroup = async groupId => {
|
||||
const group = await API.getGroup(groupId)
|
||||
updateStore(group)
|
||||
}
|
||||
|
||||
const actions = {
|
||||
init: async () => {
|
||||
// only init if these is a groups license, just to be sure but the feature will be blocked
|
||||
// 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()
|
||||
|
@ -15,19 +32,13 @@ export function createGroupsStore() {
|
|||
}
|
||||
},
|
||||
|
||||
get: getGroup,
|
||||
|
||||
save: async group => {
|
||||
const response = await API.saveGroup(group)
|
||||
group._id = response._id
|
||||
group._rev = response._rev
|
||||
store.update(state => {
|
||||
const currentIdx = state.findIndex(gr => gr._id === response._id)
|
||||
if (currentIdx >= 0) {
|
||||
state.splice(currentIdx, 1, group)
|
||||
} else {
|
||||
state.push(group)
|
||||
}
|
||||
return state
|
||||
})
|
||||
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)
|
||||
},
|
||||
}
|
||||
|
||||
|
|
|
@ -1,40 +1,91 @@
|
|||
export const buildGroupsEndpoints = API => ({
|
||||
/**
|
||||
* Creates a user group.
|
||||
* @param user the new group to create
|
||||
*/
|
||||
saveGroup: async group => {
|
||||
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",
|
||||
body: group,
|
||||
url: `/api/global/groups/${groupId}/${resource}`,
|
||||
body: {
|
||||
[operation]: ids,
|
||||
},
|
||||
})
|
||||
},
|
||||
/**
|
||||
* Gets all of the user groups
|
||||
*/
|
||||
getGroups: async () => {
|
||||
return await API.get({
|
||||
url: "/api/global/groups",
|
||||
})
|
||||
},
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets a group by ID
|
||||
*/
|
||||
getGroup: async id => {
|
||||
return await API.get({
|
||||
url: `/api/global/groups/${id}`,
|
||||
})
|
||||
},
|
||||
return {
|
||||
/**
|
||||
* Creates a user group.
|
||||
* @param group the new group to create
|
||||
*/
|
||||
saveGroup: async group => {
|
||||
return await API.post({
|
||||
url: "/api/global/groups",
|
||||
body: group,
|
||||
})
|
||||
},
|
||||
/**
|
||||
* Gets all the user groups
|
||||
*/
|
||||
getGroups: async () => {
|
||||
return await API.get({
|
||||
url: "/api/global/groups",
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a user group
|
||||
* @param id the id of the config to delete
|
||||
* @param rev the revision of the config to delete
|
||||
*/
|
||||
deleteGroup: async ({ id, rev }) => {
|
||||
return await API.delete({
|
||||
url: `/api/global/groups/${id}/${rev}`,
|
||||
})
|
||||
},
|
||||
})
|
||||
/**
|
||||
* Gets a group by ID
|
||||
*/
|
||||
getGroup: async id => {
|
||||
return await API.get({
|
||||
url: `/api/global/groups/${id}`,
|
||||
})
|
||||
},
|
||||
|
||||
/**
|
||||
* Deletes a user group
|
||||
* @param id the id of the config to delete
|
||||
* @param rev the revision of the config to delete
|
||||
*/
|
||||
deleteGroup: async ({ id, rev }) => {
|
||||
return await API.delete({
|
||||
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)
|
||||
},
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue