Skip to content

Commit

Permalink
Implementa método de taxa uso no model de área comum e configura Rail…
Browse files Browse the repository at this point in the history
…s.configuration.api.

Co-authored-by: Marcella Aleo <[email protected]>
  • Loading branch information
ruliancruz and cellaaleo committed Jul 21, 2024
1 parent 866fef5 commit 1e45778
Show file tree
Hide file tree
Showing 6 changed files with 49 additions and 1 deletion.
5 changes: 5 additions & 0 deletions app/models/common_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,9 @@ class CommonArea < ApplicationRecord
def access_allowed?(resident)
condo.residents.include?(resident)
end

def tax
common_area_fee = JSON.parse(Faraday.get("#{Rails.configuration.api['base_url']}/common_area_fees/#{id}").body)
common_area_fee['errors'].present? ? 'Não identificada' : common_area_fee['value_cents']
end
end
2 changes: 2 additions & 0 deletions config/api.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
shared:
base_url: "http://127.0.0.1:4000/api/v1"
4 changes: 3 additions & 1 deletion config/application.rb
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,9 @@ class Application < Rails::Application
# These settings can be overridden in specific environments using the files
# in config/environments, which are processed later.
#
config.time_zone = "Brasilia"
config.time_zone = "America/Sao_Paulo"

config.api = config_for(:api)
# config.eager_load_paths << Rails.root.join("extras")

# Don't generate system test files.
Expand Down
30 changes: 30 additions & 0 deletions spec/models/common_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,34 @@
expect(common_area.errors).not_to include(:max_occupancy)
end
end

describe '#tax' do
it 'returns the tax value from common area on external system' do
common_area = build :common_area

data = Rails.root.join('spec/support/json/common_areas/common_area_fee.json').read

response = double('response', body: data, success?: true)

allow(Faraday)
.to receive(:get)
.with("#{Rails.configuration.api['base_url']}/common_area_fees/#{common_area.id}")
.and_return(response)

expect(common_area.tax).to eq 4200
end

it 'returns error message if the request is not succeeded' do
common_area = build :common_area

data = Rails.root.join('spec/support/json/common_areas/common_area_fee_error.json').read

allow(Faraday)
.to receive(:get)
.with("#{Rails.configuration.api['base_url']}/common_area_fees/#{common_area.id}")
.and_return(double('response', body: data, success?: false))

expect(common_area.tax).to eq 'Não identificada'
end
end
end
6 changes: 6 additions & 0 deletions spec/support/json/common_areas/common_area_fee.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"value_cents":4200,
"created_at":"2024-07-11T21:09:13.019Z",
"common_area_id":1,
"condo_id":1
}
3 changes: 3 additions & 0 deletions spec/support/json/common_areas/common_area_fee_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"errors":"Não encontrado"
}

0 comments on commit 1e45778

Please sign in to comment.