Skip to content

Commit

Permalink
Merge pull request #137 from TreinaDev/cancelar-reserva
Browse files Browse the repository at this point in the history
Gera cancelamento de cobrança na aplicação PagueAluguel
  • Loading branch information
ruliancruz authored Jul 22, 2024
2 parents 38c753e + 44b6c28 commit a793d22
Show file tree
Hide file tree
Showing 7 changed files with 58 additions and 4 deletions.
4 changes: 3 additions & 1 deletion app/controllers/reservations_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ def canceled
return redirect_to @common_area, notice: t('notices.reservation.canceled') if cancel_reservation

redirect_to @common_area, alert: t('alerts.reservation.cancelation_failed')
rescue Faraday::ConnectionFailed
redirect_to @common_area, alert: t('alerts.lost_connection')
end

private

def cancel_reservation
Time.zone.today < @reservation.date && @reservation.canceled!
Time.zone.today < @reservation.date && @reservation.cancel_single_charge.status == 200 && @reservation.canceled!
end

def resident_access_restricted?
Expand Down
9 changes: 8 additions & 1 deletion app/models/reservation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,16 @@ class Reservation < ApplicationRecord
validates :date, presence: true
validate :check_availability, :date_must_be_actual_or_future, on: :create

def cancel_single_charge
url = "#{Rails.configuration.api['base_url']}/single_charges/#{single_charge_id}/cancel"
Faraday.patch(url)
end

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

def single_charge_json
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddSingleChargeIdToReservations < ActiveRecord::Migration[7.1]
def change
add_column :reservations, :single_charge_id, :integer
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 3 additions & 0 deletions spec/support/json/common_areas/single_charge_cancelation.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"message": "cancelado"
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,15 @@
common_area = create :common_area
resident = create :resident, :with_residence, condo: common_area.condo

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

travel_to '01/07/2024' do
reservation = create :reservation,
common_area:,
resident:,
date: '05/07/2024',
single_charge_id: 1,
status: :confirmed

login_as resident, scope: :resident
Expand All @@ -34,7 +38,7 @@
resident = create :resident, :with_residence, condo: common_area.condo

travel_to '03/07/2024' do
create :reservation, common_area:, resident:, date: '03/07/2024', status: :confirmed
create :reservation, common_area:, resident:, date: '03/07/2024', single_charge_id: 1, status: :confirmed
end

travel_to '05/07/2024' do
Expand All @@ -47,4 +51,35 @@
end
end
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
reservation = create :reservation,
common_area:,
resident:,
date: '05/07/2024',
single_charge_id: 1,
status: :confirmed

login_as resident, scope: :resident
visit common_area_path common_area

within('.table > tbody > tr:nth-child(1) > .wday-5') do
accept_confirm { click_on 'Cancelar' }
reservation.reload

expect(page).to have_content 'Reservado'
expect(page).to have_button 'Cancelar'
expect(page).not_to have_button 'Reservar'
end

expect(page).to have_content 'Conexão perdida com o servidor do PagueAluguel'
expect(page).not_to have_content 'Reserva cancelada com sucesso'
expect(current_path).to eq common_area_path common_area
expect(reservation.canceled?).to be false
end
end
end
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@

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

it 'fail if the connection is lost with external application' do
Expand Down

0 comments on commit a793d22

Please sign in to comment.