-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add endpoint for handle deposit change status
- Loading branch information
1 parent
697fafe
commit 8af9f17
Showing
5 changed files
with
189 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
59 changes: 59 additions & 0 deletions
59
app/controllers/api/v1/invoice_generator/deposit_status_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
1 change: 1 addition & 0 deletions
1
app/controllers/api/v1/invoice_generator/invoice_status_controller.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,3 +1,4 @@ | ||
# rubocop:disable Metrics | ||
module Api | ||
module V1 | ||
module InvoiceGenerator | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
127 changes: 127 additions & 0 deletions
127
spec/requests/api/v1/invoice_generator/deposit_status_spec.rb
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |