Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/main' into criar-mural-avisos
Browse files Browse the repository at this point in the history
Co-authored-by: Lucas Oliveira <[email protected]>
  • Loading branch information
Vinigperuzzi and lucasobx committed Jul 20, 2024
2 parents 3d2b1dd + efc84e0 commit fdc9d86
Show file tree
Hide file tree
Showing 54 changed files with 856 additions and 236 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
95 changes: 18 additions & 77 deletions app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
@@ -1,9 +1,7 @@
class ApplicationController < ActionController::Base
before_action :warn_tower_registration_incomplete
before_action :warn_resident_incomplete_tenant
before_action :warn_resident_incomplete_owner
include Warnings

before_action :resident_registration_incomplete
before_action :warn_resident_photo
before_action :block_manager_from_resident_sign_in
before_action :block_resident_from_manager_sign_in
before_action :add_home_breadcrumb, unless: :devise_controller?
Expand All @@ -19,33 +17,34 @@ def devise_controller?
devise_controller_name.include?(controller_name)
end

def authorize_super_manager!
authorize_redirect unless current_manager.is_super
def authorize_super_manager
not_authorized_redirect unless current_manager.is_super
end

def authorize_condo_manager!(condo)
authorize_redirect unless check_manager(condo)
def authorize_condo_manager(condo)
not_authorized_redirect unless authorize_manager(condo)
end

def authorize_condo_resident!(condo)
authorize_redirect if check_resident(condo)
def authorize_condo_resident(condo)
not_authorized_redirect if authorize_resident(condo)
end

def authorize_condo_manager_or_resident!(condo)
authorize_redirect unless check_manager(condo) || check_resident(condo)
def authorize_user(condo)
not_authorized_redirect unless authorize_manager(condo) || authorize_resident(condo)
end

def check_manager(condo)
(manager_signed_in? && current_manager.is_super) ||
(manager_signed_in? && current_manager.condos.include?(condo))
def authorize_manager(condo)
return unless manager_signed_in?

current_manager.is_super || current_manager.condos.include?(condo)
end

def check_resident(condo)
resident_signed_in? && current_resident.condo == condo
def authorize_resident(condo)
resident_signed_in? && condo.residents.include?(current_resident)
end

def authorize_redirect
redirect_to root_path, alert: I18n.t('alerts.manager.not_authorized')
def not_authorized_redirect
redirect_to root_path, alert: I18n.t('alerts.not_authorized')
end

def block_manager_from_resident_sign_in
Expand All @@ -64,62 +63,4 @@ def resident_registration_incomplete

redirect_to confirm_resident_path(current_resident) if current_resident.mail_not_confirmed?
end

def warn_resident_photo
return unless resident_signed_in?

warning_message = current_resident.photo_warning_html_message
flash.now[:warning] = warning_message if warning_message
end

def warn_tower_registration_incomplete
return unless manager_signed_in?
return if controller_name == 'towers' && action_name == 'edit_floor_units'

Tower.incomplete.each do |tower|
flash.now[:warning] ||= ''
flash.now[:warning] << tower_warning_flash_message(tower)
end
end

def warn_resident_incomplete_tenant
return unless manager_signed_in?
return if controller_name == 'tenants' && action_name == 'new'

Resident.not_tenant.each do |resident|
flash.now[:warning] ||= ''
flash.now[:warning] << resident_not_tenant_message(resident)
end
end

def warn_resident_incomplete_owner
return unless manager_signed_in?
return if controller_name == 'owners' && action_name == 'new'

Resident.not_owner.each do |resident|
flash.now[:warning] ||= ''
flash.now[:warning] << resident_not_owner_message(resident)
end
end

def tower_warning_flash_message(tower)
view_context.link_to(
view_context.sanitize(tower.warning_html_message), edit_floor_units_condo_tower_path(tower.condo, tower),
class: 'link-dark link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover'
)
end

def resident_not_tenant_message(resident)
view_context.link_to(
view_context.sanitize(resident.warning_html_message_tenant), new_resident_tenant_path(resident),
class: 'link-dark link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover'
)
end

def resident_not_owner_message(resident)
view_context.link_to(
view_context.sanitize(resident.warning_html_message_owner), new_resident_owner_path(resident),
class: 'link-dark link-offset-2 link-underline-opacity-25 link-underline-opacity-100-hover'
)
end
end
36 changes: 36 additions & 0 deletions app/controllers/bills_controller.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
class BillsController < ApplicationController
rescue_from Faraday::ConnectionFailed, with: :connection_refused
before_action :authenticate_resident!, only: %i[index show]
before_action :unit_for_current_resident
before_action :request_open_bills_list, only: :index
before_action :request_bill_details, only: :show
before_action :set_breadcrumbs_for_action, only: %i[index show]

def index; end

def show; end

private

def unit_for_current_resident
@unit = current_resident.residence
end

def request_open_bills_list
@bills = Bill.request_open_bills(@unit.id)
end

def request_bill_details
@bill = Bill.request_bill_details(params[:id])

redirect_to bills_path, alert: t('alerts.bill.not_found') unless @bill
end

def connection_refused
redirect_to root_path, alert: t('alerts.bill.lost_connection')
end

def set_breadcrumbs_for_action
add_breadcrumb I18n.t("breadcrumb.bill.#{action_name}")
end
end
4 changes: 2 additions & 2 deletions app/controllers/common_areas_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ class CommonAreasController < ApplicationController
before_action :set_condo, only: %i[new create]
before_action :set_common_area, only: %i[show edit update]
before_action :set_condo_for_details, only: %i[show edit update]
before_action -> { authorize_condo_manager!(@condo) }, only: %i[new edit create update]
before_action -> { authorize_condo_manager_or_resident!(@condo) }, only: %i[show]
before_action -> { authorize_condo_manager(@condo) }, only: %i[new create edit update]
before_action -> { authorize_user(@condo) }, only: [:show]
before_action :set_breadcrumbs_for_register, only: %i[new create]
before_action :set_breadcrumbs_for_details, only: %i[show edit update]

Expand Down
63 changes: 63 additions & 0 deletions app/controllers/concerns/warnings.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
module Warnings
extend ActiveSupport::Concern
include WarningHelper

included do
before_action :warn_tower_registration_incomplete
before_action :warn_resident_incomplete
before_action :warn_resident_photo
end

private

def warn_resident_photo
return unless resident_signed_in?

warning_message = current_resident.photo_warning_html_message
flash.now[:warning] = warning_message if warning_message
end

def warn_tower_registration_incomplete
return unless manager_signed_in?
return if controller_name == 'towers' && action_name == 'edit_floor_units'

generate_tower_registration_messages(current_manager.is_super? ? Condo.all : current_manager.condos)
end

def generate_tower_registration_messages(condos)
condos.each do |condo|
condo.towers.incomplete.each do |tower|
flash.now[:warning] ||= ''
flash.now[:warning] << tower_warning_flash_message(tower)
end
end
end

def warn_resident_incomplete
return unless manager_signed_in?
return if controller_name == 'owners' && action_name == 'new'

generate_incomplete_resident_messages(current_manager.is_super? ? Condo.all : current_manager.condos)
end

def generate_incomplete_resident_messages(condos)
condos.each do |condo|
generate_filtered_property_registration_pendings_messages condo
generate_filtered_residence_registration_pendings_messages condo
end
end

def generate_filtered_property_registration_pendings_messages(condo)
condo.filtered_property_registration_pendings.each do |resident|
flash.now[:warning] ||= ''
flash.now[:warning] << resident_property_registration_pending_message(resident)
end
end

def generate_filtered_residence_registration_pendings_messages(condo)
condo.filtered_residence_registration_pendings.each do |resident|
flash.now[:warning] ||= ''
flash.now[:warning] << resident_residence_registration_pending_message(resident)
end
end
end
21 changes: 12 additions & 9 deletions app/controllers/condos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ class CondosController < ApplicationController
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]
before_action :authorize_super_manager!, only: %i[new create add_manager associate_manager]
before_action -> { authorize_condo_manager!(@condo) }, only: %i[edit update]
before_action -> { authorize_condo_manager_or_resident!(@condo) }, only: %i[show]
before_action :authorize_super_manager, only: %i[new create add_manager associate_manager]
before_action -> { authorize_condo_manager(@condo) }, only: %i[edit update residents]
before_action -> { authorize_user(@condo) }, only: [:show]

def show
@residents = @condo.residents
Expand All @@ -14,6 +14,7 @@ def show
@announcements = @condo.announcements.order(updated_at: :desc).limit(3)
@more_than_3_announcements = @condo.announcements.count > 3
@todays_visitors = (resident_signed_in? ? current_resident.todays_visitors : [])
request_bills
end

def new
Expand Down Expand Up @@ -68,12 +69,6 @@ def residents

private

def authenticate_manager!
return redirect_to root_path if resident_signed_in?

super
end

def set_breadcrumbs_for_details
add_breadcrumb @condo.name.to_s, condo_path(@condo)
end
Expand All @@ -86,4 +81,12 @@ def condo_params
def set_condo
@condo = Condo.find_by(id: params[:id])
end

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

result = Bill.safe_request_open_bills(current_resident.residence.id)
@bills = result[:bills]
@bills_error = result[:error]
end
end
8 changes: 3 additions & 5 deletions app/controllers/home_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
class HomeController < ApplicationController
def index
redirect_to signup_choice_path unless manager_signed_in? || resident_signed_in?
return @condos = Condo.all if manager_signed_in? && current_manager.is_super
return redirect_to signup_choice_path unless manager_signed_in? || resident_signed_in?
return @condos = current_manager.is_super ? Condo.all : current_manager.condos if manager_signed_in?

return @condos = current_manager.condos if manager_signed_in? && !current_manager.is_super

@condos = Condo.all
@condos = current_resident.condos
end

def signup; end
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/managers_controller.rb
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
class ManagersController < ApplicationController
before_action :authenticate_manager!, only: %i[new create]
before_action :authorize_super_manager!, only: %i[new create]
before_action :authorize_super_manager, only: %i[new create]
before_action :set_manager, only: %i[edit_photo update_photo]

def new
Expand Down
6 changes: 3 additions & 3 deletions app/controllers/owners_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def create

def destroy
unit = Unit.find params[:id]
return if authorize_condo_manager!(unit.condo)
return if authorize_condo_manager(unit.condo)

unit.update(owner: nil)
redirect_to new_resident_owner_path(@resident), notice: t('notices.owner.unit_removed')
Expand All @@ -44,7 +44,7 @@ def set_condos
def finish_ownership_register
return unless params[:commit] == 'Finalizar Cadastro de Propriedades'

@resident.not_tenant! if @resident.not_owner?
@resident.residence_registration_pending! if @resident.property_registration_pending?
redirect_to new_resident_tenant_path(@resident), notice: t('notices.owner.finalized')
end

Expand All @@ -60,6 +60,6 @@ def set_breadcrumbs_for_register
end

def any_redirect?(unit)
inexistent_unit(unit) || authorize_condo_manager!(unit.condo)
inexistent_unit(unit) || authorize_condo_manager(unit.condo)
end
end
Loading

0 comments on commit fdc9d86

Please sign in to comment.