Type group endpoints

This commit is contained in:
Andrew Kingston 2024-12-02 15:58:16 +00:00
parent 1b6e6ed7db
commit 16205644bd
No known key found for this signature in database
3 changed files with 79 additions and 24 deletions

View File

@ -46,10 +46,7 @@ export function createGroupsStore() {
},
delete: async group => {
await API.deleteGroup({
id: group._id,
rev: group._rev,
})
await API.deleteGroup(group._id, group._rev)
store.update(state => {
state = state.filter(state => state._id !== group._id)
return state
@ -89,11 +86,11 @@ export function createGroupsStore() {
},
addGroupAppBuilder: async (groupId, appId) => {
return await API.addGroupAppBuilder({ groupId, appId })
return await API.addGroupAppBuilder(groupId, appId)
},
removeGroupAppBuilder: async (groupId, appId) => {
return await API.removeGroupAppBuilder({ groupId, appId })
return await API.removeGroupAppBuilder(groupId, appId)
},
}

View File

@ -1,13 +1,50 @@
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({
import { SearchUserGroupResponse, UserGroup } from "@budibase/types"
import { BaseAPIClient } from "./types"
export interface GroupEndpoints {
saveGroup: (group: UserGroup) => Promise<{ _id: string; _rev: string }>
getGroups: () => Promise<UserGroup[]>
getGroup: (id: string) => Promise<UserGroup>
deleteGroup: (id: string, rev: string) => Promise<{ message: string }>
getGroupUsers: (
data: GetGroupUsersRequest
) => Promise<SearchUserGroupResponse>
addUsersToGroup: (groupId: string, userIds: string[]) => Promise<void>
removeUsersFromGroup: (groupId: string, userIds: string[]) => Promise<void>
addAppsToGroup: (groupId: string, appArray: object[]) => Promise<void>
removeAppsFromGroup: (groupId: string, appArray: object[]) => Promise<void>
addGroupAppBuilder: (groupId: string, appId: string) => Promise<void>
removeGroupAppBuilder: (groupId: string, appId: string) => Promise<void>
}
enum GroupResource {
USERS = "users",
APPS = "apps",
}
enum GroupOperation {
ADD = "add",
REMOVE = "remove",
}
type GetGroupUsersRequest = {
id: string
bookmark?: string
emailSearch?: string
}
export const buildGroupsEndpoints = (API: BaseAPIClient): GroupEndpoints => {
// Underlying functionality of adding/removing users/apps to groups
async function updateGroupResource(
groupId: string,
resource: GroupResource,
operation: GroupOperation,
resources: string[] | object[]
) {
return await API.post<{ [key in GroupOperation]?: string[] | object[] }>({
url: `/api/global/groups/${groupId}/${resource}`,
body: {
[operation]: ids,
[operation]: resources,
},
})
}
@ -46,7 +83,7 @@ export const buildGroupsEndpoints = API => {
* @param id the id of the config to delete
* @param rev the revision of the config to delete
*/
deleteGroup: async ({ id, rev }) => {
deleteGroup: async (id, rev) => {
return await API.delete({
url: `/api/global/groups/${id}/${rev}`,
})
@ -61,9 +98,8 @@ export const buildGroupsEndpoints = API => {
url += `bookmark=${bookmark}&`
}
if (emailSearch) {
url += `emailSearch=${emailSearch}&`
url += `emailSearch=${emailSearch}`
}
return await API.get({
url,
})
@ -75,7 +111,12 @@ export const buildGroupsEndpoints = API => {
* @param userIds The user IDs to be added
*/
addUsersToGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "add", userIds)
return updateGroupResource(
groupId,
GroupResource.USERS,
GroupOperation.ADD,
userIds
)
},
/**
@ -84,7 +125,12 @@ export const buildGroupsEndpoints = API => {
* @param userIds The user IDs to be removed
*/
removeUsersFromGroup: async (groupId, userIds) => {
return updateGroupResource(groupId, "users", "remove", userIds)
return updateGroupResource(
groupId,
GroupResource.USERS,
GroupOperation.REMOVE,
userIds
)
},
/**
@ -93,7 +139,12 @@ export const buildGroupsEndpoints = API => {
* @param appArray Array of objects, containing the appId and roleId to be added
*/
addAppsToGroup: async (groupId, appArray) => {
return updateGroupResource(groupId, "apps", "add", appArray)
return updateGroupResource(
groupId,
GroupResource.APPS,
GroupOperation.ADD,
appArray
)
},
/**
@ -102,7 +153,12 @@ export const buildGroupsEndpoints = API => {
* @param appArray Array of objects, containing the appId to be removed
*/
removeAppsFromGroup: async (groupId, appArray) => {
return updateGroupResource(groupId, "apps", "remove", appArray)
return updateGroupResource(
groupId,
GroupResource.APPS,
GroupOperation.REMOVE,
appArray
)
},
/**
@ -110,7 +166,7 @@ export const buildGroupsEndpoints = API => {
* @param groupId The group to update
* @param appId The app id where the builder will be added
*/
addGroupAppBuilder: async ({ groupId, appId }) => {
addGroupAppBuilder: async (groupId, appId) => {
return await API.post({
url: `/api/global/groups/${groupId}/app/${appId}/builder`,
})
@ -121,7 +177,7 @@ export const buildGroupsEndpoints = API => {
* @param groupId The group to update
* @param appId The app id where the builder will be removed
*/
removeGroupAppBuilder: async ({ groupId, appId }) => {
removeGroupAppBuilder: async (groupId, appId) => {
return await API.delete({
url: `/api/global/groups/${groupId}/app/${appId}/builder`,
})

View File

@ -11,6 +11,7 @@ import { DatasourceEndpoints } from "./datasources"
import { EnvironmentVariableEndpoints } from "./environmentVariables"
import { EventEndpoints } from "./events"
import { FlagEndpoints } from "./flags"
import { GroupEndpoints } from "./groups"
export enum HTTPMethod {
POST = "POST",
@ -83,4 +84,5 @@ export type APIClient = BaseAPIClient &
DatasourceEndpoints &
EnvironmentVariableEndpoints &
EventEndpoints &
FlagEndpoints
FlagEndpoints &
GroupEndpoints