Skip to content

Commit

Permalink
Merge pull request #121 from TreinaDev/listagem-visitantes-cadastrados
Browse files Browse the repository at this point in the history
Listagem de visitantes cadastrados e Filtros para listagens
  • Loading branch information
RyanOxon authored Jul 21, 2024
2 parents e41b70e + 9cc0dc7 commit e54d101
Show file tree
Hide file tree
Showing 21 changed files with 958 additions and 76 deletions.
19 changes: 18 additions & 1 deletion app/controllers/visitor_entries_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,16 @@ class VisitorEntriesController < ApplicationController
before_action :set_breadcrumbs_for_index, only: %i[index]

def index
@visitor_entries = VisitorEntry.all.order! 'created_at DESC'
return @visitor_entries = @condo.visitor_entries.order('created_at DESC') if check_empty_params

@result = []
params.permit(:full_name, :visit_date, :identity_number).each do |key, value|
key = 'created_at' if key == 'visit_date'
@result << find_visitor_entries(key, value) if value.present?
end

@visitor_entries = @result.reduce(:&)
@visitor_entries = @visitor_entries.order('created_at DESC') if @visitor_entries.many?
end

def new
Expand All @@ -32,6 +41,14 @@ def visitor_entry_params
params.require(:visitor_entry).permit :full_name, :identity_number, :unit_id
end

def check_empty_params
params.values_at(:identity_number, :full_name, :visit_date).all?(&:blank?)
end

def find_visitor_entries(key, value)
@condo.visitor_entries.where("#{key} LIKE ?", "%#{value}%")
end

def set_condo
@condo = Condo.find(params[:condo_id])
end
Expand Down
75 changes: 43 additions & 32 deletions app/controllers/visitors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,29 +1,49 @@
class VisitorsController < ApplicationController
before_action :set_resident, only: %i[index new create]
before_action :set_condo, only: %i[find]
before_action :set_condo, only: %i[find all]
before_action :set_visitor, only: %i[confirm_entry]
before_action :authenticate_resident!, only: %i[index new create]
before_action :set_breadcrumbs_for_action, only: %i[index new create find]
before_action :authenticate_manager!, only: %i[find confirm_entry]
before_action -> { authorize_condo_manager(find_condo) }, only: %i[find confirm_entry]
before_action :set_breadcrumbs_for_action, only: %i[index new create find all]
before_action :authenticate_manager!, only: %i[find confirm_entry all]
before_action -> { authorize_condo_manager(find_condo) }, only: %i[find confirm_entry all]

def index
@visitors = @resident.visitors
return @visitors = @resident.visitors if check_empty_params

@result = []
params.permit(:visitor_name, :category).each do |key, value|
key = 'full_name' if key == 'visitor_name'
@result << search_visitors(key, value, @resident) if value.present?
end

@visitors = @result.reduce(:&)
end

def find
@date = params[:date].present? ? params[:date].to_date : Time.zone.today

if @date.past?
return redirect_to find_condo_visitors_path(@condo),
alert: I18n.t('alerts.visitor.invalid_list_date')
end
return redirect_to find_condo_visitors_path(@condo), alert: t('alerts.visitor.invalid_list_date') if @date.past?

@visitors = @condo.expected_visitors(@date)
end

def all
return @visitors = @condo.visitors if check_empty_params

@result = []
params.permit(:identity_number, :visitor_name, :resident_name, :visit_date).each do |key, value|
key = 'full_name' if key == 'visitor_name'

@result << search_visitors(key, value, @condo) if value.present?
end

@visitors = @result.reduce(:&)
end

def confirm_entry
return redirect_to find_condo_visitors_path(@visitor.condo) unless check_visitor(@visitor)
unless @visitor.visit_date == Time.zone.today && @visitor.pending?
return redirect_to find_condo_visitors_path(@visitor.condo), alert: I18n.t('alerts.visitor.entry_denied')
end

VisitorEntry.create(visitor_entry_params)
@visitor.confirmed!
Expand All @@ -46,20 +66,6 @@ def create

private

def check_visitor(visitor)
if visitor.confirmed?
flash[:alert] = I18n.t('alerts.visitor.already_confirmed')
return false
end

unless visitor.visit_date == Time.zone.today
flash[:alert] = I18n.t('alerts.visitor.invalid_date')
return false
end

true
end

def set_visit_date_job
return unless @visitor.employee?

Expand All @@ -76,10 +82,7 @@ def authenticate_resident!
return redirect_to root_path, alert: I18n.t('alerts.visitor.manager_block') if manager_signed_in?

if resident_signed_in?
if @resident.residence.nil?
return redirect_to root_path,
alert: I18n.t('alerts.visitor.residence_registration_pending')
end
return redirect_to root_path, alert: I18n.t('alerts.visitor.residence_pending') unless @resident.residence
return redirect_to root_path, alert: I18n.t('alerts.visitor.not_allowed') unless current_resident == @resident
end

Expand All @@ -105,10 +108,8 @@ def set_visitor
end

def visitor_entry_params
{
full_name: @visitor.full_name, identity_number: @visitor.identity_number,
unit_id: @visitor.resident.residence.id, condo_id: @visitor.condo_id
}
{ full_name: @visitor.full_name, identity_number: @visitor.identity_number,
unit_id: @visitor.resident.residence.id, condo_id: @visitor.condo_id }
end

def visitor_params
Expand All @@ -119,4 +120,14 @@ def visitor_params
def find_condo
@condo.nil? ? @visitor.condo : @condo
end

def check_empty_params
params.values_at(:identity_number, :visitor_name, :visit_date, :resident_name, :category).all?(&:blank?)
end

def search_visitors(key, value, model)
return model.search_visitors_by_resident_name(value) if key == 'resident_name'

model.search_visitors_by_params(key, value)
end
end
9 changes: 9 additions & 0 deletions app/models/condo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ class Condo < ApplicationRecord
has_many :managers, through: :condo_managers
has_many :announcements, dependent: :destroy
has_many :visitors, dependent: :destroy
has_many :visitor_entries, dependent: :destroy
has_many :floors, through: :towers
has_many :units, through: :floors
has_many :owners, through: :units
Expand Down Expand Up @@ -64,6 +65,14 @@ def expected_visitors(date)
visitors.where(visit_date: date)
end

def search_visitors_by_resident_name(resident_name)
visitors.joins(:resident).where('residents.full_name LIKE ?', "%#{resident_name}%")
end

def search_visitors_by_params(key, value)
visitors.where("#{key} LIKE ?", "%#{value}%")
end

def three_most_recent_announcements
announcements.order(updated_at: :desc).limit(3)
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/resident.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ def todays_visitors
visitors.where(visit_date: Date.current)
end

def search_visitors_by_params(key, value)
visitors.where("#{key} LIKE ?", "%#{value}%")
end

def description
"#{full_name} - #{email}"
end
Expand Down
4 changes: 2 additions & 2 deletions app/views/condos/dashboard/_todays_visitors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
<strong class="card-header text-center rounded-top-5 bg-light-yellow">Visitantes e funcionários esperados para hoje</strong>
<div class="card-body">
<div class="d-flex justify-content-between mx-1">
<%= link_to new_resident_visitor_path(current_resident), class:"btn btn-dark rounded-pill d-flex mb-2 shadow-sm" do %>
<%= link_to new_resident_visitor_path(current_resident), class:"btn btn-dark rounded-pill d-flex align-items-center mb-2 shadow-sm" do %>
<i class="bi bi-bookmark-plus me-2"></i> <p class="m-0 fs-sm">Cadastrar Visitante/Funcionário</p>
<% end %>
<%= link_to resident_visitors_path(current_resident), class:"btn btn-dark rounded-pill d-flex mb-2 shadow-sm" do %>
<%= link_to resident_visitors_path(current_resident), class:"btn btn-dark rounded-pill d-flex align-items-center mb-2 shadow-sm" do %>
<p class="m-0 fs-sm">Ver todos</p> <i class="bi bi-search ms-1"></i>
<% end %>
</div>
Expand Down
2 changes: 1 addition & 1 deletion app/views/condos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
<% end %>
<%= link_to find_condo_visitors_path(@condo), class:"btn py-2 btn-warning border-0 rounded-pill d-flex justify-content-center align-items-center mb-2 shadow-sm bg-light-yellow w-100" do %>
<strong>Agenda de visitantes/funcionários</strong>
<i class="bi bi-search ms-1"></i>
<i class="bi bi-people-fill ms-1"></i>
<% end %>
<% end %>
<% if resident_signed_in? && current_resident.residence&.condo == @condo %>
Expand Down
53 changes: 41 additions & 12 deletions app/views/visitor_entries/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,30 @@
<%= link_to 'Registrar nova Entrada de Visitante', new_condo_visitor_entry_path(@condo), class:'btn btn-dark rounded-pill px-4 m-2' %>
</div>

<table class="table table-sm">
<%= form_with url: condo_visitor_entries_path(@condo), method: :get do |f| %>
<div class="form-row p-2 row">
<div class="form-group col-md-4 pe-3">
<%= f.label :full_name, I18n.t("activerecord.attributes.visitor_entry.full_name") %>
<%= f.text_field :full_name, class: "form-control" %>
</div>
<div class="form-group col-md-4 pe-3">
<%= f.label :identity_number, I18n.t("activerecord.attributes.visitor_entry.identity_number") %>
<%= f.text_field :identity_number, class: "form-control" %>
</div>
<div class="form-group col-md-4 pe-3">
<%= f.label :visit_date, I18n.t("activerecord.attributes.visitor_entry.visit_date") %>
<%= f.date_field :visit_date, class: "form-control" %>
</div>
<div class="d-flex justify-content-center">
<%= f.submit "Pesquisar", class: "btn btn-dark rounded-pill d-inline-flex align-items-center mt-3 px-4 shadow-sm" %>
</div>
</div>
<% end %>
<table class="table table-hover table-sm align-middle caption-top">
<% if @result %>
<caption><%= "#{@visitor_entries.count} #{VisitorEntry.model_name.human(count: @visitor_entries.count).downcase} #{I18n.t('activerecord.attributes.visitor_entry.found', count: @visitor_entries.count).downcase}" %></caption>
<% end %>

<thead>
<tr>
<th scope="col">Nome Completo</th>
Expand All @@ -15,18 +38,24 @@
</tr>
</thead>
<tbody>
<% @visitor_entries.each do |visitor| %>
<tr>
<td><%= visitor.full_name %></td>
<td><%= visitor.identity_number %></td>
<td><%= I18n.l(visitor.created_at, format: :long) %></td>
<% if @visitor_entries.any? %>
<% @visitor_entries.each do |visitor| %>
<tr>
<td><%= visitor.full_name %></td>
<td><%= visitor.identity_number %></td>
<td><%= I18n.l(visitor.created_at, format: :long) %></td>

<% if visitor.unit.nil? %>
<td>Sem unidade referenciada</td>
<% else %>
<td><%= visitor.unit.floor.tower.name %> - <%= visitor.unit.short_identifier %></td>
<% end %>
</tr>
<% if visitor.unit.nil? %>
<td>Sem unidade referenciada</td>
<% else %>
<td><%= visitor.unit.floor.tower.name %> - <%= visitor.unit.short_identifier %></td>
<% end %>
</tr>
<% end %>
<% elsif @result %>
<p class="alert alert-warning text-center">Não foi possível encontrar entradas de visitantes com os filtros informados</p>
<% else %>
<p class="alert alert-warning text-center">No momento, não há entradas de visitantes cadastradas</p>
<% end %>
</tbody>
</table>
Expand Down
78 changes: 78 additions & 0 deletions app/views/visitors/all.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
<section class="bg-white rounded-5 py-4 px-5 shadow">

<h1>Visitantes/Funcionários cadastrados no <%= @condo.name %></h1>

<div class="d-flex justify-content-start mb-4">
<%= link_to find_condo_visitors_path(@condo), class:'btn btn-dark d-flex justify-content-center align-items-center rounded-pill px-4' do %>
Ver Agenda
<i class="bi bi-calendar ms-2 mb-1"></i>
<% end %>
</div>

<%= form_with url: all_condo_visitors_path(@condo), method: :get do |f| %>
<div class="form-row p-2 row">
<div class="form-group col-md-4 pe-3">
<%= f.label :visitor_name, I18n.t("activerecord.attributes.visitor.visitor_name") %>
<%= f.text_field :visitor_name, class: "form-control" %>
</div>
<div class="form-group col-md-2 pe-3">
<%= f.label :identity_number, I18n.t("activerecord.attributes.visitor.identity_number") %>
<%= f.text_field :identity_number, class: "form-control" %>
</div>
<div class="form-group col-md-4 pe-3">
<%= f.label :resident_name, I18n.t("activerecord.attributes.visitor.resident_name") %>
<%= f.text_field :resident_name, class: "form-control" %>
</div>
<div class="form-group col-md-2 pe-3">
<%= f.label :visit_date, I18n.t("activerecord.attributes.visitor.visit_date_query") %>
<%= f.date_field :visit_date, class: "form-control" %>
</div>
<div class="d-flex justify-content-center">
<%= f.submit "Pesquisar", class: "btn btn-dark rounded-pill d-inline-flex align-items-center mt-3 px-4 shadow-sm" %>
</div>
</div>
<% end %>

<div class="table-responsive">
<table class="table table-hover table-sm align-middle caption-top ">

<% if @result %>
<caption><%= "#{@visitors.count} #{Visitor.model_name.human(count: @visitors.count).downcase} #{I18n.t('activerecord.attributes.visitor.found', count: @visitors.count).downcase}" %></caption>
<% end %>

<thead>
<tr>
<th scope="col" class="col-3">Nome Completo</th>
<th scope="col" class="col-1">RG</th>
<th scope="col" class="col-1">Categoria</th>
<th scope="col" class="col-1">Unidade</th>
<th scope="col" class="col-3">Responsável</th>
<th scope="col" class="col-2">Data Autorizada</th>
</tr>
</thead>
<tbody>
<% if @visitors.any? %>
<% @visitors.each do |visitor| %>
<tr id="visitor-<%= visitor.id %>">
<td><%= visitor.full_name %></td>
<td><%= visitor.identity_number %></td>
<td><%= I18n.t("activerecord.attributes.visitor.categories.#{visitor.category}") %></td>
<td><%= visitor.resident.residence.tower_identifier %></td>
<td><%= visitor.resident.full_name %></td>
<td><%= I18n.l(visitor.visit_date) %>
<% if visitor.employee? %>
<span class="badge text-bg-warning ms-1 rounded-pill"><%= I18n.t("activerecord.attributes.visitor.recurrences.#{visitor.recurrence}") %></span>
<% end %>
</td>
</tr>
<% end %>
<% elsif @result %>
<p class="alert alert-warning text-center">Não foi possível encontrar visitantes com os filtros informados</p>
<% else %>
<p class="alert alert-warning text-center">No momento, não há visitantes cadastrados para este condomínio</p>
<% end %>
</tbody>
</table>
</div>
</section>
4 changes: 4 additions & 0 deletions app/views/visitors/find.html.erb
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
<section class="bg-white rounded-5 py-4 px-5 shadow">
<div class="d-flex mb-5 justify-content-between">
<h1>Agenda de visitantes/funcionários</h1>
<%= link_to all_condo_visitors_path(@condo), class:'btn btn-dark d-inline-flex align-items-center rounded-pill px-4 m-2' do %>
Ver Lista Completa
<i class="bi bi-list-ul ms-2"></i>
<% end %>
</div>

<div class="row justify-content-center">
Expand Down
Loading

0 comments on commit e54d101

Please sign in to comment.