more efficient fetching of total users per app

This commit is contained in:
Peter Clement 2022-07-29 13:10:00 +01:00
parent 7dbd6ddd8d
commit 6d12b1b24f
6 changed files with 42 additions and 6 deletions

View File

@ -13,7 +13,6 @@
export let app
export let addData
export let appUsers = []
let prevSearch = undefined,
search = undefined
let pageInfo = createPaginationStore()
@ -32,7 +31,7 @@
prevSearch = search
try {
pageInfo.loading()
await users.search({ page, search })
await users.search({ page, email: search })
pageInfo.fetched($users.hasNextPage, $users.nextPage)
} catch (error) {
notifications.error("Error getting user list")
@ -83,10 +82,10 @@
<PickerDropdown
autocomplete
primaryOptions={optionSections}
placeholder={"Search Users"}
secondaryOptions={$roles}
bind:primaryValue={input.id}
bind:secondaryValue={input.role}
bind:searchTerm={search}
getPrimaryOptionLabel={group => group.name}
getPrimaryOptionValue={group => group.name}
getPrimaryOptionIcon={group => group.icon}

View File

@ -61,6 +61,7 @@ export function createUsersStore() {
break
case "admin":
body.admin = { global: true }
body.builder = { global: true }
break
}
@ -77,6 +78,10 @@ export function createUsersStore() {
update(users => users.filter(user => user._id !== id))
}
async function getUserCountByApp({ appId }) {
return await API.getUserCountByApp({ appId })
}
async function bulkDelete(userIds) {
await API.deleteUsers(userIds)
}
@ -99,6 +104,7 @@ export function createUsersStore() {
create,
save,
bulkDelete,
getUserCountByApp,
delete: del,
}
}

View File

@ -172,4 +172,15 @@ export const buildUserEndpoints = API => ({
},
})
},
/**
* Accepts an invite to join the platform and creates a user.
* @param inviteCode the invite code sent in the email
* @param password the password for the newly created user
*/
getUserCountByApp: async ({ appId }) => {
return await API.get({
url: `/api/global/users/count/${appId}`,
})
},
})

View File

@ -3,7 +3,7 @@ import { checkInviteCode } from "../../../utilities/redis"
import { sendEmail } from "../../../utilities/email"
import { users } from "../../../sdk"
import env from "../../../environment"
import { User, CloudAccount, UserGroup } from "@budibase/types"
import { User, CloudAccount } from "@budibase/types"
import {
events,
errors,
@ -114,6 +114,16 @@ export const adminUser = async (ctx: any) => {
})
}
export const countByApp = async (ctx: any) => {
const appId = ctx.params.appId
try {
const response = await users.countUsersByApp(appId)
ctx.body = response
} catch (err: any) {
ctx.throw(err.status || 400, err)
}
}
export const destroy = async (ctx: any) => {
const id = ctx.params.id

View File

@ -64,6 +64,7 @@ router
.post("/api/global/users/search", builderOrAdmin, controller.search)
.delete("/api/global/users/:id", adminOnly, controller.destroy)
.post("/api/global/users/bulkDelete", adminOnly, controller.bulkDelete)
.get("/api/global/users/count/:appId", adminOnly, controller.countByApp)
.get("/api/global/roles/:appId")
.post(
"/api/global/users/invite",

View File

@ -20,7 +20,7 @@ import { groups as groupUtils } from "@budibase/pro"
const PAGE_LIMIT = 8
export const allUsers = async (newDb?: any) => {
export const allUsers = async () => {
const db = tenancy.getGlobalDB()
const response = await db.allDocs(
dbUtils.getGlobalUserParams(null, {
@ -30,6 +30,15 @@ export const allUsers = async (newDb?: any) => {
return response.rows.map((row: any) => row.doc)
}
export const countUsersByApp = async (appId: string) => {
let response: any = await usersCore.searchGlobalUsersByApp(appId, {
include_docs: true,
})
return {
userCount: response.length,
}
}
export const paginatedUsers = async ({
page,
email,
@ -56,7 +65,7 @@ export const paginatedUsers = async ({
userList = await usersCore.searchGlobalUsersByEmail(email, opts)
property = "email"
} else {
// no search, query allDocs
// no search, query allDcso
const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts))
userList = response.rows.map((row: any) => row.doc)
}