UNPKG

@cocalc/server

Version:

CoCalc server functionality: functions used by either the hub and the next.js server

53 lines (52 loc) 2.73 kB
"use strict"; var __importDefault = (this && this.__importDefault) || function (mod) { return (mod && mod.__esModule) ? mod : { "default": mod }; }; Object.defineProperty(exports, "__esModule", { value: true }); exports.cancelStripeEverything = exports.markAccountDeleted = void 0; const misc_1 = require("@cocalc/util/misc"); const pool_1 = __importDefault(require("@cocalc/database/pool")); const remember_me_1 = require("@cocalc/server/auth/remember-me"); const client_1 = require("@cocalc/server/stripe/client"); async function deleteAccount(account_id) { if (!(0, misc_1.isValidUUID)(account_id)) { throw Error(`invalid account_id=${account_id}`); } // Cancel any subscriptions await cancelStripeEverything(account_id); // Invalidate all sign ins (without this user can delete account, but could still be signed in). await (0, remember_me_1.deleteAllRememberMe)(account_id); // Mark the account as deleted -- do this last since once done, user is locked out. // Any step above could fail, and user could just try again in that case. await markAccountDeleted(account_id); } exports.default = deleteAccount; /* Mark the account as deleted, thus freeing up the email address and passports for use by another account, etc. The actual account entry remains in the database, since it may be referred to by many other things (projects, logs, etc.). However, the deleted field is set to true, so the account is excluded from user search and it is not possible to sign in to this account. We also save the email address from before deleting the account, since that would be very useful in case a user requests to undelete an account, or in case of bad users. TODO: Obviously, at some point we should permanently delete all PII from any deleted accounts, e.g., delete the email_address_before_delete field. */ async function markAccountDeleted(account_id) { const pool = (0, pool_1.default)(); const { rows } = await pool.query("SELECT email_address FROM accounts WHERE account_id=$1", [account_id]); if (rows.length == 0) { throw Error(`no account with account_id=${account_id}`); } const email_address_before_delete = rows[0].email_address ?? ""; await pool.query("UPDATE accounts SET deleted=true, email_address_before_delete=$1::TEXT, email_address=NULL, passports=NULL WHERE account_id=$2::UUID", [email_address_before_delete, account_id]); } exports.markAccountDeleted = markAccountDeleted; async function cancelStripeEverything(account_id) { // TODO const stripe = new client_1.StripeClient({ account_id }); await stripe.cancelEverything(); } exports.cancelStripeEverything = cancelStripeEverything; //# sourceMappingURL=delete.js.map