1
0
Fork 0
mirror of synced 2024-07-30 02:26:11 +12:00

Updating sync to cover both prod and dev apps.

This commit is contained in:
mike12345567 2023-05-17 19:35:00 +01:00
parent 5fdd7d9076
commit cf7107d19a
2 changed files with 33 additions and 28 deletions

View file

@ -7,7 +7,7 @@ import {
InternalTables, InternalTables,
} from "../../db/utils" } from "../../db/utils"
import { isEqual } from "lodash" import { isEqual } from "lodash"
import { ContextUser, UserMetadata, User } from "@budibase/types" import { ContextUser, UserMetadata, User, Database } from "@budibase/types"
export function combineMetadataAndUser( export function combineMetadataAndUser(
user: ContextUser, user: ContextUser,
@ -51,8 +51,10 @@ export function combineMetadataAndUser(
return null return null
} }
export async function rawUserMetadata() { export async function rawUserMetadata(db?: Database) {
const db = context.getAppDB() if (!db) {
db = context.getAppDB()
}
return ( return (
await db.allDocs( await db.allDocs(
getUserMetadataParams(null, { getUserMetadataParams(null, {
@ -64,30 +66,36 @@ export async function rawUserMetadata() {
export async function syncGlobalUsers() { export async function syncGlobalUsers() {
// sync user metadata // sync user metadata
const db = context.getAppDB() const dbs = [context.getDevAppDB(), context.getProdAppDB()]
const resp = await Promise.all([getGlobalUsers(), rawUserMetadata()]) for (let db of dbs) {
const users = resp[0] as User[] if (!(await db.exists())) {
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) {
continue continue
} }
const alreadyExisting = data.email && foundEmails.indexOf(data.email) !== -1 const resp = await Promise.all([getGlobalUsers(), rawUserMetadata(db)])
const globalId = getGlobalIDFromUserMetadataID(data._id) const users = resp[0] as User[]
if (!users.find(user => user._id === globalId) || alreadyExisting) { const metadata = resp[1] as UserMetadata[]
toWrite.push({ ...data, _deleted: true }) const toWrite = []
for (let user of users) {
const combined = combineMetadataAndUser(user, metadata)
if (combined) {
toWrite.push(combined)
}
} }
if (data.email) { let foundEmails: string[] = []
foundEmails.push(data.email) 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)
} }

View file

@ -122,11 +122,8 @@ export async function getGlobalUsers(
delete user.forceResetPassword delete user.forceResetPassword
return user return user
}) })
if (!appId) {
return globalUsers
}
if (opts?.noProcessing) { if (opts?.noProcessing || !appId) {
return globalUsers return globalUsers
} else { } else {
// pass in the groups, meaning we don't actually need to retrieve them for // pass in the groups, meaning we don't actually need to retrieve them for