Adding the ability to cleanup users from get functions (default is old behaviour).
This commit is contained in:
parent
01076bd35f
commit
0bc340052c
|
@ -10,14 +10,35 @@ import { BulkDocsResponse, User } from "@budibase/types"
|
||||||
import { getGlobalDB } from "./context"
|
import { getGlobalDB } from "./context"
|
||||||
import * as context from "./context"
|
import * as context from "./context"
|
||||||
|
|
||||||
export const bulkGetGlobalUsersById = async (userIds: string[]) => {
|
type GetOpts = { cleanup?: boolean }
|
||||||
|
|
||||||
|
function cleanupUsers(users: User | User[]) {
|
||||||
|
if (Array.isArray(users)) {
|
||||||
|
return users.map(user => {
|
||||||
|
delete user.password
|
||||||
|
return user
|
||||||
|
})
|
||||||
|
} else {
|
||||||
|
delete users.password
|
||||||
|
return users
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
export const bulkGetGlobalUsersById = async (
|
||||||
|
userIds: string[],
|
||||||
|
opts?: GetOpts
|
||||||
|
) => {
|
||||||
const db = getGlobalDB()
|
const db = getGlobalDB()
|
||||||
return (
|
let users = (
|
||||||
await db.allDocs({
|
await db.allDocs({
|
||||||
keys: userIds,
|
keys: userIds,
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
})
|
})
|
||||||
).rows.map(row => row.doc) as User[]
|
).rows.map(row => row.doc) as User[]
|
||||||
|
if (opts?.cleanup) {
|
||||||
|
users = cleanupUsers(users) as User[]
|
||||||
|
}
|
||||||
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
export const bulkUpdateGlobalUsers = async (users: User[]) => {
|
export const bulkUpdateGlobalUsers = async (users: User[]) => {
|
||||||
|
@ -25,9 +46,13 @@ export const bulkUpdateGlobalUsers = async (users: User[]) => {
|
||||||
return (await db.bulkDocs(users)) as BulkDocsResponse
|
return (await db.bulkDocs(users)) as BulkDocsResponse
|
||||||
}
|
}
|
||||||
|
|
||||||
export async function getById(id: string): Promise<User> {
|
export async function getById(id: string, opts?: GetOpts): Promise<User> {
|
||||||
const db = context.getGlobalDB()
|
const db = context.getGlobalDB()
|
||||||
return db.get(id)
|
let user = await db.get(id)
|
||||||
|
if (opts?.cleanup) {
|
||||||
|
user = cleanupUsers(user)
|
||||||
|
}
|
||||||
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -36,7 +61,8 @@ export async function getById(id: string): Promise<User> {
|
||||||
* @param {string} email the email to lookup the user by.
|
* @param {string} email the email to lookup the user by.
|
||||||
*/
|
*/
|
||||||
export const getGlobalUserByEmail = async (
|
export const getGlobalUserByEmail = async (
|
||||||
email: String
|
email: String,
|
||||||
|
opts?: GetOpts
|
||||||
): Promise<User | undefined> => {
|
): Promise<User | undefined> => {
|
||||||
if (email == null) {
|
if (email == null) {
|
||||||
throw "Must supply an email address to view"
|
throw "Must supply an email address to view"
|
||||||
|
@ -52,10 +78,19 @@ export const getGlobalUserByEmail = async (
|
||||||
throw new Error(`Multiple users found with email address: ${email}`)
|
throw new Error(`Multiple users found with email address: ${email}`)
|
||||||
}
|
}
|
||||||
|
|
||||||
return response
|
let user = response as User
|
||||||
|
if (opts?.cleanup) {
|
||||||
|
user = cleanupUsers(user) as User
|
||||||
|
}
|
||||||
|
|
||||||
|
return user
|
||||||
}
|
}
|
||||||
|
|
||||||
export const searchGlobalUsersByApp = async (appId: any, opts: any) => {
|
export const searchGlobalUsersByApp = async (
|
||||||
|
appId: any,
|
||||||
|
opts: any,
|
||||||
|
getOpts?: GetOpts
|
||||||
|
) => {
|
||||||
if (typeof appId !== "string") {
|
if (typeof appId !== "string") {
|
||||||
throw new Error("Must provide a string based app ID")
|
throw new Error("Must provide a string based app ID")
|
||||||
}
|
}
|
||||||
|
@ -67,7 +102,11 @@ export const searchGlobalUsersByApp = async (appId: any, opts: any) => {
|
||||||
if (!response) {
|
if (!response) {
|
||||||
response = []
|
response = []
|
||||||
}
|
}
|
||||||
return Array.isArray(response) ? response : [response]
|
let users: User[] = Array.isArray(response) ? response : [response]
|
||||||
|
if (getOpts?.cleanup) {
|
||||||
|
users = cleanupUsers(users) as User[]
|
||||||
|
}
|
||||||
|
return users
|
||||||
}
|
}
|
||||||
|
|
||||||
export const getGlobalUserByAppPage = (appId: string, user: User) => {
|
export const getGlobalUserByAppPage = (appId: string, user: User) => {
|
||||||
|
@ -80,7 +119,11 @@ export const getGlobalUserByAppPage = (appId: string, user: User) => {
|
||||||
/**
|
/**
|
||||||
* Performs a starts with search on the global email view.
|
* Performs a starts with search on the global email view.
|
||||||
*/
|
*/
|
||||||
export const searchGlobalUsersByEmail = async (email: string, opts: any) => {
|
export const searchGlobalUsersByEmail = async (
|
||||||
|
email: string,
|
||||||
|
opts: any,
|
||||||
|
getOpts?: GetOpts
|
||||||
|
) => {
|
||||||
if (typeof email !== "string") {
|
if (typeof email !== "string") {
|
||||||
throw new Error("Must provide a string to search by")
|
throw new Error("Must provide a string to search by")
|
||||||
}
|
}
|
||||||
|
@ -95,5 +138,9 @@ export const searchGlobalUsersByEmail = async (email: string, opts: any) => {
|
||||||
if (!response) {
|
if (!response) {
|
||||||
response = []
|
response = []
|
||||||
}
|
}
|
||||||
return Array.isArray(response) ? response : [response]
|
let users: User[] = Array.isArray(response) ? response : [response]
|
||||||
|
if (getOpts?.cleanup) {
|
||||||
|
users = cleanupUsers(users) as User[]
|
||||||
|
}
|
||||||
|
return users
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue