Skip to content

Commit

Permalink
Merge branch 'main' into administrador-consulta-visitantes
Browse files Browse the repository at this point in the history
Co-authored-by: thalyta lima <[email protected]>
  • Loading branch information
RyanOxon and thalytalima211 committed Jul 19, 2024
2 parents 46405a8 + 234e4d6 commit dc6ecaa
Show file tree
Hide file tree
Showing 10 changed files with 247 additions and 0 deletions.
1 change: 1 addition & 0 deletions Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ gem 'breadcrumbs_on_rails'
gem 'cpf_cnpj'
gem 'cssbundling-rails'
gem 'devise'
gem 'faraday'
gem 'image_processing', '>= 1.2'
gem 'jbuilder'
gem 'jsbundling-rails'
Expand Down
10 changes: 10 additions & 0 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,11 @@ GEM
factory_bot_rails (6.4.3)
factory_bot (~> 6.4)
railties (>= 5.0.0)
faraday (2.10.0)
faraday-net_http (>= 2.0, < 3.2)
logger
faraday-net_http (3.1.0)
net-http
ferrum (0.15)
addressable (~> 2.5)
concurrent-ruby (~> 1.1)
Expand All @@ -148,6 +153,7 @@ GEM
railties (>= 6.0.0)
json (2.7.2)
language_server-protocol (3.17.0.3)
logger (1.6.0)
loofah (2.22.0)
crass (~> 1.0.2)
nokogiri (>= 1.12.0)
Expand All @@ -163,6 +169,8 @@ GEM
minitest (5.24.1)
msgpack (1.7.2)
mutex_m (0.2.0)
net-http (0.4.1)
uri
net-imap (0.4.14)
date
net-protocol
Expand Down Expand Up @@ -305,6 +313,7 @@ GEM
tzinfo (2.0.6)
concurrent-ruby (~> 1.0)
unicode-display_width (2.5.0)
uri (0.13.0)
warden (1.2.9)
rack (>= 2.0.9)
webrick (1.8.1)
Expand All @@ -329,6 +338,7 @@ DEPENDENCIES
debug
devise
factory_bot_rails
faraday
image_processing (>= 1.2)
jbuilder
jsbundling-rails
Expand Down
13 changes: 13 additions & 0 deletions app/controllers/condos_controller.rb
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
class CondosController < ApplicationController
rescue_from Faraday::ConnectionFailed, with: :connection_refused
before_action :authenticate_manager!, only: %i[new create edit update residents]
before_action :set_condo, only: %i[show edit update add_manager associate_manager residents]
before_action :set_breadcrumbs_for_details, only: %i[show edit update add_manager associate_manager]
Expand All @@ -11,6 +12,7 @@ def show
@common_areas = @condo.common_areas.order :name
@unit_types = @condo.unit_types.order :description
@todays_visitors = visitors_list(@condo)
request_bills
end

def new
Expand Down Expand Up @@ -87,4 +89,15 @@ def set_condo
def visitors_list(condo)
resident_signed_in? ? current_resident.todays_visitors : condo.expected_visitors(Time.zone.today)
end

def request_bills
return unless resident_signed_in? && current_resident.residence.present?

@bills = Bill.request_open_bills(current_resident.residence.id)
end

def connection_refused
@refused = true
render 'show', status: :unprocessable_entity
end
end
42 changes: 42 additions & 0 deletions app/models/bill.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
class Bill
attr_accessor :unit_id, :id, :condo_id, :issue_date, :due_date, :total_value_cents, :status, :values

def initialize(params = {})
@unit_id = params.fetch('unit_id', nil)
@id = params.fetch('id', nil)
@condo_id = params.fetch('condo_id', nil)
@issue_date = params.fetch('issue_date', nil)
@due_date = params.fetch('due_date', nil)
@total_value_cents = params.fetch('total_value_cents', nil)
@status = params.fetch('status', nil)
@values = params.fetch('values', [])
end

def self.request_open_bills(unit_id)
response = Faraday.get("http://localhost:4000/api/v1/units/#{unit_id}/bills")

return [] unless response.success?

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

def total_value_formatted
"R$ #{format('%.2f', total_value_cents / 100.0).gsub('.', ',')}"
end

def due_date_formatted
format_date(@due_date)
end

def issue_date_formatted
format_date(@issue_date)
end

private

def format_date(date)
Date.parse(date).strftime('%d/%m/%Y')
end
end
40 changes: 40 additions & 0 deletions app/views/condos/dashboard/_bills.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
<div class="accordion-item rounded-5 shadow-sm">
<h2 class="accordion-header">
<button class="accordion-button collapsed rounded-pill py-3" style="background-color: #FDE879" type="button" data-bs-toggle="collapse" data-bs-target="#bills" aria-expanded="false" aria-controls="collapseOne">
<strong class="position-absolute top-50 start-50 translate-middle text-black">Faturas em Aberto</strong>
</button>
</h2>

<div id="bills" class="accordion-collapse collapse" data-bs-parent="#accordionExample">
<div class="accordion-body">
<% if !@refused && @bills.present? && @bills.any? %>
<% @bills.take(3).each do |bill| %>
<div class="d-flex align-items-center my-1">
<svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" fill="currentColor" class="bi bi-currency-dollar mx-2" viewBox="0 0 16 16">
<path d="M4 10.781c.148 1.667 1.513 2.85 3.591 3.003V15h1.043v-1.216c2.27-.179 3.678-1.438 3.678-3.3 0-1.59-.947-2.51-2.956-3.028l-.722-.187V3.467c1.122.11 1.879.714 2.07 1.616h1.47c-.166-1.6-1.54-2.748-3.54-2.875V1H7.591v1.233c-1.939.23-3.27 1.472-3.27 3.156 0 1.454.966 2.483 2.661 2.917l.61.162v4.031c-1.149-.17-1.94-.8-2.131-1.718zm3.391-3.836c-1.043-.263-1.6-.825-1.6-1.616 0-.944.704-1.641 1.8-1.828v3.495l-.2-.05zm1.591 1.872c1.287.323 1.852.859 1.852 1.769 0 1.097-.826 1.828-2.2 1.939V8.73z"/>
</svg>
<div class="d-flex flex-column mx-3">
<div class="mt-3 fs-5">Valor: <%= bill.total_value_formatted %></div>
<div class="text-muted small">Vencimento: <%= bill.due_date_formatted %></div>
</div>
<%= link_to root_path, class: "ms-auto btn btn-dark d-inline-flex align-items-center rounded-pill" do %>
<p style="margin: 0; font-size: 14px;">Visualizar</p> <i class="bi bi-search ms-1"></i>
<% end %>
</div>
<hr class="m-0">
<% end %>
<% if @bills.count > 3 %>
<div class='d-flex'>
<%= link_to root_path, class:"btn py-1 rounded-pill d-flex justify-content-center align-items-center mb-2 w-25 fs-6 text-muted", style: "width: 100%;" do %>
<strong>ver mais</strong>
<% end %>
</div>
<% end %>
<% elsif @refused %>
<div class="alert alert-warning text-center">Conexão perdida com o servidor do PagueAlugel.</div>
<% else %>
<div class="alert alert-warning text-center">Não existem faturas em aberto.</div>
<% end %>
</div>
</div>
</div>
6 changes: 6 additions & 0 deletions app/views/condos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -78,5 +78,11 @@
<%= render 'condos/dashboard/common_area_list' %>
</div>

<div class="accordion mt-2">
<% if resident_signed_in? %>
<%= render 'condos/dashboard/bills' %>
<% end %>
</div>
</div>
</div>
5 changes: 5 additions & 0 deletions spec/support/json/empty_bills.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"bills": [

]
}
34 changes: 34 additions & 0 deletions spec/support/json/five_bills.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
{
"bills": [
{
"id": 11,
"issue_date": "2024-09-01",
"due_date": "2024-09-10",
"total_value_cents": 5000
},
{
"id": 1,
"issue_date": "2024-07-01",
"due_date": "2024-07-10",
"total_value_cents": 1000
},
{
"id": 3,
"issue_date": "2024-05-01",
"due_date": "2024-05-10",
"total_value_cents": 3000
},
{
"id": 6,
"issue_date": "2024-03-01",
"due_date": "2024-03-10",
"total_value_cents": 2564
},
{
"id": 9,
"issue_date": "2024-02-01",
"due_date": "2024-02-10",
"total_value_cents": 1234
}
]
}
16 changes: 16 additions & 0 deletions spec/support/json/two_bills.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
{
"bills": [
{
"id": 1,
"issue_date": "2024-07-01",
"due_date": "2024-07-10",
"total_value_cents": 1000
},
{
"id": 3,
"issue_date": "2024-05-01",
"due_date": "2024-05-10",
"total_value_cents": 3000
}
]
}
80 changes: 80 additions & 0 deletions spec/system/fees/user_sees_fee_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
require 'rails_helper'

describe 'user access condo show' do
it 'and see fees on dashboard' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
unit11 = resident.residence
json_data = Rails.root.join('spec/support/json/two_bills.json').read
fake_response = double('faraday_response', body: json_data, success?: true)
allow(Faraday).to receive(:get).with("http://localhost:4000/api/v1/units/#{unit11.id}/bills").and_return(fake_response)

login_as resident, scope: :resident

visit condo_path condo
click_on 'Faturas em Aberto'

expect(page).to have_content 'Faturas em Aberto'
expect(page).to have_content 'Vencimento: 10/07/2024'
expect(page).to have_content 'Valor: R$ 10,00'
expect(page).to have_content 'Vencimento: 10/05/2024'
expect(page).to have_content 'Valor: R$ 30,00'
end

it 'and do not see more than 3 ordered by more recent' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
unit11 = resident.residence
json_data = Rails.root.join('spec/support/json/five_bills.json').read
fake_response = double('faraday_response', body: json_data, success?: true)
allow(Faraday).to receive(:get).with("http://localhost:4000/api/v1/units/#{unit11.id}/bills").and_return(fake_response)

login_as resident, scope: :resident

visit condo_path condo
click_on 'Faturas em Aberto'

expect(page).to have_content 'Faturas em Aberto'
expect(page).to have_content 'Vencimento: 10/09/2024'
expect(page).to have_content 'Valor: R$ 50,00'
expect(page).to have_content 'Vencimento: 10/07/2024'
expect(page).to have_content 'Valor: R$ 10,00'
expect(page).to have_content 'Vencimento: 10/05/2024'
expect(page).to have_content 'Valor: R$ 30,00'
within '#bills' do
expect(page).to have_content 'ver mais'
end
expect(page).not_to have_content 'Vencimento: 10/03/2024'
expect(page).not_to have_content 'Valor: R$ 25,64'
expect(page).not_to have_content 'Vencimento: 10/02/2024'
expect(page).not_to have_content 'Valor: R$ 12,34'
end

it "and there's no data returned" do
condo = create :condo
resident = create(:resident, :with_residence, condo:)
unit11 = resident.residence
json_data = Rails.root.join('spec/support/json/empty_bills.json').read
fake_response = double('faraday_response', body: json_data, success?: true)
allow(Faraday).to receive(:get).with("http://localhost:4000/api/v1/units/#{unit11.id}/bills").and_return(fake_response)

login_as resident, scope: :resident

visit condo_path condo
click_on 'Faturas em Aberto'

expect(page).to have_content 'Não existem faturas em aberto.'
end

it 'and connection is lost with the external API' do
condo = create :condo
resident = create(:resident, :with_residence, condo:)

login_as resident, scope: :resident

visit condo_path condo
click_on 'Faturas em Aberto'

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

0 comments on commit dc6ecaa

Please sign in to comment.