2023-07-25 18:48:57 +02:00
|
|
|
import {
|
|
|
|
AccountMetadata,
|
|
|
|
PlatformUser,
|
|
|
|
PlatformUserByEmail,
|
|
|
|
User,
|
|
|
|
} from "@budibase/types"
|
|
|
|
import * as dbUtils from "../db"
|
|
|
|
import { ViewName } from "../constants"
|
2023-11-17 17:20:10 +01:00
|
|
|
import { getExistingInvites } from "../cache/invite"
|
2023-07-25 18:48:57 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Apply a system-wide search on emails:
|
|
|
|
* - in tenant
|
|
|
|
* - cross tenant
|
|
|
|
* - accounts
|
|
|
|
* return an array of emails that match the supplied emails.
|
|
|
|
*/
|
|
|
|
export async function searchExistingEmails(emails: string[]) {
|
|
|
|
let matchedEmails: string[] = []
|
|
|
|
|
|
|
|
const existingTenantUsers = await getExistingTenantUsers(emails)
|
|
|
|
matchedEmails.push(...existingTenantUsers.map(user => user.email))
|
|
|
|
|
|
|
|
const existingPlatformUsers = await getExistingPlatformUsers(emails)
|
|
|
|
matchedEmails.push(...existingPlatformUsers.map(user => user._id!))
|
|
|
|
|
|
|
|
const existingAccounts = await getExistingAccounts(emails)
|
|
|
|
matchedEmails.push(...existingAccounts.map(account => account.email))
|
|
|
|
|
2023-11-09 15:51:07 +01:00
|
|
|
const invitedEmails = await getExistingInvites(emails)
|
|
|
|
matchedEmails.push(...invitedEmails.map(invite => invite.email))
|
|
|
|
|
2023-07-25 18:48:57 +02:00
|
|
|
return [...new Set(matchedEmails.map(email => email.toLowerCase()))]
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup, could be email or userId, either will return a doc
|
|
|
|
export async function getPlatformUser(
|
|
|
|
identifier: string
|
|
|
|
): Promise<PlatformUser | null> {
|
|
|
|
// use the view here and allow to find anyone regardless of casing
|
|
|
|
// Use lowercase to ensure email login is case insensitive
|
|
|
|
return (await dbUtils.queryPlatformView(ViewName.PLATFORM_USERS_LOWERCASE, {
|
|
|
|
keys: [identifier.toLowerCase()],
|
|
|
|
include_docs: true,
|
|
|
|
})) as PlatformUser
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExistingTenantUsers(
|
|
|
|
emails: string[]
|
|
|
|
): Promise<User[]> {
|
|
|
|
const lcEmails = emails.map(email => email.toLowerCase())
|
|
|
|
const params = {
|
|
|
|
keys: lcEmails,
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
arrayResponse: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return (await dbUtils.queryGlobalView(
|
|
|
|
ViewName.USER_BY_EMAIL,
|
|
|
|
params,
|
|
|
|
undefined,
|
|
|
|
opts
|
|
|
|
)) as User[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExistingPlatformUsers(
|
|
|
|
emails: string[]
|
|
|
|
): Promise<PlatformUserByEmail[]> {
|
|
|
|
const lcEmails = emails.map(email => email.toLowerCase())
|
|
|
|
const params = {
|
|
|
|
keys: lcEmails,
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
arrayResponse: true,
|
|
|
|
}
|
|
|
|
return (await dbUtils.queryPlatformView(
|
|
|
|
ViewName.PLATFORM_USERS_LOWERCASE,
|
|
|
|
params,
|
|
|
|
opts
|
|
|
|
)) as PlatformUserByEmail[]
|
|
|
|
}
|
|
|
|
|
|
|
|
export async function getExistingAccounts(
|
|
|
|
emails: string[]
|
|
|
|
): Promise<AccountMetadata[]> {
|
|
|
|
const lcEmails = emails.map(email => email.toLowerCase())
|
|
|
|
const params = {
|
|
|
|
keys: lcEmails,
|
|
|
|
include_docs: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
const opts = {
|
|
|
|
arrayResponse: true,
|
|
|
|
}
|
|
|
|
|
|
|
|
return (await dbUtils.queryPlatformView(
|
|
|
|
ViewName.ACCOUNT_BY_EMAIL,
|
|
|
|
params,
|
|
|
|
opts
|
|
|
|
)) as AccountMetadata[]
|
|
|
|
}
|