From 2072664294e19b2203889c3a3815dadc2ed0abf9 Mon Sep 17 00:00:00 2001 From: adrinr Date: Fri, 10 Mar 2023 17:06:53 +0100 Subject: [PATCH] Move user search to core --- packages/backend-core/src/db/utils.ts | 6 +-- packages/backend-core/src/users.ts | 42 ++++++++++++++++++- .../src/api/controllers/global/users.ts | 2 +- packages/worker/src/sdk/users/users.ts | 39 ----------------- 4 files changed, 45 insertions(+), 44 deletions(-) diff --git a/packages/backend-core/src/db/utils.ts b/packages/backend-core/src/db/utils.ts index 76c52d08ad..441c118235 100644 --- a/packages/backend-core/src/db/utils.ts +++ b/packages/backend-core/src/db/utils.ts @@ -434,8 +434,8 @@ export const getPluginParams = (pluginId?: string | null, otherProps = {}) => { return getDocParams(DocumentType.PLUGIN, pluginId, otherProps) } -export function pagination( - data: any[], +export function pagination( + data: T[], pageSize: number, { paginate, @@ -444,7 +444,7 @@ export function pagination( }: { paginate: boolean property: string - getKey?: (doc: any) => string | undefined + getKey?: (doc: T) => string | undefined } = { paginate: true, property: "_id", diff --git a/packages/backend-core/src/users.ts b/packages/backend-core/src/users.ts index dfc544c3ed..c7d8a94e95 100644 --- a/packages/backend-core/src/users.ts +++ b/packages/backend-core/src/users.ts @@ -8,8 +8,10 @@ import { DocumentType, SEPARATOR, directCouchFind, + getGlobalUserParams, + pagination, } from "./db" -import { BulkDocsResponse, User } from "@budibase/types" +import { BulkDocsResponse, SearchUsersRequest, User } from "@budibase/types" import { getGlobalDB } from "./context" import * as context from "./context" @@ -199,3 +201,41 @@ export const searchGlobalUsersByEmail = async ( } return users } + +const PAGE_LIMIT = 8 +export const paginatedUsers = async ({ + page, + email, + appId, +}: SearchUsersRequest = {}) => { + const db = getGlobalDB() + // get one extra document, to have the next page + const opts: any = { + include_docs: true, + limit: PAGE_LIMIT + 1, + } + // add a startkey if the page was specified (anchor) + if (page) { + opts.startkey = page + } + // property specifies what to use for the page/anchor + let userList: User[], + property = "_id", + getKey + if (appId) { + userList = await searchGlobalUsersByApp(appId, opts) + getKey = (doc: any) => getGlobalUserByAppPage(appId, doc) + } else if (email) { + userList = await searchGlobalUsersByEmail(email, opts) + property = "email" + } else { + // no search, query allDocs + const response = await db.allDocs(getGlobalUserParams(null, opts)) + userList = response.rows.map((row: any) => row.doc) + } + return pagination(userList, PAGE_LIMIT, { + paginate: true, + property, + getKey, + }) +} diff --git a/packages/worker/src/api/controllers/global/users.ts b/packages/worker/src/api/controllers/global/users.ts index 9c95268ce4..c0855ce193 100644 --- a/packages/worker/src/api/controllers/global/users.ts +++ b/packages/worker/src/api/controllers/global/users.ts @@ -197,7 +197,7 @@ export const search = async (ctx: any) => { if (body.paginated === false) { await getAppUsers(ctx) } else { - const paginated = await userSdk.paginatedUsers(body) + const paginated = await userSdk.core.paginatedUsers(body) // user hashed password shouldn't ever be returned for (let user of paginated.data) { if (user) { diff --git a/packages/worker/src/sdk/users/users.ts b/packages/worker/src/sdk/users/users.ts index 135128d816..ad7fcc95e2 100644 --- a/packages/worker/src/sdk/users/users.ts +++ b/packages/worker/src/sdk/users/users.ts @@ -37,8 +37,6 @@ import { EmailTemplatePurpose } from "../../constants" import * as pro from "@budibase/pro" import * as accountSdk from "../accounts" -const PAGE_LIMIT = 8 - export const allUsers = async () => { const db = tenancy.getGlobalDB() const response = await db.allDocs( @@ -68,43 +66,6 @@ export const getUsersByAppAccess = async (appId?: string) => { return response } -export const paginatedUsers = async ({ - page, - email, - appId, -}: SearchUsersRequest = {}) => { - const db = tenancy.getGlobalDB() - // get one extra document, to have the next page - const opts: any = { - include_docs: true, - limit: PAGE_LIMIT + 1, - } - // add a startkey if the page was specified (anchor) - if (page) { - opts.startkey = page - } - // property specifies what to use for the page/anchor - let userList, - property = "_id", - getKey - if (appId) { - userList = await usersCore.searchGlobalUsersByApp(appId, opts) - getKey = (doc: any) => usersCore.getGlobalUserByAppPage(appId, doc) - } else if (email) { - userList = await usersCore.searchGlobalUsersByEmail(email, opts) - property = "email" - } else { - // no search, query allDocs - const response = await db.allDocs(dbUtils.getGlobalUserParams(null, opts)) - userList = response.rows.map((row: any) => row.doc) - } - return dbUtils.pagination(userList, PAGE_LIMIT, { - paginate: true, - property, - getKey, - }) -} - export async function getUserByEmail(email: string) { return usersCore.getGlobalUserByEmail(email) }