"use strict";
(() => {
var exports = {};
exports.id = 9770;
exports.ids = [9770];
exports.modules = {

/***/ 439491:
/***/ ((module) => {

module.exports = require("assert");

/***/ }),

/***/ 714300:
/***/ ((module) => {

module.exports = require("buffer");

/***/ }),

/***/ 532081:
/***/ ((module) => {

module.exports = require("child_process");

/***/ }),

/***/ 435001:
/***/ ((module) => {

module.exports = require("cluster");

/***/ }),

/***/ 722057:
/***/ ((module) => {

module.exports = require("constants");

/***/ }),

/***/ 706113:
/***/ ((module) => {

module.exports = require("crypto");

/***/ }),

/***/ 809523:
/***/ ((module) => {

module.exports = require("dns");

/***/ }),

/***/ 582361:
/***/ ((module) => {

module.exports = require("events");

/***/ }),

/***/ 657147:
/***/ ((module) => {

module.exports = require("fs");

/***/ }),

/***/ 773292:
/***/ ((module) => {

module.exports = require("fs/promises");

/***/ }),

/***/ 113685:
/***/ ((module) => {

module.exports = require("http");

/***/ }),

/***/ 795687:
/***/ ((module) => {

module.exports = require("https");

/***/ }),

/***/ 241808:
/***/ ((module) => {

module.exports = require("net");

/***/ }),

/***/ 822037:
/***/ ((module) => {

module.exports = require("os");

/***/ }),

/***/ 371017:
/***/ ((module) => {

module.exports = require("path");

/***/ }),

/***/ 504074:
/***/ ((module) => {

module.exports = require("perf_hooks");

/***/ }),

/***/ 677282:
/***/ ((module) => {

module.exports = require("process");

/***/ }),

/***/ 514521:
/***/ ((module) => {

module.exports = require("readline");

/***/ }),

/***/ 12781:
/***/ ((module) => {

module.exports = require("stream");

/***/ }),

/***/ 371576:
/***/ ((module) => {

module.exports = require("string_decoder");

/***/ }),

/***/ 924404:
/***/ ((module) => {

module.exports = require("tls");

/***/ }),

/***/ 76224:
/***/ ((module) => {

module.exports = require("tty");

/***/ }),

/***/ 257310:
/***/ ((module) => {

module.exports = require("url");

/***/ }),

/***/ 473837:
/***/ ((module) => {

module.exports = require("util");

/***/ }),

/***/ 984655:
/***/ ((module) => {

module.exports = require("v8");

/***/ }),

/***/ 959796:
/***/ ((module) => {

module.exports = require("zlib");

/***/ }),

/***/ 539879:
/***/ ((__unused_webpack_module, __webpack_exports__, __webpack_require__) => {

__webpack_require__.r(__webpack_exports__);
/* harmony export */ __webpack_require__.d(__webpack_exports__, {
/* harmony export */   "default": () => (/* binding */ handle)
/* harmony export */ });
/* harmony import */ var lib_account_get_account__WEBPACK_IMPORTED_MODULE_0__ = __webpack_require__(559459);
/* harmony import */ var _cocalc_server_accounts_delete__WEBPACK_IMPORTED_MODULE_1__ = __webpack_require__(872421);
/* harmony import */ var _cocalc_server_accounts_delete__WEBPACK_IMPORTED_MODULE_1___default = /*#__PURE__*/__webpack_require__.n(_cocalc_server_accounts_delete__WEBPACK_IMPORTED_MODULE_1__);
/*
Delete the account that the user is currently signed in using.
*/ 

async function handle(req, res) {
    try {
        const account_id = await (0,lib_account_get_account__WEBPACK_IMPORTED_MODULE_0__/* ["default"] */ .Z)(req);
        if (!account_id) {
            throw Error("must be signed in");
        }
        await _cocalc_server_accounts_delete__WEBPACK_IMPORTED_MODULE_1___default()(account_id);
        res.json({});
    } catch (err) {
        res.json({
            error: err.message
        });
    }
}


/***/ }),

/***/ 872421:
/***/ (function(__unused_webpack_module, exports, __webpack_require__) {


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 = __webpack_require__(854006);
const pool_1 = __importDefault(__webpack_require__(541808));
const remember_me_1 = __webpack_require__(820540);
const client_1 = __webpack_require__(946911);
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

/***/ })

};
;

// load runtime
var __webpack_require__ = require("../../../../webpack-api-runtime.js");
__webpack_require__.C(exports);
var __webpack_exec__ = (moduleId) => (__webpack_require__(__webpack_require__.s = moduleId))
var __webpack_exports__ = __webpack_require__.X(0, [6918,1199,8180,2921,9557,5245,6652,6942,5517,6099,5751,2739,9295,8759,2003,1808,4006,8312,5710,9459,4181,4557,7896,6177,7710,7877,6911], () => (__webpack_exec__(539879)));
module.exports = __webpack_exports__;

})();