UNPKG

@cocalc/database

Version:

CoCalc: code for working with our PostgreSQL database

109 lines 4.55 kB
"use strict"; /* * This file is part of CoCalc: Copyright © 2020 Sagemath, Inc. * License: AGPLv3 s.t. "Commons Clause" – see LICENSE.md for details */ Object.defineProperty(exports, "__esModule", { value: true }); exports.set_email_address_verified = exports.get_account = exports.set_account = exports.set_account_info_if_different = exports.set_account_info_if_not_set = exports.is_paying_customer = void 0; // Various functions involving the database and accounts. const async_utils_1 = require("@cocalc/util/async-utils"); const misc_1 = require("@cocalc/util/misc"); const search_1 = require("./site-license/search"); //import getLogger from "@cocalc/backend/logger"; //const L = getLogger("db:pg:account-queries"); /* 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... */ async function is_paying_customer(db, account_id) { let x; try { x = await (0, async_utils_1.callback2)(db.get_account, { account_id, columns: ["stripe_customer"], }); } catch (_err) { // error probably means there is no such account or account_id is badly formatted. return false; } if (!!x.stripe_customer?.subscriptions?.total_count) { // they have at least one subscription of some form -- so that's enough to count. return true; } // If they manage any licenses then they also count: return await (0, search_1.is_a_site_license_manager)(db, account_id); } exports.is_paying_customer = is_paying_customer; // this is like set_account_info_if_different, but only sets the fields if they're not set async function set_account_info_if_not_set(opts) { return await set_account_info_if_different(opts, false); } exports.set_account_info_if_not_set = set_account_info_if_not_set; // This sets the given fields of an account, if it is different from the current value – except for the email address, which we only set but not change async function set_account_info_if_different(opts, overwrite = true) { const columns = ["email_address", "first_name", "last_name"]; // this could throw an error for "no such account" const account = await get_account(opts.db, opts.account_id, columns); const do_set = {}; let do_email = undefined; for (const field of columns) { if (typeof opts[field] !== "string") continue; if (!overwrite && account[field] != null) continue; if (account[field] != opts[field]) { if (field === "email_address") { do_email = opts[field]; } else { do_set[field] = opts[field]; } } } if ((0, misc_1.len)(do_set) > 0) { await set_account(opts.db, opts.account_id, do_set); } if (do_email) { if (account["email_address"] != null) { // if it changes, we have to call the change_email_address function await (0, async_utils_1.callback2)(opts.db.change_email_address.bind(opts.db), { account_id: opts.account_id, email_address: do_email, }); } // Just changed email address - might be added to a project... await (0, async_utils_1.callback2)(opts.db.do_account_creation_actions.bind(opts.db), { email_address: do_email, account_id: opts.account_id, }); } } exports.set_account_info_if_different = set_account_info_if_different; async function set_account(db, account_id, set) { await db.async_query({ query: "UPDATE accounts", where: { "account_id = $::UUID": account_id }, set, }); } exports.set_account = set_account; async function get_account(db, account_id, columns) { return await (0, async_utils_1.callback2)(db.get_account.bind(db), { account_id, columns, }); } exports.get_account = get_account; async function set_email_address_verified(opts) { const { db, account_id, email_address } = opts; (0, misc_1.assert_valid_account_id)(account_id); (0, misc_1.assert_valid_email_address)(email_address); return await db.async_query({ query: "UPDATE accounts", jsonb_set: { email_address_verified: { [email_address]: new Date() } }, where: { "account_id = $::UUID": account_id }, }); } exports.set_email_address_verified = set_email_address_verified; //# sourceMappingURL=account-queries.js.map