From db209619602dc74d735a848218e77db8bc6b8808 Mon Sep 17 00:00:00 2001 From: Marcella Aleo Date: Sun, 21 Jul 2024 23:49:48 +0100 Subject: [PATCH] =?UTF-8?q?Gera=20cobran=C3=A7a=20na=20app=20PagueAluguel?= =?UTF-8?q?=20ao=20reservar=20uma=20=C3=A1rea=20comum.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Rulian Cruz --- app/controllers/reservations_controller.rb | 7 ++++- app/models/common_area.rb | 2 -- app/models/reservation.rb | 17 +++++++++++ .../_reservation_calendar.html.erb | 2 +- .../json/common_areas/single_charge.json | 4 +++ .../resident_reserves_common_area_spec.rb | 29 +++++++++++++++++++ ...c.rb => user_sees_common_area_fee_spec.rb} | 0 7 files changed, 57 insertions(+), 4 deletions(-) create mode 100644 spec/support/json/common_areas/single_charge.json rename spec/system/common_area/{user_sees_common_area_tax_spec.rb => user_sees_common_area_fee_spec.rb} (100%) diff --git a/app/controllers/reservations_controller.rb b/app/controllers/reservations_controller.rb index 6090b09c..cc493c67 100644 --- a/app/controllers/reservations_controller.rb +++ b/app/controllers/reservations_controller.rb @@ -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 diff --git a/app/models/common_area.rb b/app/models/common_area.rb index c0c0ef3a..a9b76d07 100644 --- a/app/models/common_area.rb +++ b/app/models/common_area.rb @@ -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) diff --git a/app/models/reservation.rb b/app/models/reservation.rb index eb4ae4e0..52289752 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -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 diff --git a/app/views/common_areas/_reservation_calendar.html.erb b/app/views/common_areas/_reservation_calendar.html.erb index 11991a9c..92fb14ab 100644 --- a/app/views/common_areas/_reservation_calendar.html.erb +++ b/app/views/common_areas/_reservation_calendar.html.erb @@ -12,7 +12,7 @@

Reservado por <%= reservation.resident.full_name %>

<% 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 %>
diff --git a/spec/support/json/common_areas/single_charge.json b/spec/support/json/common_areas/single_charge.json new file mode 100644 index 00000000..e7f57a80 --- /dev/null +++ b/spec/support/json/common_areas/single_charge.json @@ -0,0 +1,4 @@ +{ + "message": "sucesso", + "single_charge_id": 1 +} \ No newline at end of file diff --git a/spec/system/common_area/reservation/resident_reserves_common_area_spec.rb b/spec/system/common_area/reservation/resident_reserves_common_area_spec.rb index a87c8a24..6cd8f888 100644 --- a/spec/system/common_area/reservation/resident_reserves_common_area_spec.rb +++ b/spec/system/common_area/reservation/resident_reserves_common_area_spec.rb @@ -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:, @@ -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 diff --git a/spec/system/common_area/user_sees_common_area_tax_spec.rb b/spec/system/common_area/user_sees_common_area_fee_spec.rb similarity index 100% rename from spec/system/common_area/user_sees_common_area_tax_spec.rb rename to spec/system/common_area/user_sees_common_area_fee_spec.rb