1
0
Fork 0
mirror of synced 2024-07-04 22:11:23 +12:00

Adding the ability to cleanup users from get functions (default is old behaviour).

This commit is contained in:
mike12345567 2023-02-23 11:28:18 +00:00
parent 01076bd35f
commit 0bc340052c

View file

@ -10,14 +10,35 @@ import { BulkDocsResponse, User } from "@budibase/types"
import { getGlobalDB } 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()
return (
let users = (
await db.allDocs({
keys: userIds,
include_docs: true,
})
).rows.map(row => row.doc) as User[]
if (opts?.cleanup) {
users = cleanupUsers(users) as User[]
}
return users
}
export const bulkUpdateGlobalUsers = async (users: User[]) => {
@ -25,9 +46,13 @@ export const bulkUpdateGlobalUsers = async (users: User[]) => {
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()
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.
*/
export const getGlobalUserByEmail = async (
email: String
email: String,
opts?: GetOpts
): Promise<User | undefined> => {
if (email == null) {
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}`)
}
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") {
throw new Error("Must provide a string based app ID")
}
@ -67,7 +102,11 @@ export const searchGlobalUsersByApp = async (appId: any, opts: any) => {
if (!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) => {
@ -80,7 +119,11 @@ export const getGlobalUserByAppPage = (appId: string, user: User) => {
/**
* 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") {
throw new Error("Must provide a string to search by")
}
@ -95,5 +138,9 @@ export const searchGlobalUsersByEmail = async (email: string, opts: any) => {
if (!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
}