diff --git a/packages/server/src/sdk/users/utils.ts b/packages/server/src/sdk/users/utils.ts index 7f7f0b4809..619a7199cd 100644 --- a/packages/server/src/sdk/users/utils.ts +++ b/packages/server/src/sdk/users/utils.ts @@ -7,7 +7,7 @@ import { InternalTables, } from "../../db/utils" import { isEqual } from "lodash" -import { ContextUser, UserMetadata, User } from "@budibase/types" +import { ContextUser, UserMetadata, User, Database } from "@budibase/types" export function combineMetadataAndUser( user: ContextUser, @@ -51,8 +51,10 @@ export function combineMetadataAndUser( return null } -export async function rawUserMetadata() { - const db = context.getAppDB() +export async function rawUserMetadata(db?: Database) { + if (!db) { + db = context.getAppDB() + } return ( await db.allDocs( getUserMetadataParams(null, { @@ -64,30 +66,36 @@ export async function rawUserMetadata() { export async function syncGlobalUsers() { // sync user metadata - const db = context.getAppDB() - const resp = await Promise.all([getGlobalUsers(), rawUserMetadata()]) - const users = resp[0] as User[] - const metadata = resp[1] as UserMetadata[] - const toWrite = [] - for (let user of users) { - const combined = combineMetadataAndUser(user, metadata) - if (combined) { - toWrite.push(combined) - } - } - let foundEmails: string[] = [] - for (let data of metadata) { - if (!data._id) { + const dbs = [context.getDevAppDB(), context.getProdAppDB()] + for (let db of dbs) { + if (!(await db.exists())) { continue } - const alreadyExisting = data.email && foundEmails.indexOf(data.email) !== -1 - const globalId = getGlobalIDFromUserMetadataID(data._id) - if (!users.find(user => user._id === globalId) || alreadyExisting) { - toWrite.push({ ...data, _deleted: true }) + const resp = await Promise.all([getGlobalUsers(), rawUserMetadata(db)]) + const users = resp[0] as User[] + const metadata = resp[1] as UserMetadata[] + const toWrite = [] + for (let user of users) { + const combined = combineMetadataAndUser(user, metadata) + if (combined) { + toWrite.push(combined) + } } - if (data.email) { - foundEmails.push(data.email) + let foundEmails: string[] = [] + for (let data of metadata) { + if (!data._id) { + continue + } + const alreadyExisting = + data.email && foundEmails.indexOf(data.email) !== -1 + const globalId = getGlobalIDFromUserMetadataID(data._id) + if (!users.find(user => user._id === globalId) || alreadyExisting) { + toWrite.push({ ...data, _deleted: true }) + } + if (data.email) { + foundEmails.push(data.email) + } } + await db.bulkDocs(toWrite) } - await db.bulkDocs(toWrite) } diff --git a/packages/server/src/utilities/global.ts b/packages/server/src/utilities/global.ts index 21e86a28b9..93b7fc1207 100644 --- a/packages/server/src/utilities/global.ts +++ b/packages/server/src/utilities/global.ts @@ -122,11 +122,8 @@ export async function getGlobalUsers( delete user.forceResetPassword return user }) - if (!appId) { - return globalUsers - } - if (opts?.noProcessing) { + if (opts?.noProcessing || !appId) { return globalUsers } else { // pass in the groups, meaning we don't actually need to retrieve them for