UNPKG

smc-hub

Version:

CoCalc: Backend webserver component

178 lines 8.94 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) { function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); } return new (P || (P = Promise))(function (resolve, reject) { function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } } function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } } function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); } step((generator = generator.apply(thisArg, _arguments || [])).next()); }); }; var __generator = (this && this.__generator) || function (thisArg, body) { var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g; return g = { next: verb(0), "throw": verb(1), "return": verb(2) }, typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g; function verb(n) { return function (v) { return step([n, v]); }; } function step(op) { if (f) throw new TypeError("Generator is already executing."); while (_) try { if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t; if (y = 0, t) op = [op[0] & 2, t.value]; switch (op[0]) { case 0: case 1: t = op; break; case 4: _.label++; return { value: op[1], done: false }; case 5: _.label++; y = op[1]; op = [0]; continue; case 7: op = _.ops.pop(); _.trys.pop(); continue; default: if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; } if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; } if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; } if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; } if (t[2]) _.ops.pop(); _.trys.pop(); continue; } op = body.call(thisArg, _); } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; } if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true }; } }; var __values = (this && this.__values) || function(o) { var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0; if (m) return m.call(o); if (o && typeof o.length === "number") return { next: function () { if (o && i >= o.length) o = void 0; return { value: o && o[i++], done: !o }; } }; throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined."); }; Object.defineProperty(exports, "__esModule", { value: true }); exports.get_account = exports.set_account = exports.set_account_info_if_possible = exports.is_paying_customer = void 0; var async_utils_1 = require("smc-util/async-utils"); var misc_1 = require("smc-util/misc"); var search_1 = require("./site-license/search"); /* For now we define "paying customer" to mean they have a subscription. It's OK if it expired. They at least bought one once. This is mainly used for anti-abuse purposes... */ function is_paying_customer(db, account_id) { var _a, _b; return __awaiter(this, void 0, void 0, function () { var x, _err_1; return __generator(this, function (_c) { switch (_c.label) { case 0: _c.trys.push([0, 2, , 3]); return [4 /*yield*/, async_utils_1.callback2(db.get_account, { account_id: account_id, columns: ["stripe_customer"], })]; case 1: x = _c.sent(); return [3 /*break*/, 3]; case 2: _err_1 = _c.sent(); // error probably means there is no such account or account_id is badly formatted. return [2 /*return*/, false]; case 3: if (!!((_b = (_a = x.stripe_customer) === null || _a === void 0 ? void 0 : _a.subscriptions) === null || _b === void 0 ? void 0 : _b.total_count)) { // they have at least one subscription of some form -- so that's enough to count. return [2 /*return*/, true]; } return [4 /*yield*/, search_1.is_a_site_license_manager(db, account_id)]; case 4: // If they manage any licenses then they also count: return [2 /*return*/, _c.sent()]; } }); }); } exports.is_paying_customer = is_paying_customer; function set_account_info_if_possible(opts) { return __awaiter(this, void 0, void 0, function () { var columns, account, do_set, columns_1, columns_1_1, field; var e_1, _a; return __generator(this, function (_b) { switch (_b.label) { case 0: columns = ["email_address", "first_name", "last_name"]; return [4 /*yield*/, get_account(opts.db, opts.account_id, columns)]; case 1: account = _b.sent(); do_set = {}; try { for (columns_1 = __values(columns), columns_1_1 = columns_1.next(); !columns_1_1.done; columns_1_1 = columns_1.next()) { field = columns_1_1.value; if (!!opts[field] && !account[field]) { do_set[field] = opts[field]; } } } catch (e_1_1) { e_1 = { error: e_1_1 }; } finally { try { if (columns_1_1 && !columns_1_1.done && (_a = columns_1.return)) _a.call(columns_1); } finally { if (e_1) throw e_1.error; } } if (misc_1.len(do_set) == 0) { // nothing to change return [2 /*return*/]; } return [4 /*yield*/, set_account(opts.db, opts.account_id, do_set)]; case 2: _b.sent(); if (!do_set["email_address"]) return [3 /*break*/, 4]; // Just changed email address - might be added to a project... // Note -- this functionality will go away completely and be replaced by "add codes"... return [4 /*yield*/, async_utils_1.callback2(opts.db.do_account_creation_actions.bind(opts.db), { email_address: opts.email_address, account_id: opts.account_id, })]; case 3: // Just changed email address - might be added to a project... // Note -- this functionality will go away completely and be replaced by "add codes"... _b.sent(); _b.label = 4; case 4: return [2 /*return*/]; } }); }); } exports.set_account_info_if_possible = set_account_info_if_possible; function set_account(db, account_id, set) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db._query.bind(db), { query: "UPDATE accounts", where: { "account_id = $::UUID": account_id }, set: set, })]; case 1: _a.sent(); return [2 /*return*/]; } }); }); } exports.set_account = set_account; function get_account(db, account_id, columns) { return __awaiter(this, void 0, void 0, function () { return __generator(this, function (_a) { switch (_a.label) { case 0: return [4 /*yield*/, async_utils_1.callback2(db.get_account.bind(db), { account_id: account_id, columns: columns, })]; case 1: return [2 /*return*/, _a.sent()]; } }); }); } exports.get_account = get_account; //# sourceMappingURL=account-queries.js.map