Move user search to core
This commit is contained in:
parent
39f9ffa4e6
commit
2072664294
|
@ -434,8 +434,8 @@ export const getPluginParams = (pluginId?: string | null, otherProps = {}) => {
|
||||||
return getDocParams(DocumentType.PLUGIN, pluginId, otherProps)
|
return getDocParams(DocumentType.PLUGIN, pluginId, otherProps)
|
||||||
}
|
}
|
||||||
|
|
||||||
export function pagination(
|
export function pagination<T>(
|
||||||
data: any[],
|
data: T[],
|
||||||
pageSize: number,
|
pageSize: number,
|
||||||
{
|
{
|
||||||
paginate,
|
paginate,
|
||||||
|
@ -444,7 +444,7 @@ export function pagination(
|
||||||
}: {
|
}: {
|
||||||
paginate: boolean
|
paginate: boolean
|
||||||
property: string
|
property: string
|
||||||
getKey?: (doc: any) => string | undefined
|
getKey?: (doc: T) => string | undefined
|
||||||
} = {
|
} = {
|
||||||
paginate: true,
|
paginate: true,
|
||||||
property: "_id",
|
property: "_id",
|
||||||
|
|
|
@ -8,8 +8,10 @@ import {
|
||||||
DocumentType,
|
DocumentType,
|
||||||
SEPARATOR,
|
SEPARATOR,
|
||||||
directCouchFind,
|
directCouchFind,
|
||||||
|
getGlobalUserParams,
|
||||||
|
pagination,
|
||||||
} from "./db"
|
} from "./db"
|
||||||
import { BulkDocsResponse, User } from "@budibase/types"
|
import { BulkDocsResponse, SearchUsersRequest, User } from "@budibase/types"
|
||||||
import { getGlobalDB } from "./context"
|
import { getGlobalDB } from "./context"
|
||||||
import * as context from "./context"
|
import * as context from "./context"
|
||||||
|
|
||||||
|
@ -199,3 +201,41 @@ export const searchGlobalUsersByEmail = async (
|
||||||
}
|
}
|
||||||
return users
|
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,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
|
@ -197,7 +197,7 @@ export const search = async (ctx: any) => {
|
||||||
if (body.paginated === false) {
|
if (body.paginated === false) {
|
||||||
await getAppUsers(ctx)
|
await getAppUsers(ctx)
|
||||||
} else {
|
} else {
|
||||||
const paginated = await userSdk.paginatedUsers(body)
|
const paginated = await userSdk.core.paginatedUsers(body)
|
||||||
// user hashed password shouldn't ever be returned
|
// user hashed password shouldn't ever be returned
|
||||||
for (let user of paginated.data) {
|
for (let user of paginated.data) {
|
||||||
if (user) {
|
if (user) {
|
||||||
|
|
|
@ -37,8 +37,6 @@ import { EmailTemplatePurpose } from "../../constants"
|
||||||
import * as pro from "@budibase/pro"
|
import * as pro from "@budibase/pro"
|
||||||
import * as accountSdk from "../accounts"
|
import * as accountSdk from "../accounts"
|
||||||
|
|
||||||
const PAGE_LIMIT = 8
|
|
||||||
|
|
||||||
export const allUsers = async () => {
|
export const allUsers = async () => {
|
||||||
const db = tenancy.getGlobalDB()
|
const db = tenancy.getGlobalDB()
|
||||||
const response = await db.allDocs(
|
const response = await db.allDocs(
|
||||||
|
@ -68,43 +66,6 @@ export const getUsersByAppAccess = async (appId?: string) => {
|
||||||
return response
|
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) {
|
export async function getUserByEmail(email: string) {
|
||||||
return usersCore.getGlobalUserByEmail(email)
|
return usersCore.getGlobalUserByEmail(email)
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue