forked from tsai/budibase
17 changed files with 190 additions and 5 deletions
@ -0,0 +1,11 @@ |
|||||
|
class BudibaseError extends Error { |
||||
|
constructor(message, type, code) { |
||||
|
super(message) |
||||
|
this.type = type |
||||
|
this.code = code |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = { |
||||
|
BudibaseError, |
||||
|
} |
||||
@ -0,0 +1,15 @@ |
|||||
|
const licensing = require("./licensing") |
||||
|
|
||||
|
const codes = { |
||||
|
...licensing.codes, |
||||
|
} |
||||
|
|
||||
|
const types = { |
||||
|
...licensing.types, |
||||
|
} |
||||
|
|
||||
|
module.exports = { |
||||
|
codes, |
||||
|
types, |
||||
|
UsageLimitError: licensing.UsageLimitError, |
||||
|
} |
||||
@ -0,0 +1,22 @@ |
|||||
|
const { BudibaseError } = require("./base") |
||||
|
|
||||
|
const types = { |
||||
|
LICENSE_ERROR: "license_error", |
||||
|
} |
||||
|
|
||||
|
const codes = { |
||||
|
USAGE_LIMIT_EXCEEDED: "usage_limit_exceeded", |
||||
|
} |
||||
|
|
||||
|
class UsageLimitError extends BudibaseError { |
||||
|
constructor(message, limitName) { |
||||
|
super(message, types.LICENSE_ERROR, codes.USAGE_LIMIT_EXCEEDED) |
||||
|
this.limitName = limitName |
||||
|
} |
||||
|
} |
||||
|
|
||||
|
module.exports = { |
||||
|
types, |
||||
|
codes, |
||||
|
UsageLimitError, |
||||
|
} |
||||
@ -1,7 +1,9 @@ |
|||||
const middleware = require("./middleware") |
const middleware = require("./middleware") |
||||
const cache = require("./cache") |
const cache = require("./cache") |
||||
|
const usage = require("./usage") |
||||
|
|
||||
module.exports = { |
module.exports = { |
||||
middleware, |
middleware, |
||||
cache, |
cache, |
||||
|
usage, |
||||
} |
} |
||||
|
|||||
@ -0,0 +1,41 @@ |
|||||
|
const { getTenantId } = require("../../context") |
||||
|
const cache = require("../cache") |
||||
|
const env = require("../../environment") |
||||
|
const utils = require("../../utils") |
||||
|
const { UsageLimitError } = require("../../errors") |
||||
|
|
||||
|
const UsageType = { |
||||
|
MONTHLY: "monthly", |
||||
|
STATIC: "static", |
||||
|
} |
||||
|
|
||||
|
const StaticUsageLimits = { |
||||
|
MAX_DEVELOPERS: "maxDevelopers", |
||||
|
QUERY_TIMEOUT_SECONDS: "queryTimeoutSeconds", |
||||
|
} |
||||
|
|
||||
|
// eslint-disable-next-line no-unused-vars
|
||||
|
const MonthlyUsageLimits = { |
||||
|
MAX_QUERY_COUNT: "maxQueryCount", |
||||
|
} |
||||
|
|
||||
|
exports.checkMaxDevelopers = async () => { |
||||
|
const developerCount = await utils.getBuildersCount() |
||||
|
await checkUsageLimit( |
||||
|
developerCount, |
||||
|
UsageType.STATIC, |
||||
|
StaticUsageLimits.MAX_DEVELOPERS |
||||
|
) |
||||
|
} |
||||
|
|
||||
|
const checkUsageLimit = async (currentUsage, usageType, usageLimit) => { |
||||
|
const tenantId = getTenantId() |
||||
|
const license = await cache.getLicense(tenantId) |
||||
|
const limit = license.usageLimits[usageType][usageLimit] |
||||
|
if (currentUsage >= limit.value) { |
||||
|
throw new UsageLimitError( |
||||
|
`Licensed ${limit.name} has been exceeded. To upgrade, visit ${env.ACCOUNT_PORTAL_URL}/portal/plans`, |
||||
|
limit.name |
||||
|
) |
||||
|
} |
||||
|
} |
||||
@ -0,0 +1,13 @@ |
|||||
|
const { createUserBuildersView } = require("@budibase/backend-core/db") |
||||
|
|
||||
|
/** |
||||
|
* Date: |
||||
|
* March 2022 |
||||
|
* |
||||
|
* Description: |
||||
|
* Create the builder users view. |
||||
|
*/ |
||||
|
|
||||
|
export const run = async (db: any) => { |
||||
|
await createUserBuildersView(db) |
||||
|
} |
||||
Loading…
Reference in new issue