From 2bc690e520558401fe5d94c2069aff5753660d8f Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 25 Sep 2023 11:25:32 +0300 Subject: [PATCH] added service object for notify billing about change status of deposit and added tests --- .../deposit_statuses_controller.rb | 17 +++++- app/models/domain_participate_auction.rb | 8 ++- .../eis_billing/deposit_status_service.rb | 38 +++++++++++++ app/views/admin/paid_deposits/index.html.erb | 10 ++-- .../paid_deposits/deposit_statuses_test.rb | 4 ++ test/services/deposit_status_service_test.rb | 57 +++++++++++++++++++ 6 files changed, 125 insertions(+), 9 deletions(-) create mode 100644 app/services/eis_billing/deposit_status_service.rb create mode 100644 test/services/deposit_status_service_test.rb diff --git a/app/controllers/admin/paid_deposit/deposit_statuses_controller.rb b/app/controllers/admin/paid_deposit/deposit_statuses_controller.rb index 47cea3ce7..76be9c2c1 100644 --- a/app/controllers/admin/paid_deposit/deposit_statuses_controller.rb +++ b/app/controllers/admin/paid_deposit/deposit_statuses_controller.rb @@ -1,12 +1,25 @@ +# rubocop:disable Metrics module Admin class PaidDeposit::DepositStatusesController < BaseController def update @deposit = DomainParticipateAuction.find(params[:id]) + deposits = DomainParticipateAuction.includes(:user).includes(:auction).search(params) + @pagy, @deposits = pagy(deposits, items: params[:per_page] ||= 15) + if @deposit.update(status: params[:status]) - redirect_to admin_paid_deposits_path, status: :see_other, flash: { notice: 'Invoice status updated successfully' } + res = @deposit.send_deposit_status_to_billing_system + + if res.result? + redirect_to admin_paid_deposits_path, status: :see_other, + flash: { notice: 'Invoice status updated successfully' } + else + flash[:alert] = "Deposit status is updated in auction side, but got error from billing side: #{res.errors}" + redirect_to admin_paid_deposits_path, status: :see_other + end else - render 'admin/paid_deposits/index', status: :unprocessable_entity + flash[:alert] = @deposit.errors.full_messages.join(', ') + redirect_to admin_paid_deposits_path, status: :see_other end end end diff --git a/app/models/domain_participate_auction.rb b/app/models/domain_participate_auction.rb index 315516159..f6eff6936 100644 --- a/app/models/domain_participate_auction.rb +++ b/app/models/domain_participate_auction.rb @@ -6,11 +6,15 @@ class DomainParticipateAuction < ApplicationRecord scope :search_by_auction_name_and_user_email, ->(origin) { if origin.present? - self.joins(:user).joins(:auction).where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%") + joins(:user).joins(:auction).where('users.email ILIKE ? OR auctions.domain_name ILIKE ?', "%#{origin}%", "%#{origin}%") end } def self.search(params = {}) - self.search_by_auction_name_and_user_email(params[:search_string]) + search_by_auction_name_and_user_email(params[:search_string]) + end + + def send_deposit_status_to_billing_system + EisBilling::DepositStatusService.call(domain_name: auction.domain_name, user_uuid: user.uuid, status: status) end end diff --git a/app/services/eis_billing/deposit_status_service.rb b/app/services/eis_billing/deposit_status_service.rb new file mode 100644 index 000000000..a69691b84 --- /dev/null +++ b/app/services/eis_billing/deposit_status_service.rb @@ -0,0 +1,38 @@ +module EisBilling + class DepositStatusService + include EisBilling::Request + include EisBilling::BaseService + + attr_reader :user_uuid, :status, :domain_name + + def initialize(user_uuid:, status:, domain_name:) + @user_uuid = user_uuid + @status = status + @domain_name = domain_name + end + + def self.call(user_uuid:, status:, domain_name:) + new(user_uuid: user_uuid, status: status, domain_name:).call + end + + def call + struct_response(fetch) + end + + private + + def fetch + post deposit_status_url, params + end + + def params + { user_uuid: user_uuid, + domain_name: domain_name, + status: status } + end + + def deposit_status_url + '/api/v1/invoice_generator/deposit_status' + end + end +end diff --git a/app/views/admin/paid_deposits/index.html.erb b/app/views/admin/paid_deposits/index.html.erb index ba263e077..5c8b90529 100644 --- a/app/views/admin/paid_deposits/index.html.erb +++ b/app/views/admin/paid_deposits/index.html.erb @@ -67,11 +67,11 @@ <%= deposit.auction&.deposit %> <%= link_to deposit&.auction&.domain_name, admin_auction_path(deposit.auction) %> - <%= form_with url: admin_paid_deposit_deposit_status_path(deposit.id), method: :patch, - data: { controller: 'autosave', target: 'autosave.form'} do |f| %> - <%= f.select :status, options_for_select(DomainParticipateAuction.statuses.invert.values, deposit&.status), {}, - selected: deposit&.status, data: { action: 'autosave#save', target: 'autosave.select' } %> - <% end %> + <%= form_with url: admin_paid_deposit_deposit_status_path(deposit.id), method: :patch, + data: { controller: 'autosave', target: 'autosave.form', turbo: 'false'} do |f| %> + <%= f.select :status, options_for_select(DomainParticipateAuction.statuses.invert.values, deposit&.status), {}, + selected: deposit&.status, data: { action: 'autosave#save', target: 'autosave.select' } %> + <% end %> <% end %> diff --git a/test/integration/admin/paid_deposits/deposit_statuses_test.rb b/test/integration/admin/paid_deposits/deposit_statuses_test.rb index 4009e713d..2ad41ac95 100644 --- a/test/integration/admin/paid_deposits/deposit_statuses_test.rb +++ b/test/integration/admin/paid_deposits/deposit_statuses_test.rb @@ -38,4 +38,8 @@ def test_should_change_status_to_returned d.reload assert_equal d.status, 'returned' end + + def test_should_render_error_from_billing_side + # TODO: implement + end end \ No newline at end of file diff --git a/test/services/deposit_status_service_test.rb b/test/services/deposit_status_service_test.rb new file mode 100644 index 000000000..bb2dc737d --- /dev/null +++ b/test/services/deposit_status_service_test.rb @@ -0,0 +1,57 @@ +require 'test_helper' + +class DepositStatusServiceTest < ActiveSupport::TestCase + setup do + @auction = auctions(:english) + @user = users(:participant) + + @base_url = 'http://eis_billing_system:3000' + end + + def test_call_fetches_data_from_billing_system + stub_request(:post, "#{@base_url}/api/v1/invoice_generator/deposit_status") + .with( + body: { + 'user_uuid' => @user.uuid, + 'domain_name' => @auction.domain_name, + 'status' => 'returned' + } + ) + .to_return( + status: 200, + body: '{"message": "Status updated"}', + headers: {} + ) + + d = DomainParticipateAuction.create(user_id: @user.id, auction_id: @auction.id) + d.update(status: 'returned') + + res = d.send_deposit_status_to_billing_system + + assert res.result? + end + + def test_return_errors_in_struct_format + stub_request(:post, "#{@base_url}/api/v1/invoice_generator/deposit_status") + .with( + body: { + 'user_uuid' => @user.uuid, + 'domain_name' => @auction.domain_name, + 'status' => 'returned' + } + ) + .to_return( + status: 422, + body: '{"error": "Shit happens"}', + headers: {} + ) + + d = DomainParticipateAuction.create(user_id: @user.id, auction_id: @auction.id) + d.update(status: 'returned') + + res = d.send_deposit_status_to_billing_system + + refute res.result? + assert_equal res.errors, "Shit happens" + end +end