UNPKG

@topgroup/diginext

Version:

A BUILD SERVER & CLI to deploy apps to any Kubernetes clusters.

43 lines (42 loc) 2.32 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.checkQuotaByWorkspaceId = exports.checkQuota = void 0; const lodash_1 = require("lodash"); const app_config_1 = require("../../app.config"); const AppService_1 = require("../../services/AppService"); const BuildService_1 = require("../../services/BuildService"); const ProjectService_1 = require("../../services/ProjectService"); const WorkspaceService_1 = require("../../services/WorkspaceService"); const dx_subscription_1 = require("../diginext/dx-subscription"); async function checkQuota(workspace, options = {}) { // SKIP on development and test environment if ((0, app_config_1.IsTest)() || (0, app_config_1.IsSuperAccount)(workspace.slug)) return { status: 1, data: { isExceed: false }, messages: ["Ok"] }; const { dx_key } = workspace; const { resourceSize } = options; const projectSvc = new ProjectService_1.ProjectService(); const appSvc = new AppService_1.AppService(); const buildSvc = new BuildService_1.BuildService(); const projects = await projectSvc.count({ workspace: workspace._id }); const apps = await appSvc.count({ workspace: workspace._id }); const concurrentBuilds = await buildSvc.count({ status: "building", workspace: workspace._id }); let containerSize; if (resourceSize) containerSize = resourceSize === "none" ? 0 : (0, lodash_1.toNumber)(resourceSize.substring(0, resourceSize.length - 1)); const checkQuotaParams = { projects, apps, concurrentBuilds, containerSize }; if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("checkQuota > checkQuotaParams :>> ", checkQuotaParams); const res = await (0, dx_subscription_1.dxCheckQuota)(checkQuotaParams, dx_key); if (options === null || options === void 0 ? void 0 : options.isDebugging) console.log("checkQuota > res :>> ", res); return res; } exports.checkQuota = checkQuota; async function checkQuotaByWorkspaceId(id) { const workspaceSvc = new WorkspaceService_1.WorkspaceService(); const workspace = await workspaceSvc.findOne({ _id: id }); if (!workspace) throw new Error(`Workspace not found`); return checkQuota(workspace); } exports.checkQuotaByWorkspaceId = checkQuotaByWorkspaceId;