# frozen_string_literal: true require "forwardable" require_relative "./customer_plan" require_relative "./backend_sgx" require_relative "./ibr" require_relative "./payment_methods" require_relative "./plan" class Customer def self.for_jid(jid) REDIS.get("jmp_customer_id-#{jid}").then do |customer_id| raise "No customer id" unless customer_id for_customer_id(customer_id) end end def self.for_customer_id(customer_id) result = DB.query_defer(<<~SQL, [customer_id]) SELECT COALESCE(balance,0) AS balance, plan_name, expires_at FROM customer_plans LEFT JOIN balances USING (customer_id) WHERE customer_id=$1 LIMIT 1 SQL result.then do |rows| new(customer_id, **rows.first&.transform_keys(&:to_sym) || {}) end end extend Forwardable attr_reader :customer_id, :balance def_delegators :@plan, :active?, :activate_plan_starting_now, :bill_plan, :currency, :merchant_account, :plan_name def_delegators :@sgx, :register!, :registered? def initialize( customer_id, plan_name: nil, expires_at: Time.now, balance: BigDecimal.new(0), sgx: BackendSgx.new(customer_id) ) @plan = CustomerPlan.new( customer_id, plan: plan_name && Plan.for(plan_name), expires_at: expires_at ) @customer_id = customer_id @balance = balance @sgx = sgx end def with_plan(plan_name) self.class.new( @customer_id, balance: @balance, expires_at: expires_at, plan_name: plan_name ) end def payment_methods @payment_methods ||= BRAINTREE .customer .find(@customer_id) .then(PaymentMethods.method(:for_braintree_customer)) end protected def_delegator :@plan, :expires_at end