From 36182e5f1a70061eb52a895076a469cc5e0b22f9 Mon Sep 17 00:00:00 2001 From: Christopher Vollick <0@psycoti.ca> Date: Tue, 7 Jun 2022 11:24:25 -0400 Subject: [PATCH] ResetDeclines Command This is a nice simple first one. I set the declines to 0, and on Undo I set it back to what it was before. There's no extra form or information, it just does the thing. --- forms/admin_menu.rb | 3 +- lib/admin_actions/reset_declines.rb | 44 +++++++++++++++++++++++++++++ lib/admin_command.rb | 4 ++- lib/customer_finacials.rb | 8 ++++++ 4 files changed, 57 insertions(+), 2 deletions(-) create mode 100644 lib/admin_actions/reset_declines.rb diff --git a/forms/admin_menu.rb b/forms/admin_menu.rb index 650642e..dc65dca 100644 --- a/forms/admin_menu.rb +++ b/forms/admin_menu.rb @@ -12,6 +12,7 @@ field( { value: "financial", label: "Customer Billing Information" }, { value: "bill_plan", label: "Bill Customer" }, { value: "cancel_account", label: "Cancel Customer" }, - { value: "undo", label: "Undo" } + { value: "undo", label: "Undo" }, + { value: "reset_declines", label: "Reset Declines" } ] ) diff --git a/lib/admin_actions/reset_declines.rb b/lib/admin_actions/reset_declines.rb new file mode 100644 index 0000000..57bf2ee --- /dev/null +++ b/lib/admin_actions/reset_declines.rb @@ -0,0 +1,44 @@ +# frozen_string_literal: true + +require "value_semantics/monkey_patched" +require_relative "../admin_action" + +class AdminAction + class ResetDeclines < AdminAction + class Command + def self.for(target_customer, **) + target_customer.declines.then { |declines| + AdminAction::ResetDeclines.for( + customer_id: target_customer.customer_id, + previous_value: declines + ) + } + end + end + + def customer_id + @attributes[:customer_id] + end + + def previous_value + @attributes[:previous_value].to_i + end + + def forward + CustomerFinancials.new(customer_id).set_declines(0).then { self } + end + + # I could make sure here that they're still set to 0 in the reverse case, so + # I know there haven't been any declines since I ran the command, but I + # think I don't care actually, and I should just set it back to what it was + # and trust the human knows what they're doing + def reverse + CustomerFinancials.new(customer_id).set_declines(previous_value) + .then { self } + end + + def to_s + "reset_declines(#{customer_id}): #{previous_value} -> 0" + end + end +end diff --git a/lib/admin_command.rb b/lib/admin_command.rb index 955db83..8e721c6 100644 --- a/lib/admin_command.rb +++ b/lib/admin_command.rb @@ -3,6 +3,7 @@ require_relative "admin_action_repo" require_relative "admin_actions/cancel" require_relative "admin_actions/financial" +require_relative "admin_actions/reset_declines" require_relative "bill_plan_command" require_relative "customer_info_form" require_relative "financial_info" @@ -174,7 +175,8 @@ class AdminCommand [ [:cancel_account, Simple.new(AdminAction::CancelCustomer)], [:financial, Simple.new(AdminAction::Financial)], - [:undo, Undoable.new(Undo)] + [:undo, Undoable.new(Undo)], + [:reset_declines, Undoable.new(AdminAction::ResetDeclines::Command)] ].each do |action, handler| define_method("action_#{action}") do handler.call( diff --git a/lib/customer_finacials.rb b/lib/customer_finacials.rb index 07c33ea..8ff43d4 100644 --- a/lib/customer_finacials.rb +++ b/lib/customer_finacials.rb @@ -42,6 +42,14 @@ class CustomerFinancials end end + def set_declines(num) + if num.positive? + REDIS.set("jmp_pay_decline-#{@customer_id}", num) + else + REDIS.del("jmp_pay_decline-#{@customer_id}") + end + end + class TransactionInfo value_semantics do transaction_id String -- 2.34.5