2022-07-06 17:09:05 +02:00
|
|
|
const { ViewNames, getUsersByAppParams, getProdAppID } = require("./db/utils")
|
2022-04-08 02:28:22 +02:00
|
|
|
const { queryGlobalView } = require("./db/views")
|
2022-07-06 17:09:05 +02:00
|
|
|
const { UNICODE_MAX, SEPARATOR } = require("./db/constants")
|
2022-04-08 02:28:22 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Given an email address this will use a view to search through
|
|
|
|
* all the users to find one with this email address.
|
|
|
|
* @param {string} email the email to lookup the user by.
|
|
|
|
* @return {Promise<object|null>}
|
|
|
|
*/
|
|
|
|
exports.getGlobalUserByEmail = async email => {
|
|
|
|
if (email == null) {
|
|
|
|
throw "Must supply an email address to view"
|
|
|
|
}
|
|
|
|
|
2022-07-06 17:09:05 +02:00
|
|
|
return await queryGlobalView(ViewNames.USER_BY_EMAIL, {
|
2022-04-08 02:28:22 +02:00
|
|
|
key: email.toLowerCase(),
|
|
|
|
include_docs: true,
|
|
|
|
})
|
2022-07-06 17:09:05 +02:00
|
|
|
}
|
2022-04-12 13:34:36 +02:00
|
|
|
|
2022-07-06 17:09:05 +02:00
|
|
|
exports.searchGlobalUsersByApp = async (appId, opts) => {
|
|
|
|
if (typeof appId !== "string") {
|
|
|
|
throw new Error("Must provide a string based app ID")
|
|
|
|
}
|
|
|
|
const params = getUsersByAppParams(appId, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
params.startkey = opts && opts.startkey ? opts.startkey : params.startkey
|
|
|
|
let response = await queryGlobalView(ViewNames.USER_BY_APP, params)
|
|
|
|
if (!response) {
|
|
|
|
response = []
|
|
|
|
}
|
|
|
|
return Array.isArray(response) ? response : [response]
|
|
|
|
}
|
|
|
|
|
|
|
|
exports.getGlobalUserByAppPage = (appId, user) => {
|
|
|
|
if (!user) {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return `${getProdAppID(appId)}${SEPARATOR}${user._id}`
|
2022-04-08 02:28:22 +02:00
|
|
|
}
|
2022-06-30 16:39:26 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Performs a starts with search on the global email view.
|
|
|
|
*/
|
|
|
|
exports.searchGlobalUsersByEmail = async (email, opts) => {
|
|
|
|
if (typeof email !== "string") {
|
|
|
|
throw new Error("Must provide a string to search by")
|
|
|
|
}
|
|
|
|
const lcEmail = email.toLowerCase()
|
|
|
|
// handle if passing up startkey for pagination
|
|
|
|
const startkey = opts && opts.startkey ? opts.startkey : lcEmail
|
|
|
|
let response = await queryGlobalView(ViewNames.USER_BY_EMAIL, {
|
|
|
|
...opts,
|
|
|
|
startkey,
|
|
|
|
endkey: `${lcEmail}${UNICODE_MAX}`,
|
|
|
|
})
|
|
|
|
if (!response) {
|
|
|
|
response = []
|
|
|
|
}
|
|
|
|
return Array.isArray(response) ? response : [response]
|
|
|
|
}
|