@cto.ai/ops
Version:
💻 CTO.ai - The CLI built for Teams 🚀
76 lines (75 loc) • 3.34 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.BillingService = void 0;
const tslib_1 = require("tslib");
const debug_1 = tslib_1.__importDefault(require("debug"));
const axios_1 = tslib_1.__importDefault(require("axios"));
const env_1 = require("./../constants/env");
const CustomErrors_1 = require("./../errors/CustomErrors");
const open_1 = tslib_1.__importDefault(require("open"));
const cli_sdk_1 = require("@cto.ai/cli-sdk");
const debug = (0, debug_1.default)('ops:BillingService');
class BillingService {
constructor() {
this.hasPublishAccess = async (teamName, workflowType, workflowName, email, username, token) => {
try {
// any failure of this request will throw and block publishing, intentionally
const res = await axios_1.default.post(`${env_1.BILLING_HOST}/v1/workflows/prepublish`, { teamName, workflowType, workflowName, email, username }, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
return res.data;
}
catch (err) {
debug('prepublish validation failed', err);
return false;
}
};
this.validateBilling = async (teamName, workflowTypeIn, workflowName, email, username, token) => {
cli_sdk_1.ux.spinner.start(`💡 ${cli_sdk_1.ux.colors.white(`Checking the billing status for team:`)} ${cli_sdk_1.ux.colors.callOutCyan(`${teamName}`)}`);
let workflowType;
switch (workflowTypeIn) {
case 'workflow':
workflowType = 'Pipelines';
break;
case 'pipeline':
workflowType = 'Pipelines';
break;
case 'service':
workflowType = 'Services';
break;
case 'command':
workflowType = 'Commands';
break;
default:
throw new Error(`unrecognized workflow type ${workflowTypeIn}`);
}
const stopSpinner = () => {
cli_sdk_1.ux.spinner.stop(`${cli_sdk_1.ux.colors.successGreen('Done')}`);
};
let access = await this.hasPublishAccess(teamName, workflowType, workflowName, email, username, token);
debug('billing', access);
if (!access.hasPublishAccess) {
stopSpinner();
(0, open_1.default)(`${env_1.WWW_HOST}/home/teams/${teamName}/checkout`);
throw new CustomErrors_1.NeedsPaymentMethod(access, env_1.WWW_HOST);
}
stopSpinner();
return access;
};
this.updateBilling = async (teamName, username, email, token) => {
await axios_1.default.put(`${env_1.BILLING_HOST}/v1/teams/subscriptions/workflows/${teamName}`, {
email,
username,
}, {
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${token}`,
},
});
};
}
}
exports.BillingService = BillingService;