abstract usagedoc creation into auth, create the doc if it doesn't exist

This commit is contained in:
Martin McKeaveney 2021-09-27 17:35:22 +01:00
parent f6014190c8
commit 1e3fca15c5
6 changed files with 45 additions and 25 deletions

View File

@ -12,6 +12,7 @@ exports.StaticDatabases = {
name: "global-info",
docs: {
tenants: "tenants",
usageQuota: "usage_quota",
},
},
}

View File

@ -368,8 +368,33 @@ async function getScopedConfig(db, params) {
return configDoc && configDoc.config ? configDoc.config : configDoc
}
function generateNewUsageQuotaDoc() {
return {
_id: StaticDatabases.PLATFORM_INFO.docs.usageQuota,
quotaReset: Date.now() + 2592000000,
usageQuota: {
automationRuns: 0,
rows: 0,
storage: 0,
apps: 0,
users: 0,
views: 0,
emails: 0,
},
usageLimits: {
automationRuns: 1000,
rows: 4000,
apps: 4,
storage: 1000,
users: 10,
emails: 50,
},
}
}
exports.Replication = Replication
exports.getScopedConfig = getScopedConfig
exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams
exports.getScopedFullConfig = getScopedFullConfig
exports.generateNewUsageQuotaDoc = generateNewUsageQuotaDoc

View File

@ -27,13 +27,11 @@ router
.post(
"/api/users/metadata/self",
authorized(PermissionTypes.USER, PermissionLevels.WRITE),
// usage,
controller.updateSelfMetadata
)
.delete(
"/api/users/metadata/:id",
authorized(PermissionTypes.USER, PermissionLevels.WRITE),
// usage,
controller.destroyMetadata
)

View File

@ -8,7 +8,6 @@ const {
PermissionTypes,
PermissionLevels,
} = require("@budibase/auth/permissions")
const usage = require("../../middleware/usageQuota")
const router = Router()

View File

@ -1,5 +1,9 @@
const env = require("../environment")
const { getGlobalDB } = require("@budibase/auth/tenancy")
const {
StaticDatabases,
generateNewUsageQuotaDoc,
} = require("@budibase/auth/db")
function getNewQuotaReset() {
return Date.now() + 2592000000
@ -15,6 +19,18 @@ exports.Properties = {
EMAILS: "emails",
}
async function getUsageQuotaDoc(db) {
let quota
try {
quota = await db.get(StaticDatabases.PLATFORM_INFO.docs.usageQuota)
} catch (err) {
// doc doesn't exist. Create it
quota = await db.post(generateNewUsageQuotaDoc())
}
return quota
}
/**
* Given a specified tenantId this will add to the usage object for the specified property.
* @param {string} property The property which is to be added to (within the nested usageQuota object).
@ -29,7 +45,7 @@ exports.update = async (property, usage) => {
try {
const db = getGlobalDB()
const quota = await db.get("usage_quota")
const quota = await getUsageQuotaDoc(db)
// Check if the quota needs reset
if (Date.now() >= quota.quotaReset) {

View File

@ -2,6 +2,7 @@ const {
generateGlobalUserID,
getGlobalUserParams,
StaticDatabases,
generateNewUsageQuotaDoc,
} = require("@budibase/auth/db")
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
@ -141,27 +142,7 @@ exports.adminUser = async ctx => {
// write usage quotas for cloud
if (!env.SELF_HOSTED) {
await db.post({
_id: "usage_quota",
quotaReset: Date.now() + 2592000000,
usageQuota: {
automationRuns: 0,
rows: 0,
storage: 0,
apps: 0,
users: 0,
views: 0,
emails: 0,
},
usageLimits: {
automationRuns: 1000,
rows: 4000,
apps: 4,
storage: 1000,
users: 10,
emails: 50,
},
})
await db.post(generateNewUsageQuotaDoc())
}
if (response.rows.some(row => row.doc.admin)) {