import { PostgreSQL } from "./types" ; import { callback2 } from "@cocalc/util/async-utils" ; import { len } from "@cocalc/util/misc" ; import { is_a_site_license_manager } from "./site-license/search" ; export async function is_paying_customer ( db , account_id ) { let x ; try { x = await callback2 ( db . get_account , { account_id , columns : [ "stripe_customer" ] , } ) ; } catch ( _err ) { return false ; } if ( ! ! x . stripe_customer ?. subscriptions ?. total_count ) { return true ; } return await is_a_site_license_manager ( db , account_id ) ; } export async function set_account_info_if_possible ( opts ) { const columns = [ "email_address" , "first_name" , "last_name" ] ; const account = await get_account ( opts . db , opts . account_id , columns ) ; const do_set = { } ; for ( const field of columns ) { if ( ! ! opts [ field ] && ! account [ field ] ) { do_set [ field ] = opts [ field ] ; } } if ( len ( do_set ) == 0 ) { return ; } await set_account ( opts . db , opts . account_id , do_set ) ; if ( do_set [ "email_address" ] ) { await callback2 ( opts . db . do_account_creation_actions . bind ( opts . db ) , { email_address : opts . email_address , account_id : opts . account_id , } ) ; } } export async function set_account ( db , account_id , set ) { await callback2 ( db . _query . bind ( db ) , { query : "UPDATE accounts" , where : { "account_id = $::UUID" : account_id } , set , } ) ; } export async function get_account ( db , account_id , columns ) { return await callback2 ( db . get_account . bind ( db ) , { account_id , columns , } ) ; }