Allow injecting a customer repo into Web

Stephen Paul Weber created

Change summary

web.rb | 19 +++++++++++--------
1 file changed, 11 insertions(+), 8 deletions(-)

Detailed changes

web.rb 🔗

@@ -97,6 +97,10 @@ class Web < Roda
 		end
 	end
 
+	def customer_repo(**kwargs)
+		opts[:customer_repo] || CustomerRepo.new(**kwargs)
+	end
+
 	TEL_CANDIDATES = {
 		"Restricted" => "14",
 		"anonymous" => "15",
@@ -152,7 +156,7 @@ class Web < Roda
 							end
 						end
 
-						CustomerRepo.new.find_by_tel(params["to"]).then do |customer|
+						customer_repo.find_by_tel(params["to"]).then do |customer|
 							CDR.for_inbound(customer.customer_id, params).save
 						end
 					end
@@ -186,7 +190,7 @@ class Web < Roda
 								"https://jmp.chat"
 							)
 
-							CustomerRepo.new.find_by_tel(params["to"]).then do |customer|
+							customer_repo.find_by_tel(params["to"]).then do |customer|
 								m = Blather::Stanza::Message.new
 								m.chat_state = nil
 								m.from = from_jid
@@ -204,7 +208,7 @@ class Web < Roda
 							           Time.parse(params["startTime"])
 							next "OK<5" unless duration > 5
 
-							CustomerRepo.new.find_by_tel(params["to"]).then do |customer|
+							customer_repo.find_by_tel(params["to"]).then do |customer|
 								m = Blather::Stanza::Message.new
 								m.chat_state = nil
 								m.from = from_jid
@@ -219,8 +223,7 @@ class Web < Roda
 						end
 
 						r.post do
-							CustomerRepo
-								.new(sgx_repo: Bwmsgsv2Repo.new)
+							customer_repo(sgx_repo: Bwmsgsv2Repo.new)
 								.find_by_tel(params["to"])
 								.then { |c|
 									EMPromise.all([c, c.ogm(params["from"])])
@@ -239,7 +242,7 @@ class Web < Roda
 				end
 
 				r.post do
-					CustomerRepo.new(
+					customer_repo(
 						sgx_repo: Bwmsgsv2Repo.new
 					).find_by_tel(params["to"]).then(&:fwd).then do |fwd|
 						call = fwd.create_call(CONFIG[:creds][:account]) { |cc|
@@ -272,7 +275,7 @@ class Web < Roda
 
 				r.post do
 					customer_id = params["from"].sub(/^\+1/, "")
-					CustomerRepo.new(
+					customer_repo(
 						sgx_repo: Bwmsgsv2Repo.new
 					).find(customer_id).then do |c|
 						render :forward, locals: {
@@ -298,7 +301,7 @@ class Web < Roda
 				ogm.download.then do
 					File.rename(ogm.path, "#{CONFIG[:ogm_path]}/#{ogm.cid}")
 					File.chmod(0o644, "#{CONFIG[:ogm_path]}/#{ogm.cid}")
-					CustomerRepo.new.find(params["customer_id"]).then do |customer|
+					customer_repo.find(params["customer_id"]).then do |customer|
 						customer.set_ogm_url("#{CONFIG[:ogm_web_root]}/#{ogm.cid}.mp3")
 					end
 				end