import { is_valid_uuid_string as isValidUUID } from "@cocalc/util/misc" ; import getPool from "@cocalc/database/pool" ; import { deleteAllRememberMe } from "@cocalc/server/auth/remember-me" ; import { StripeClient } from "@cocalc/server/stripe/client" ; export default async function deleteAccount ( account_id ) { if ( ! isValidUUID ( account_id ) ) { throw Error ( `invalid account_id=${ account_id }` ) ; } await cancelStripeEverything ( account_id ) ; await deleteAllRememberMe ( account_id ) ; await markAccountDeleted ( account_id ) ; } export async function markAccountDeleted ( account_id ) { const pool = getPool ( ) ; 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 ] ) ; } export async function cancelStripeEverything ( account_id ) { const stripe = new StripeClient ( { account_id } ) ; await stripe . cancelEverything ( ) ; }