Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Consultar no Pague Aluguel a taxa de uso de área comum #128

Merged
merged 7 commits into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion app/controllers/bills_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def request_bill_details
end

def connection_refused
redirect_to root_path, alert: t('alerts.bill.lost_connection')
redirect_to root_path, alert: t('alerts.lost_connection')
end

def set_breadcrumbs_for_action
Expand Down
12 changes: 12 additions & 0 deletions app/helpers/currency_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
module CurrencyHelper
def integer_to_brl(value)
return nil unless value

ActionController::Base.helpers.number_to_currency(
value / 100.0,
unit: 'R$',
delimiter: '.',
separator: ','
).delete(' ')
end
end
21 changes: 21 additions & 0 deletions app/models/common_area.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
class CommonArea < ApplicationRecord
include CurrencyHelper

belongs_to :condo
has_many :reservations, dependent: :destroy

Expand All @@ -8,4 +10,23 @@ class CommonArea < ApplicationRecord
def access_allowed?(resident)
condo.residents.include?(resident)
end

def formatted_fee
return fee if fee == 'Não encontrada'

integer_to_brl(fee) || 'Não informada'
end

private

def fee
url = "#{Rails.configuration.api['base_url']}/condos/#{condo.id}/common_area_fees"
response = Faraday.get(url)

common_area_fees = JSON.parse(response.body)
this_fee = common_area_fees.find { |fee| fee['common_area_id'] == id }
this_fee ? this_fee['value_cents'] : nil
rescue Faraday::ConnectionFailed
'Não encontrada'
end
end
4 changes: 2 additions & 2 deletions app/services/bill_service.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,14 @@ class BillService
def self.request_open_bills(unit_id)
response = Faraday.get("http://localhost:4000/api/v1/units/#{unit_id}/bills")

return { bills: [], error: I18n.t('alerts.bill.lost_connection') } unless response.success?
return { bills: [], error: I18n.t('alerts.lost_connection') } unless response.success?

bills_data = JSON.parse(response.body)['bills']
bills_array = bills_data.map { |bill_data| Bill.new(bill_data) }
sorted_bills = bills_array.sort_by { |bill| bill.due_date.to_time.to_i }.reverse

{ bills: sorted_bills, error: nil }
rescue Faraday::ConnectionFailed
{ bills: [], error: I18n.t('alerts.bill.lost_connection') }
{ bills: [], error: I18n.t('alerts.lost_connection') }
end
end
2 changes: 2 additions & 0 deletions app/views/common_areas/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@
<p><%= @common_area.rules %></p>
<% end %>

<p><strong>Taxa da reserva:</strong> <%= @common_area.formatted_fee %></p>

<%= link_to 'Editar', edit_common_area_path(@common_area), class: 'btn btn-dark rounded-pill px-4' if manager_signed_in? %>
</div>

Expand Down
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
3 changes: 2 additions & 1 deletion config/locales/messages.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
pt-BR:
alerts:
access_denied: 'Para continuar, faça login ou registre-se.'
not_authorized: 'Você não tem permissão para fazer isso'
not_authorized: 'Você não tem permissão para fazer isso'
lost_connection: 'Conexão perdida com o servidor do PagueAluguel'
3 changes: 1 addition & 2 deletions config/locales/models/bills.pt-BR.yml
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
pt-BR:
alerts:
bill:
lost_connection: 'Conexão perdida com o servidor do PagueAlugel.'
not_found: 'Fatura não encontrada'
not_found: 'Fatura não encontrada'
28 changes: 28 additions & 0 deletions spec/models/common_area_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -32,4 +32,32 @@
expect(common_area.errors).not_to include(:max_occupancy)
end
end

describe '#formatted_fee' do
it 'returns the fee value from common area on external system' do
common_area = build :common_area, id: 2

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

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

expect(common_area.formatted_fee).to eq 'R$300,00'
end

it 'returns a message if the fee is not found' do
common_area = build :common_area, id: 99

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

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

expect(common_area.formatted_fee).to eq 'Não informada'
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":4242,
"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"
}
23 changes: 23 additions & 0 deletions spec/support/json/common_areas/common_area_fees.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
[
{
"id":1,
"value_cents":20000,
"created_at":"2024-07-11T21:09:13.019Z",
"common_area_id":1,
"condo_id":1
},
{
"id":2,
"value_cents":30000,
"created_at":"2024-07-11T21:09:13.024Z",
"common_area_id":2,
"condo_id":1
},
{
"id":3,
"value_cents":40000,
"created_at":"2024-07-11T21:09:13.027Z",
"common_area_id":3,
"condo_id":1
}
]
60 changes: 60 additions & 0 deletions spec/system/common_area/user_sees_common_area_tax_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
require 'rails_helper'

describe 'User sees common area fee' do
context 'as a resident' do
it 'successfully' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
common_area = create :common_area, condo:, id: 2

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

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

login_as resident, scope: :resident
visit common_area_path common_area

expect(page).to have_content 'Taxa da reserva: R$300,00'
end

it 'and see a message if the fee is not found' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
common_area = create :common_area, condo:, id: 99

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

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

login_as resident, scope: :resident
visit common_area_path common_area

expect(page).to have_content 'Taxa da reserva: Não informada'
end
end

context 'as a manager' do
it 'successfully' do
manager = create :manager
common_area = create :common_area, id: 2

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

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

login_as manager, scope: :manager
visit common_area_path common_area

expect(page).to have_content 'Taxa da reserva: R$300,00'
end
end
end
2 changes: 1 addition & 1 deletion spec/system/fees/user_sees_fee_details_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@
login_as resident, scope: :resident
visit bills_path

expect(page).to have_content 'Conexão perdida com o servidor do PagueAlugel.'
expect(page).to have_content 'Conexão perdida com o servidor do PagueAluguel'
expect(current_path).to eq root_path
end
end
2 changes: 1 addition & 1 deletion spec/system/fees/user_sees_fee_index_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@
login_as resident, scope: :resident
visit bills_path

expect(page).to have_content 'Conexão perdida com o servidor do PagueAlugel.'
expect(page).to have_content 'Conexão perdida com o servidor do PagueAluguel'
expect(current_path).to eq root_path
end
end
2 changes: 1 addition & 1 deletion spec/system/fees/user_sees_fee_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,6 @@
visit condo_path condo
click_on 'Faturas em Aberto'

expect(page).to have_content 'Conexão perdida com o servidor do PagueAlugel.'
expect(page).to have_content 'Conexão perdida com o servidor do PagueAluguel'
end
end
Loading