customer_repo.rb

  1# frozen_string_literal: true
  2
  3require "lazy_object"
  4require "value_semantics/monkey_patched"
  5
  6require_relative "bandwidth_tn_repo"
  7require_relative "customer"
  8require_relative "polyfill"
  9
 10class CustomerRepo
 11	class NotFound < RuntimeError; end
 12
 13	value_semantics do
 14		redis             Anything(), default: LazyObject.new { REDIS }
 15		db                Anything(), default: LazyObject.new { DB }
 16		braintree         Anything(), default: LazyObject.new { BRAINTREE }
 17		sgx_repo          Anything(), default: TrivialBackendSgxRepo.new
 18		bandwidth_tn_repo Anything(), default: BandwidthTnRepo.new
 19	end
 20
 21	def find(customer_id)
 22		@redis.get("jmp_customer_jid-#{customer_id}").then do |jid|
 23			raise NotFound, "No jid" unless jid
 24
 25			find_inner(customer_id, jid)
 26		end
 27	end
 28
 29	def find_by_jid(jid)
 30		if jid.to_s =~ /\Acustomer_(.+)@#{CONFIG[:component][:jid]}\Z/
 31			find($1)
 32		else
 33			@redis.get("jmp_customer_id-#{jid}").then do |customer_id|
 34				raise NotFound, "No customer" unless customer_id
 35
 36				find_inner(customer_id, jid)
 37			end
 38		end
 39	end
 40
 41	def find_by_tel(tel)
 42		@redis.get("catapult_jid-#{tel}").then do |jid|
 43			raise NotFound, "No jid" unless jid
 44
 45			find_by_jid(jid)
 46		end
 47	end
 48
 49	def create(jid)
 50		@braintree.customer.create.then do |result|
 51			raise "Braintree customer create failed" unless result.success?
 52
 53			cid = result.customer.id
 54			@redis.msetnx(
 55				"jmp_customer_id-#{jid}", cid, "jmp_customer_jid-#{cid}", jid
 56			).then do |redis_result|
 57				raise "Saving new customer to redis failed" unless redis_result == 1
 58
 59				Customer.new(cid, Blather::JID.new(jid), sgx: new_sgx(cid))
 60			end
 61		end
 62	end
 63
 64	def put_lidb_name(customer, lidb_name)
 65		@bandwidth_tn_repo.put_lidb_name(customer.registered?.phone, lidb_name)
 66	end
 67
 68	def put_transcription_enabled(customer, transcription_enabled)
 69		@sgx_repo.put_transcription_enabled(
 70			customer.customer_id, transcription_enabled
 71		)
 72	end
 73
 74	def put_fwd(customer, customer_fwd)
 75		tel = customer.registered?.phone
 76		@sgx_repo.put_fwd(customer.customer_id, tel, customer_fwd)
 77	end
 78
 79protected
 80
 81	def new_sgx(customer_id)
 82		TrivialBackendSgxRepo.new.get(customer_id).with(registered?: false)
 83	end
 84
 85	def hydrate_plan(customer_id, raw_customer)
 86		raw_customer.dup.tap do |data|
 87			data[:plan] = CustomerPlan.new(
 88				customer_id,
 89				plan: data.delete(:plan_name)&.then(&Plan.method(:for)),
 90				expires_at: data.delete(:expires_at)
 91			)
 92		end
 93	end
 94
 95	SQL = <<~SQL
 96		SELECT COALESCE(balance,0) AS balance, plan_name, expires_at
 97		FROM customer_plans LEFT JOIN balances USING (customer_id)
 98		WHERE customer_id=$1 LIMIT 1
 99	SQL
100
101	def tndetails(sgx)
102		return unless sgx.registered?
103
104		LazyObject.new { @bandwidth_tn_repo.find(sgx.registered?.phone) }
105	end
106
107	def find_inner(customer_id, jid)
108		result = @db.query_defer(SQL, [customer_id])
109		EMPromise.all([@sgx_repo.get(customer_id), result]).then do |(sgx, rows)|
110			data = hydrate_plan(
111				customer_id, rows.first&.transform_keys(&:to_sym) || {}
112			)
113			Customer.new(
114				customer_id, Blather::JID.new(jid),
115				sgx: sgx, tndetails: tndetails(sgx), **data
116			)
117		end
118	end
119end