Skip to content

Commit

Permalink
Merge pull request #130 from TreinaDev/gerar-cobranca-reserva
Browse files Browse the repository at this point in the history
Gerar cobrança avulsa de reserva de área comum no Pague Aluguel
  • Loading branch information
ruliancruz authored Jul 22, 2024
2 parents 38e1446 + 71a164d commit b2fdc37
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 4 deletions.
7 changes: 6 additions & 1 deletion app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,12 @@ def create
resident: current_resident,
common_area: @common_area

redirect_to @common_area, notice: t('notices.reservation.created') if @reservation.save
return unless @reservation.generate_single_charge.status == 201

@reservation.save
redirect_to @common_area, notice: t('notices.reservation.created')
rescue Faraday::ConnectionFailed
redirect_to @common_area, alert: t('alerts.lost_connection')
end

def canceled
Expand Down
2 changes: 0 additions & 2 deletions app/models/common_area.rb
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,6 @@ def formatted_fee
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)
Expand Down
17 changes: 17 additions & 0 deletions app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,23 @@ class Reservation < ApplicationRecord
validates :date, presence: true
validate :check_availability, :date_must_be_actual_or_future, on: :create

def generate_single_charge
url = "#{Rails.configuration.api['base_url']}/single_charges/"
Faraday.post(url, single_charge_json, 'Content-Type' => 'application/json')
end

def single_charge_json
{ single_charge: {
description: nil,
value_cents: common_area.fee,
charge_type: 'common_area_fee',
issue_date: date,
condo_id: common_area.condo.id,
common_area_id: common_area.id,
unit_id: resident.residence.id
} }.to_json
end

def check_availability
common_area.reservations.confirmed.each do |reservation|
errors.add(:date, "#{I18n.l date} já está reservada para esta área comum") if reservation[:date] == date
Expand Down
2 changes: 1 addition & 1 deletion app/views/common_areas/_reservation_calendar.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
<div class="card-body">
<p class="card-text">Reservado por <%= reservation.resident.full_name %></p>
<% if reservation.date > Time.zone.today %>
<%= button_to 'Cancelar', canceled_common_area_reservation_path(@common_area, reservation), data: { turbo_confirm: 'Você tem certeza que deseja cancelar a reserva?' }, class: 'btn btn-warning btn-sm rounded-pill shadow-sm' unless manager_signed_in? %>
<%= button_to 'Cancelar', canceled_common_area_reservation_path(@common_area, reservation), data: { turbo_confirm: "Você tem certeza que deseja cancelar a reserva do dia #{I18n.l(date)}?" }, class: 'btn btn-warning btn-sm rounded-pill shadow-sm' unless manager_signed_in? %>
<% end %>
</div>
</div>
Expand Down
4 changes: 4 additions & 0 deletions spec/support/json/common_areas/single_charge.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
{
"message": "sucesso",
"single_charge_id": 1
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,12 @@
first_resident = create :resident, :with_residence, full_name: 'Maria Pereira', condo: common_area.condo
second_resident = create :resident

get_data = Rails.root.join('spec/support/json/common_areas/common_area_fees.json').read
allow(Faraday).to receive(:get).and_return(double('response', body: get_data, success?: true, status: 200))

post_data = Rails.root.join('spec/support/json/common_areas/single_charge.json').read
allow(Faraday).to receive(:post).and_return(double('response', body: post_data, success?: true, status: 201))

travel_to '01/07/2024' do
create :reservation,
common_area:,
Expand All @@ -28,5 +34,28 @@
end

expect(Reservation.last.resident).to eq first_resident
expect(common_area.reservations.last.date).to eq Date.new(2024, 7, 5)
end

it 'fail if the connection is lost with external application' do
common_area = create :common_area
resident = create :resident, :with_residence, condo: common_area.condo

travel_to '01/07/2024' do
login_as resident, scope: :resident
visit common_area_path common_area
within('.table > tbody > tr:nth-child(1) > .wday-5') { accept_confirm { click_on 'Reservar' } }
end

expect(current_path).to eq common_area_path common_area
expect(page).to have_content 'Conexão perdida com o servidor do PagueAluguel'

within('.table > tbody > tr:nth-child(1) > .wday-5') do
expect(page).not_to have_content 'Reservado por Maria Pereira'
expect(page).to have_button 'Reservar'
expect(page).not_to have_button 'Cancelar'
end

expect(Reservation.count).to eq 0
end
end

0 comments on commit b2fdc37

Please sign in to comment.