Skip to content

Commit

Permalink
add endpoint for handle deposit change status
Browse files Browse the repository at this point in the history
  • Loading branch information
OlegPhenomenon committed Sep 25, 2023
1 parent 697fafe commit 8af9f17
Show file tree
Hide file tree
Showing 5 changed files with 189 additions and 0 deletions.
1 change: 1 addition & 0 deletions .rubocop.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ require: rubocop-rails
AllCops:
Exclude:
- 'test/**/*'
- 'spec/**/*'
- 'db/**/*'
- 'config/**/*'
- 'bin/*'
Expand Down
Original file line number Diff line number Diff line change
@@ -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
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
# rubocop:disable Metrics
module Api
module V1
module InvoiceGenerator
Expand Down
1 change: 1 addition & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
127 changes: 127 additions & 0 deletions spec/requests/api/v1/invoice_generator/deposit_status_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
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 [email protected]"
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

0 comments on commit 8af9f17

Please sign in to comment.