From bf5aacb10f74ad81c8d04ed3e14ec807d4b3786b Mon Sep 17 00:00:00 2001 From: Rulian de Jesus Cruz Date: Mon, 22 Jul 2024 00:57:37 -0300 Subject: [PATCH] =?UTF-8?q?Gera=20cancelamento=20de=20cobran=C3=A7a=20na?= =?UTF-8?q?=20aplica=C3=A7=C3=A3o=20PagueAluguel=20ao=20cancelar=20uma=20r?= =?UTF-8?q?eserva=20de=20=C3=A1rea=20comum.?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Co-authored-by: Marcella Aleo --- app/controllers/reservations_controller.rb | 4 +- app/models/reservation.rb | 9 ++++- ...16_add_single_charge_id_to_reservations.rb | 5 +++ db/schema.rb | 3 +- .../single_charge_cancelation.json | 3 ++ ...ent_cancel_common_area_reservation_spec.rb | 37 ++++++++++++++++++- .../resident_reserves_common_area_spec.rb | 1 + 7 files changed, 58 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20240722025316_add_single_charge_id_to_reservations.rb create mode 100644 spec/support/json/common_areas/single_charge_cancelation.json diff --git a/app/controllers/reservations_controller.rb b/app/controllers/reservations_controller.rb index cc493c67..d87b8603 100644 --- a/app/controllers/reservations_controller.rb +++ b/app/controllers/reservations_controller.rb @@ -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? diff --git a/app/models/reservation.rb b/app/models/reservation.rb index 52289752..b64e6d54 100644 --- a/app/models/reservation.rb +++ b/app/models/reservation.rb @@ -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 diff --git a/db/migrate/20240722025316_add_single_charge_id_to_reservations.rb b/db/migrate/20240722025316_add_single_charge_id_to_reservations.rb new file mode 100644 index 00000000..0f651411 --- /dev/null +++ b/db/migrate/20240722025316_add_single_charge_id_to_reservations.rb @@ -0,0 +1,5 @@ +class AddSingleChargeIdToReservations < ActiveRecord::Migration[7.1] + def change + add_column :reservations, :single_charge_id, :integer + end +end diff --git a/db/schema.rb b/db/schema.rb index 9df08839..cb013f13 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[7.1].define(version: 2024_07_18_231004) do +ActiveRecord::Schema[7.1].define(version: 2024_07_22_025316) do create_table "action_text_rich_texts", force: :cascade do |t| t.string "name", null: false t.text "body" @@ -131,6 +131,7 @@ t.integer "resident_id", null: false t.datetime "created_at", null: false t.datetime "updated_at", null: false + t.integer "single_charge_id" t.index ["common_area_id"], name: "index_reservations_on_common_area_id" t.index ["resident_id"], name: "index_reservations_on_resident_id" end diff --git a/spec/support/json/common_areas/single_charge_cancelation.json b/spec/support/json/common_areas/single_charge_cancelation.json new file mode 100644 index 00000000..57d1ee1e --- /dev/null +++ b/spec/support/json/common_areas/single_charge_cancelation.json @@ -0,0 +1,3 @@ +{ + "message": "cancelado" +} \ No newline at end of file diff --git a/spec/system/common_area/reservation/resident_cancel_common_area_reservation_spec.rb b/spec/system/common_area/reservation/resident_cancel_common_area_reservation_spec.rb index 6c064baf..d1b639de 100644 --- a/spec/system/common_area/reservation/resident_cancel_common_area_reservation_spec.rb +++ b/spec/system/common_area/reservation/resident_cancel_common_area_reservation_spec.rb @@ -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 @@ -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 @@ -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 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 6cd8f888..1b79898e 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 @@ -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