Adding a user core bulk update function.

This commit is contained in:
mike12345567 2022-09-21 13:55:10 +01:00
parent e6c91b6f8e
commit 115a0c146b
3 changed files with 14 additions and 12 deletions

View File

@ -40,9 +40,9 @@ export async function usersAdded(count: number, group: UserGroup) {
await publishEvent(Event.USER_GROUP_USERS_ADDED, properties)
}
export async function usersDeleted(emails: string[], group: UserGroup) {
export async function usersDeleted(count: number, group: UserGroup) {
const properties: GroupUsersDeletedEvent = {
count: emails.length,
count,
groupId: group._id as string,
}
await publishEvent(Event.USER_GROUP_USERS_REMOVED, properties)

View File

@ -6,7 +6,7 @@ import {
} from "./db/utils"
import { queryGlobalView } from "./db/views"
import { UNICODE_MAX } from "./db/constants"
import { User } from "@budibase/types"
import { BulkDocsResponse, User } from "@budibase/types"
import { getGlobalDB } from "./context"
import PouchDB from "pouchdb"
@ -20,6 +20,11 @@ export const bulkGetGlobalUsersById = async (userIds: string[]) => {
).rows.map(row => row.doc) as User[]
}
export const bulkUpdateGlobalUsers = async (users: User[]) => {
const db = getGlobalDB() as PouchDB.Database
return (await db.bulkDocs(users)) as BulkDocsResponse
}
/**
* Given an email address this will use a view to search through
* all the users to find one with this email address.

View File

@ -21,7 +21,6 @@ import {
AllDocsResponse,
BulkCreateUsersResponse,
BulkDeleteUsersResponse,
BulkDocsResponse,
CloudAccount,
CreateUserResponse,
InviteUsersRequest,
@ -391,7 +390,7 @@ export const bulkCreate = async (
})
const usersToBulkSave = await Promise.all(usersToSave)
await db.bulkDocs(usersToBulkSave)
await usersCore.bulkUpdateGlobalUsers(usersToBulkSave)
// Post-processing of bulk added users, e.g. events and cache operations
for (const user of usersToBulkSave) {
@ -469,13 +468,11 @@ export const bulkDelete = async (
)
// Delete from DB
const dbResponse: BulkDocsResponse = await db.bulkDocs(
usersToDelete.map(user => ({
const toDelete = usersToDelete.map(user => ({
...user,
_deleted: true,
}))
)
const dbResponse = await usersCore.bulkUpdateGlobalUsers(toDelete)
for (let user of usersToDelete) {
await bulkDeleteProcessing(user)
}