Skip to content

Commit

Permalink
Per user per creator changes
Browse files Browse the repository at this point in the history
  • Loading branch information
jvcalderon committed Oct 18, 2023
1 parent 9a8f7ae commit 5b2f55a
Show file tree
Hide file tree
Showing 17 changed files with 236 additions and 55 deletions.
4 changes: 2 additions & 2 deletions packages/backend-core/src/cache/writethrough.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,8 @@ export class Writethrough {
this.writeRateMs = writeRateMs
}

async put(doc: any) {
return put(this.db, doc, this.writeRateMs)
async put(doc: any, writeRateMs: number = this.writeRateMs) {
return put(this.db, doc, writeRateMs)
}

async get(id: string) {
Expand Down
111 changes: 63 additions & 48 deletions packages/backend-core/src/users/db.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,17 +21,21 @@ import {
User,
UserStatus,
UserGroup,
ContextUser,
} from "@budibase/types"
import {
getAccountHolderFromUserIds,
isAdmin,
isCreator,
validateUniqueUser,
} from "./utils"
import { searchExistingEmails } from "./lookup"
import { hash } from "../utils"

type QuotaUpdateFn = (change: number, cb?: () => Promise<any>) => Promise<any>
type QuotaUpdateFn = (
change: number,
creatorsChange: number,
cb?: () => Promise<any>
) => Promise<any>
type GroupUpdateFn = (groupId: string, userIds: string[]) => Promise<any>
type FeatureFn = () => Promise<Boolean>
type GroupGetFn = (ids: string[]) => Promise<UserGroup[]>
Expand Down Expand Up @@ -135,7 +139,7 @@ export class UserDB {
if (!fullUser.roles) {
fullUser.roles = {}
}
// add the active status to a user if its not provided
// add the active status to a user if it's not provided
if (fullUser.status == null) {
fullUser.status = UserStatus.ACTIVE
}
Expand Down Expand Up @@ -246,7 +250,8 @@ export class UserDB {
}

const change = dbUser ? 0 : 1 // no change if there is existing user
return UserDB.quotas.addUsers(change, async () => {
const creatorsChange = isCreator(dbUser) !== isCreator(user) ? 1 : 0
return UserDB.quotas.addUsers(change, creatorsChange, async () => {
await validateUniqueUser(email, tenantId)

let builtUser = await UserDB.buildUser(user, opts, tenantId, dbUser)
Expand Down Expand Up @@ -308,6 +313,7 @@ export class UserDB {

let usersToSave: any[] = []
let newUsers: any[] = []
let newCreators: any[] = []

const emails = newUsersRequested.map((user: User) => user.email)
const existingEmails = await searchExistingEmails(emails)
Expand All @@ -328,59 +334,66 @@ export class UserDB {
}
newUser.userGroups = groups
newUsers.push(newUser)
if (isCreator(newUser)) {
newCreators.push(newUser)
}
}

const account = await accountSdk.getAccountByTenantId(tenantId)
return UserDB.quotas.addUsers(newUsers.length, async () => {
// create the promises array that will be called by bulkDocs
newUsers.forEach((user: any) => {
usersToSave.push(
UserDB.buildUser(
user,
{
hashPassword: true,
requirePassword: user.requirePassword,
},
tenantId,
undefined, // no dbUser
account
return UserDB.quotas.addUsers(
newUsers.length,
newCreators.length,
async () => {
// create the promises array that will be called by bulkDocs
newUsers.forEach((user: any) => {
usersToSave.push(
UserDB.buildUser(
user,
{
hashPassword: true,
requirePassword: user.requirePassword,
},
tenantId,
undefined, // no dbUser
account
)
)
)
})
})

const usersToBulkSave = await Promise.all(usersToSave)
await usersCore.bulkUpdateGlobalUsers(usersToBulkSave)
const usersToBulkSave = await Promise.all(usersToSave)
await usersCore.bulkUpdateGlobalUsers(usersToBulkSave)

// Post-processing of bulk added users, e.g. events and cache operations
for (const user of usersToBulkSave) {
// TODO: Refactor to bulk insert users into the info db
// instead of relying on looping tenant creation
await platform.users.addUser(tenantId, user._id, user.email)
await eventHelpers.handleSaveEvents(user, undefined)
}

const saved = usersToBulkSave.map(user => {
return {
_id: user._id,
email: user.email,
// Post-processing of bulk added users, e.g. events and cache operations
for (const user of usersToBulkSave) {
// TODO: Refactor to bulk insert users into the info db
// instead of relying on looping tenant creation
await platform.users.addUser(tenantId, user._id, user.email)
await eventHelpers.handleSaveEvents(user, undefined)
}
})

// now update the groups
if (Array.isArray(saved) && groups) {
const groupPromises = []
const createdUserIds = saved.map(user => user._id)
for (let groupId of groups) {
groupPromises.push(UserDB.groups.addUsers(groupId, createdUserIds))
const saved = usersToBulkSave.map(user => {
return {
_id: user._id,
email: user.email,
}
})

// now update the groups
if (Array.isArray(saved) && groups) {
const groupPromises = []
const createdUserIds = saved.map(user => user._id)
for (let groupId of groups) {
groupPromises.push(UserDB.groups.addUsers(groupId, createdUserIds))
}
await Promise.all(groupPromises)
}
await Promise.all(groupPromises)
}

return {
successful: saved,
unsuccessful,
return {
successful: saved,
unsuccessful,
}
}
})
)
}

static async bulkDelete(userIds: string[]): Promise<BulkUserDeleted> {
Expand Down Expand Up @@ -420,11 +433,12 @@ export class UserDB {
_deleted: true,
}))
const dbResponse = await usersCore.bulkUpdateGlobalUsers(toDelete)
const creatorsToDelete = usersToDelete.filter(isCreator)

await UserDB.quotas.removeUsers(toDelete.length)
for (let user of usersToDelete) {
await bulkDeleteProcessing(user)
}
await UserDB.quotas.removeUsers(toDelete.length, creatorsToDelete.length)

// Build Response
// index users by id
Expand Down Expand Up @@ -473,7 +487,8 @@ export class UserDB {

await db.remove(userId, dbUser._rev)

await UserDB.quotas.removeUsers(1)
const creatorsToDelete = isCreator(dbUser) ? 1 : 0
await UserDB.quotas.removeUsers(1, creatorsToDelete)
await eventHelpers.handleDeleteEvents(dbUser)
await cache.user.invalidateUser(userId)
await sessions.invalidateSessions(userId, { reason: "deletion" })
Expand Down
18 changes: 16 additions & 2 deletions packages/backend-core/src/users/users.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ import {
} from "../db"
import {
BulkDocsResponse,
ContextUser,
SearchQuery,
SearchQueryOperators,
SearchUsersRequest,
User,
ContextUser,
} from "@budibase/types"
import * as context from "../context"
import { getGlobalDB } from "../context"
import * as context from "../context"
import { isCreator } from "./utils"

type GetOpts = { cleanup?: boolean }

Expand Down Expand Up @@ -283,6 +284,19 @@ export async function getUserCount() {
return response.total_rows
}

export async function getCreatorCount() {
let creators = 0
async function iterate(startPage?: string) {
const page = await paginatedUsers({ bookmark: startPage })
creators += page.data.filter(isCreator).length
if (page.hasNextPage) {
await iterate(page.nextPage)
}
}
await iterate()
return creators
}

// used to remove the builder/admin permissions, for processing the
// user as an app user (they may have some specific role/group
export function removePortalUserPermissions(user: User | ContextUser) {
Expand Down
1 change: 1 addition & 0 deletions packages/backend-core/src/users/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { getAccountByTenantId } from "../accounts"
// extract from shared-core to make easily accessible from backend-core
export const isBuilder = sdk.users.isBuilder
export const isAdmin = sdk.users.isAdmin
export const isCreator = sdk.users.isCreator
export const isGlobalBuilder = sdk.users.isGlobalBuilder
export const isAdminOrBuilder = sdk.users.isAdminOrBuilder
export const hasAdminPermissions = sdk.users.hasAdminPermissions
Expand Down
54 changes: 54 additions & 0 deletions packages/backend-core/tests/core/users/users.spec.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
const _ = require('lodash/fp')
const {structures} = require("../../../tests")

jest.mock("../../../src/context")
jest.mock("../../../src/db")

const context = require("../../../src/context")
const db = require("../../../src/db")

const {getCreatorCount} = require('../../../src/users/users')

describe("Users", () => {

let getGlobalDBMock
let getGlobalUserParamsMock
let paginationMock

beforeEach(() => {
jest.resetAllMocks()

getGlobalDBMock = jest.spyOn(context, "getGlobalDB")
getGlobalUserParamsMock = jest.spyOn(db, "getGlobalUserParams")
paginationMock = jest.spyOn(db, "pagination")
})

it("Retrieves the number of creators", async () => {
const getUsers = (offset, limit, creators = false) => {
const range = _.range(offset, limit)
const opts = creators ? {builder: {global: true}} : undefined
return range.map(() => structures.users.user(opts))
}
const page1Data = getUsers(0, 8)
const page2Data = getUsers(8, 12, true)
getGlobalDBMock.mockImplementation(() => ({
name : "fake-db",
allDocs: () => ({
rows: [...page1Data, ...page2Data]
})
}))
paginationMock.mockImplementationOnce(() => ({
data: page1Data,
hasNextPage: true,
nextPage: "1"
}))
paginationMock.mockImplementation(() => ({
data: page2Data,
hasNextPage: false,
nextPage: undefined
}))
const creatorsCount = await getCreatorCount()
expect(creatorsCount).toBe(4)
expect(paginationMock).toHaveBeenCalledTimes(2)
})
})
13 changes: 13 additions & 0 deletions packages/backend-core/tests/core/utilities/structures/licenses.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ export function quotas(): Quotas {
value: 1,
triggers: [],
},
creators: {
name: "Creators",
value: 1,
triggers: [],
},
userGroups: {
name: "User Groups",
value: 1,
Expand Down Expand Up @@ -118,6 +123,10 @@ export function customer(): Customer {
export function subscription(): Subscription {
return {
amount: 10000,
amounts: {
user: 10000,
creator: 0,
},
cancelAt: undefined,
currency: "usd",
currentPeriodEnd: 0,
Expand All @@ -126,6 +135,10 @@ export function subscription(): Subscription {
duration: PriceDuration.MONTHLY,
pastDueAt: undefined,
quantity: 0,
quantities: {
user: 0,
creator: 0,
},
status: "active",
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { MonthlyQuotaName, QuotaUsage } from "@budibase/types"

export const usage = (): QuotaUsage => {
export const usage = (users: number = 0, creators: number = 0): QuotaUsage => {
return {
_id: "usage_quota",
quotaReset: new Date().toISOString(),
Expand Down Expand Up @@ -58,7 +58,8 @@ export const usage = (): QuotaUsage => {
usageQuota: {
apps: 0,
plugins: 0,
users: 0,
users,
creators,
userGroups: 0,
rows: 0,
triggers: {},
Expand Down
2 changes: 1 addition & 1 deletion packages/pro
Submodule pro updated from 044bec to 570d14
2 changes: 2 additions & 0 deletions packages/server/src/migrations/functions/syncQuotas.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import * as syncApps from "./usageQuotas/syncApps"
import * as syncRows from "./usageQuotas/syncRows"
import * as syncPlugins from "./usageQuotas/syncPlugins"
import * as syncUsers from "./usageQuotas/syncUsers"
import * as syncCreators from "./usageQuotas/syncCreators"

/**
* Synchronise quotas to the state of the db.
Expand All @@ -13,5 +14,6 @@ export const run = async () => {
await syncRows.run()
await syncPlugins.run()
await syncUsers.run()
await syncCreators.run()
})
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { users } from "@budibase/backend-core"
import { quotas } from "@budibase/pro"
import { QuotaUsageType, StaticQuotaName } from "@budibase/types"

export const run = async () => {
const creatorCount = await users.getCreatorCount()
console.log(`Syncing creator count: ${creatorCount}`)
await quotas.setUsage(
creatorCount,
StaticQuotaName.CREATORS,
QuotaUsageType.STATIC
)
}
Loading

0 comments on commit 5b2f55a

Please sign in to comment.