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 0692263960
commit f877244571
6 changed files with 45 additions and 25 deletions

View File

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

View File

@ -368,8 +368,33 @@ async function getScopedConfig(db, params) {
return configDoc && configDoc.config ? configDoc.config : configDoc 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.Replication = Replication
exports.getScopedConfig = getScopedConfig exports.getScopedConfig = getScopedConfig
exports.generateConfigID = generateConfigID exports.generateConfigID = generateConfigID
exports.getConfigParams = getConfigParams exports.getConfigParams = getConfigParams
exports.getScopedFullConfig = getScopedFullConfig exports.getScopedFullConfig = getScopedFullConfig
exports.generateNewUsageQuotaDoc = generateNewUsageQuotaDoc

View File

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

View File

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

View File

@ -1,5 +1,9 @@
const env = require("../environment") const env = require("../environment")
const { getGlobalDB } = require("@budibase/auth/tenancy") const { getGlobalDB } = require("@budibase/auth/tenancy")
const {
StaticDatabases,
generateNewUsageQuotaDoc,
} = require("@budibase/auth/db")
function getNewQuotaReset() { function getNewQuotaReset() {
return Date.now() + 2592000000 return Date.now() + 2592000000
@ -15,6 +19,18 @@ exports.Properties = {
EMAILS: "emails", 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. * 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). * @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 { try {
const db = getGlobalDB() const db = getGlobalDB()
const quota = await db.get("usage_quota") const quota = await getUsageQuotaDoc(db)
// Check if the quota needs reset // Check if the quota needs reset
if (Date.now() >= quota.quotaReset) { if (Date.now() >= quota.quotaReset) {

View File

@ -2,6 +2,7 @@ const {
generateGlobalUserID, generateGlobalUserID,
getGlobalUserParams, getGlobalUserParams,
StaticDatabases, StaticDatabases,
generateNewUsageQuotaDoc,
} = require("@budibase/auth/db") } = require("@budibase/auth/db")
const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils const { hash, getGlobalUserByEmail } = require("@budibase/auth").utils
const { UserStatus, EmailTemplatePurpose } = require("../../../constants") const { UserStatus, EmailTemplatePurpose } = require("../../../constants")
@ -141,27 +142,7 @@ exports.adminUser = async ctx => {
// write usage quotas for cloud // write usage quotas for cloud
if (!env.SELF_HOSTED) { if (!env.SELF_HOSTED) {
await db.post({ await db.post(generateNewUsageQuotaDoc())
_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,
},
})
} }
if (response.rows.some(row => row.doc.admin)) { if (response.rows.some(row => row.doc.admin)) {