Allow setting data overage limit

Stephen Paul Weber created

Change summary

forms/plan_settings.rb | 14 +++++++++++++-
lib/customer_repo.rb   | 10 +++++-----
sgx_jmp.rb             | 20 ++++++++++++++------
3 files changed, 32 insertions(+), 12 deletions(-)

Detailed changes

forms/plan_settings.rb 🔗

@@ -6,7 +6,7 @@ instructions(
 	"Your plan includes #{@customer.message_limit} and " \
 	"#{@customer.minute_limit}.  JMP will always prompt for your explicit " \
 	"consent before allowing any action which would incur more overage in a " \
-	"calendar month than your configured limit below."
+	"calendar month than your configured limits below."
 )
 
 field(
@@ -19,3 +19,15 @@ field(
 		"than your monthly fee",
 	value: @customer.monthly_overage_limit.to_s
 )
+
+if @sims && !@sims.empty?
+	field(
+		var: "monthly_data_limit",
+		type: "text-single",
+		datatype: "xs:integer",
+		label: "Dollars of data charges to allow each month",
+		description:
+			"0 means you will never be automatically charged",
+		value: @data_limit
+	)
+end

lib/customer_repo.rb 🔗

@@ -125,9 +125,10 @@ class CustomerRepo
 		@sgx_repo.put_fwd(customer.customer_id, tel, customer_fwd)
 	end
 
-	def put_monthly_overage_limit(customer, limit)
-		k = "jmp_customer_monthly_overage_limit-#{customer.customer_id}"
-		@redis.set(k, limit)
+	def put_monthly_limits(customer, **kwargs)
+		@redis.mset(*kwargs.flat_map { |(k, v)|
+			["jmp_customer_#{k}-#{customer.customer_id}", v.to_i]
+		})
 	end
 
 	def change_jid(customer, new_jid)
@@ -194,8 +195,7 @@ protected
 		set_user.call(customer_id: cid, jid: jid)
 		EMPromise.all([
 			@sgx_repo.get(cid, tel: tel).then { |sgx| { sgx: sgx } },
-			@db.query_one(SQL, cid, default: {}),
-			fetch_redis(cid)
+			@db.query_one(SQL, cid, default: {}), fetch_redis(cid)
 		]).then { |all| all.reduce(&:merge) }.then do |data|
 			Customer.extract(cid, jid, tndetails: tndetails(data[:sgx]), **data)
 		end

sgx_jmp.rb 🔗

@@ -691,15 +691,23 @@ Command.new(
 	"📝 Manage your plan, including overage limits",
 	list_for: ->(customer:, **) { !!customer&.currency }
 ) {
-	Command.customer.then do |customer|
+	Command.customer.then { |customer|
+		EMPromise.all([
+			REDIS.get("jmp_customer_monthly_data_limit-#{customer.customer_id}"),
+			SIMRepo.new.owned_by(customer)
+		]).then { |(limit, sims)| [customer, sims, limit] }
+	}.then do |(customer, sims, limit)|
 		Command.reply { |reply|
 			reply.allowed_actions = [:next]
-			reply.command << FormTemplate.render("plan_settings", customer: customer)
-		}.then { |iq|
-			Command.execution.customer_repo.put_monthly_overage_limit(
-				customer,
-				iq.form.field("monthly_overage_limit")&.value.to_i
+			reply.command << FormTemplate.render(
+				"plan_settings", customer: customer, sims: sims, data_limit: limit
 			)
+		}.then { |iq|
+			kwargs = {
+				monthly_overage_limit: iq.form.field("monthly_overage_limit")&.value,
+				monthly_data_limit: iq.form.field("monthly_data_limit")&.value
+			}.compact
+			Command.execution.customer_repo.put_monthly_limits(customer, **kwargs)
 		}.then { Command.finish("Configuration saved!") }
 	end
 }.register(self).then(&CommandList.method(:register))