From 084293cf83b6f156518f3f818ec0ec49c3f8e27e Mon Sep 17 00:00:00 2001 From: Oleg Hasjanov Date: Mon, 25 Sep 2023 10:58:38 +0300 Subject: [PATCH] add endpoint for handle deposit change status --- .rubocop.yml | 1 + .../deposit_status_controller.rb | 59 ++++++++ .../invoice_status_controller.rb | 1 + config/routes.rb | 1 + .../invoice_generator/deposit_status_spec.rb | 126 ++++++++++++++++++ 5 files changed, 188 insertions(+) create mode 100644 app/controllers/api/v1/invoice_generator/deposit_status_controller.rb create mode 100644 spec/requests/api/v1/invoice_generator/deposit_status_spec.rb diff --git a/.rubocop.yml b/.rubocop.yml index 28852db..fbcb7f8 100644 --- a/.rubocop.yml +++ b/.rubocop.yml @@ -3,6 +3,7 @@ require: rubocop-rails AllCops: Exclude: - 'test/**/*' + - 'spec/**/*' - 'db/**/*' - 'config/**/*' - 'bin/*' diff --git a/app/controllers/api/v1/invoice_generator/deposit_status_controller.rb b/app/controllers/api/v1/invoice_generator/deposit_status_controller.rb new file mode 100644 index 0000000..1f9e7c5 --- /dev/null +++ b/app/controllers/api/v1/invoice_generator/deposit_status_controller.rb @@ -0,0 +1,59 @@ +module Api + module V1 + module InvoiceGenerator + class DepositStatusController < Api::V1::InvoiceGenerator::BaseController + before_action :set_invoice, only: :create + before_action :set_status, only: :create + + api! 'Invoice deposit status updates' + + param :invoice_number, String, required: true + param :status, String, required: true + + def create + if @invoice.update(status: @status) + render json: { 'message' => 'Status updated' }, status: :ok + else + error_message = "Status for #{params[:invoice_number]} wasn't updated; Status #{@status}" + NotifierMailer.inform_admin('Status received error', error_message).deliver_now + render json: { 'error' => error_message }, status: :unprocessable_entity + end + rescue StandardError => e + Rails.logger.info e + NotifierMailer.inform_admin('Status received standard error', e).deliver_now + end + + private + + # rubocop:disable Metrics/AbcSize + def set_invoice + @invoice = ::Invoice.where("description LIKE ? AND description LIKE ?", "%#{params[:domain_name]}%", "%#{params[:user_uuid]}%").first + return if @invoice.present? && @invoice.affiliation == 'auction_deposit' + + message = "Invoice with #{params[:domain_name]} and #{params[:user_uuid]} not found in Deposit Status Controller" + NotifierMailer.inform_admin("Invoice with #{params[:domain_name]} and #{params[:user_uuid]} not found", message).deliver_now + + render json: { 'error' => message }, status: :unprocessable_entity and return + end + + def set_status + @status = reform_status[params[:status]] + return if %w[paid unpaid refunded].include?(@status) + + message = "Wrong invoice status #{@status} in Deposit Status Controller" + NotifierMailer.inform_admin("Invoice status #{@status} is wrong", message).deliver_now + + render json: { 'error' => message }, status: :unprocessable_entity and return + end + + def reform_status + { + 'paid' => 'paid', + 'prepayment' => 'unpaid', + 'returned' => 'refunded' + } + end + end + end + end +end diff --git a/app/controllers/api/v1/invoice_generator/invoice_status_controller.rb b/app/controllers/api/v1/invoice_generator/invoice_status_controller.rb index 9eb79e3..3373f26 100644 --- a/app/controllers/api/v1/invoice_generator/invoice_status_controller.rb +++ b/app/controllers/api/v1/invoice_generator/invoice_status_controller.rb @@ -1,3 +1,4 @@ +# rubocop:disable Metrics module Api module V1 module InvoiceGenerator diff --git a/config/routes.rb b/config/routes.rb index 751e466..543e2a5 100644 --- a/config/routes.rb +++ b/config/routes.rb @@ -71,6 +71,7 @@ resources :oneoff, only: [:create] resources :deposit_prepayment, only: [:create] resources :bulk_payment, only: [:create] + resources :deposit_status, only: [:create] end namespace :refund do diff --git a/spec/requests/api/v1/invoice_generator/deposit_status_spec.rb b/spec/requests/api/v1/invoice_generator/deposit_status_spec.rb new file mode 100644 index 0000000..8456247 --- /dev/null +++ b/spec/requests/api/v1/invoice_generator/deposit_status_spec.rb @@ -0,0 +1,126 @@ +require 'rails_helper' + +RSpec.describe 'Api::V1::InvoiceGenerator::DepositStatusController', type: :request do + let(:invoice) { create(:invoice) } + + before(:each) do + allow_any_instance_of(ApplicationController).to receive(:authorized).and_return(true) + @user_uuid = 'df0ad0b9-8b4e-4946-9562-0a2bb877156c' + @domain_name = 'deping10.ee' + @description = "auction_deposit #{@domain_name}, user_uuid #{@user_uuid}, user_email john_doe@test.ee" + end + + it 'should update status to paid' do + invoice.update(status: 'unpaid', affiliation: 'auction_deposit', description: @description) + invoice.reload + + expect(invoice.status).to eq('unpaid') + + params = { + user_uuid: @user_uuid, + domain_name: @domain_name, + status: 'paid' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(invoice.status).to eq('paid') + end + + it 'should update status to unpaid' do + invoice.update(status: 'paid', affiliation: 'auction_deposit', description: @description) + invoice.reload + + expect(invoice.status).to eq('paid') + + params = { + user_uuid: @user_uuid, + domain_name: @domain_name, + status: 'prepayment' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(invoice.status).to eq('unpaid') + end + + it 'should update status to refunded' do + invoice.update(status: 'unpaid', affiliation: 'auction_deposit', description: @description) + invoice.reload + + expect(invoice.status).to eq('unpaid') + + params = { + user_uuid: @user_uuid, + domain_name: @domain_name, + status: 'returned' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(invoice.status).to eq('refunded') + end + + it 'should notify if status is invalid' do + invoice.update(status: 'unpaid', affiliation: 'auction_deposit', description: @description) + invoice.reload + + expect(invoice.status).to eq('unpaid') + + params = { + user_uuid: @user_uuid, + domain_name: @domain_name, + status: 'refunded' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(response.status).to eq 422 + expect(invoice.status).to eq('unpaid') + end + + it 'should notify if invoice not exists' do + invoice.update(status: 'unpaid', affiliation: 'auction_deposit', description: @description) + invoice.reload + + expect(invoice.status).to eq('unpaid') + + params = { + user_uuid: 'non-existing-uuid', + domain_name: @domain_name, + status: 'paid' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(response.status).to eq 422 + expect(JSON.parse(response.body)['error']).to eq 'Invoice with deping10.ee and non-existing-uuid not found in Deposit Status Controller' + expect(invoice.status).to eq('unpaid') + end + + it 'should notify if invoice is not auction_deposit' do + invoice.update(status: 'unpaid', affiliation: 'regular', description: @description) + invoice.reload + + expect(invoice.status).to eq('unpaid') + + params = { + user_uuid: @user_uuid, + domain_name: @domain_name, + status: 'refunded' + } + + post api_v1_invoice_generator_deposit_status_index_path, params: params + invoice.reload + + expect(response.status).to eq 422 + expect(JSON.parse(response.body)['error']).to eq "Invoice with deping10.ee and df0ad0b9-8b4e-4946-9562-0a2bb877156c not found in Deposit Status Controller" + expect(invoice.status).to eq('unpaid') + + end +end