diff --git a/lib/admin_actions/cancel.rb b/lib/admin_actions/cancel.rb new file mode 100644 index 0000000000000000000000000000000000000000..a45d9f32882d52404e7a80d688dcaece3d31eedf --- /dev/null +++ b/lib/admin_actions/cancel.rb @@ -0,0 +1,18 @@ +# frozen_string_literal: true + +class AdminAction + class CancelCustomer + def self.call(customer, customer_repo:, **) + m = Blather::Stanza::Message.new + m.from = CONFIG[:notify_from] + m.body = "Your JMP account has been cancelled." + customer.stanza_to(m).then { + EMPromise.all([ + customer.stanza_to(Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!)), + customer.deregister!, + customer_repo.disconnect_tel(customer) + ]) + } + end + end +end diff --git a/lib/admin_actions/financial.rb b/lib/admin_actions/financial.rb new file mode 100644 index 0000000000000000000000000000000000000000..732c70d2b383f4dfce6d8d710a376b4495b2a8b8 --- /dev/null +++ b/lib/admin_actions/financial.rb @@ -0,0 +1,45 @@ +# frozen_string_literal: true + +require_relative "../admin_action" +require_relative "../financial_info" +require_relative "../form_template" + +class AdminAction + class Financial + def self.call(customer_id, reply:, **) + new(customer_id, reply: reply).call + end + + def initialize(customer_id, reply:) + @customer_id = customer_id + @reply = reply + end + + def call + AdminFinancialInfo.for(@customer_id).then do |financial_info| + @reply.call(FormTemplate.render( + "admin_financial_info", + info: financial_info + )).then { + pay_methods(financial_info) + }.then { + transactions(financial_info) + } + end + end + + def pay_methods(financial_info) + @reply.call(FormTemplate.render( + "admin_payment_methods", + **financial_info.to_h + )) + end + + def transactions(financial_info) + @reply.call(FormTemplate.render( + "admin_transaction_list", + transactions: financial_info.transactions + )) + end + end +end diff --git a/lib/admin_command.rb b/lib/admin_command.rb index 1ce24ac7ae54ca11fca5bf51e1726113bedca107..84378e9d4b16bcff840ec8ca2e086080f12551a6 100644 --- a/lib/admin_command.rb +++ b/lib/admin_command.rb @@ -1,5 +1,7 @@ # frozen_string_literal: true +require_relative "admin_actions/cancel" +require_relative "admin_actions/financial" require_relative "bill_plan_command" require_relative "customer_info_form" require_relative "financial_info" @@ -83,53 +85,43 @@ class AdminCommand new_context(@target_customer.customer_id) end - def action_financial - AdminFinancialInfo.for(@target_customer).then do |financial_info| - reply(FormTemplate.render( - "admin_financial_info", - info: financial_info - )).then { - pay_methods(financial_info) - }.then { - transactions(financial_info) - } - end - end - def action_bill_plan BillPlanCommand.for(@target_customer).call end - def notify_customer(body) - m = Blather::Stanza::Message.new - m.from = CONFIG[:notify_from] - m.body = body - @target_customer.stanza_to(m) - end + class Simple + def initialize(klass) + @klass = klass + end - def action_cancel_account - notify_customer("Your JMP account has been cancelled.").then { - EMPromise.all([ - @target_customer.stanza_to( - Blather::Stanza::Iq::IBR.new(:set).tap(&:remove!) - ), - @target_customer.deregister!, - @customer_repo.disconnect_tel(@target_customer) - ]) - } - end + def call(customer_id, customer_repo:, **) + @klass.call( + customer_id, + reply: method(:reply), + customer_repo: customer_repo + ) + end - def pay_methods(financial_info) - reply(FormTemplate.render( - "admin_payment_methods", - **financial_info.to_h - )) + def reply(form=nil, note_type: nil, note_text: nil) + Command.reply { |reply| + reply.allowed_actions = [:next, :complete] + reply.command << form if form + reply.note_type = note_type if note_type + reply.note_text = note_text if note_text + } + end end - def transactions(financial_info) - reply(FormTemplate.render( - "admin_transaction_list", - transactions: financial_info.transactions - )) + [ + [:cancel_account, Simple.new(AdminAction::CancelCustomer)], + [:financial, Simple.new(AdminAction::Financial)] + ].each do |action, handler| + define_method("action_#{action}") do + handler.call( + @target_customer, + admin_action_repo: @admin_action_repo, + customer_repo: @customer_repo + ) + end end end diff --git a/lib/financial_info.rb b/lib/financial_info.rb index ca394d797ad741a0aecf8af21660843c5a120025..712c261cf5039d26a7c4da969495e8301b7d640f 100644 --- a/lib/financial_info.rb +++ b/lib/financial_info.rb @@ -1,6 +1,8 @@ # frozen_string_literal: true require "value_semantics/monkey_patched" +require_relative "customer_finacials" +require_relative "payment_methods" class AdminFinancialInfo value_semantics do