Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Administrador consulta agenda e confirma entrada #115

Merged
merged 17 commits into from
Jul 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
17 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/assets/stylesheets/_custom_spacers.scss
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ $spacers: (
3: $spacer,
4: $spacer * 1.5,
5: $spacer * 3,
6: $spacer * 5,
7: $spacer * 7,
6: $spacer * 3.5,
7: $spacer * 4,
);

@import 'bootstrap/scss/bootstrap';
4 changes: 4 additions & 0 deletions app/assets/stylesheets/application.bootstrap.scss
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,7 @@
@import 'bootstrap/scss/bootstrap';
@import 'bootstrap-icons/font/bootstrap-icons';
@import 'simple_calendar';

body {
margin-bottom: 4rem;
}
6 changes: 5 additions & 1 deletion app/controllers/condos_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ def show
@towers = @condo.towers.order :name
@common_areas = @condo.common_areas.order :name
@unit_types = @condo.unit_types.order :description
@todays_visitors = (resident_signed_in? ? current_resident.todays_visitors : [])
@todays_visitors = visitors_list(@condo)
request_bills
end

Expand Down Expand Up @@ -80,6 +80,10 @@ def set_condo
@condo = Condo.find_by(id: params[:id])
end

def visitors_list(condo)
resident_signed_in? ? current_resident.todays_visitors : condo.expected_visitors(Time.zone.today)
end

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

Expand Down
69 changes: 66 additions & 3 deletions app/controllers/visitors_controller.rb
Original file line number Diff line number Diff line change
@@ -1,12 +1,35 @@
class VisitorsController < ApplicationController
before_action :set_resident, only: %i[index new create]
before_action :set_condo, only: %i[find]
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[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]

def index
@visitors = @resident.visitors
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

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

def confirm_entry
return redirect_to find_condo_visitors_path(@visitor.condo) unless check_visitor(@visitor)

VisitorEntry.create(visitor_entry_params)
@visitor.confirmed!
redirect_to find_condo_visitors_path(@visitor.condo), notice: I18n.t('notice.visitor.entry_confirmed')
end

def new
@visitor = Visitor.new
end
Expand All @@ -23,14 +46,29 @@ 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?

UpdateVisitDateJob.set(wait_until: (@visitor.visit_date + 1.day).to_datetime).perform_later(@visitor)
end

def set_breadcrumbs_for_action
add_breadcrumb @resident.residence.condo.name, @resident.residence.condo
condo = @condo || @resident.residence.condo
add_breadcrumb condo.name, condo
add_breadcrumb I18n.t("breadcrumb.visitor.#{action_name}")
end

Expand All @@ -48,12 +86,37 @@ def authenticate_resident!
super
end

def authenticate_manager!
return redirect_to root_path, alert: I18n.t('alerts.visitor.resident_block') if resident_signed_in?

super
end

def set_resident
@resident = Resident.find(params[:resident_id])
end

def set_condo
@condo = Condo.find(params[:condo_id])
end

def set_visitor
@visitor = Visitor.find(params[:id])
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
}
end

def visitor_params
params.require(:visitor).permit(:full_name, :identity_number, :visit_date, :category,
:recurrence).merge resident: @resident
:recurrence).merge resident: @resident, condo: @resident.residence.condo
end

def find_condo
@condo.nil? ? @visitor.condo : @condo
end
end
2 changes: 1 addition & 1 deletion app/jobs/update_visit_date_job.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def perform(visitor)

return if new_visit_date.nil?

visitor.update(visit_date: new_visit_date)
visitor.update(visit_date: new_visit_date, status: :pending)
UpdateVisitDateJob.set(wait_until: (new_visit_date + 1.day).to_datetime).perform_later(visitor)
end
end
5 changes: 5 additions & 0 deletions app/models/condo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ class Condo < ApplicationRecord
has_many :unit_types, dependent: :destroy
has_many :condo_managers, dependent: :destroy
has_many :managers, through: :condo_managers
has_many :visitors, dependent: :destroy
has_many :floors, through: :towers
has_many :units, through: :floors
has_many :owners, through: :units
Expand Down Expand Up @@ -58,6 +59,10 @@ def units_json
end
end

def expected_visitors(date)
visitors.where(visit_date: date)
end

private

def ordered_units
Expand Down
3 changes: 3 additions & 0 deletions app/models/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,14 @@ class Visitor < ApplicationRecord
ID_REGEX = /\A[a-zA-Z0-9]+\z/

belongs_to :resident
belongs_to :condo
enum category: { visitor: 0, employee: 1 }

enum recurrence: { once: 0, daily: 1, working_days: 2, weekly: 3, biweekly: 4,
monthly: 5, bimonthly: 6, quarterly: 7, semiannual: 8, annual: 9 }

enum status: { pending: 0, confirmed: 1 }
RyanOxon marked this conversation as resolved.
Show resolved Hide resolved

validates :visit_date, :full_name, :identity_number, :category, presence: true
validate :date_is_future, on: :create
validates :recurrence, presence: true, if: -> { employee? }
Expand Down
8 changes: 3 additions & 5 deletions app/views/condos/dashboard/_todays_visitors.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,10 @@
<strong class="card-header text-center rounded-top-5" style="background-color: #FDE879;">Visitantes e funcionários esperados para hoje</strong>
<div class="card-body">
<div class="d-flex justify-content-between mx-1">
<% if resident_signed_in? && current_resident.residence.present? %>
<%= link_to new_resident_visitor_path(current_resident), class:"btn btn-dark rounded-pill d-flex mb-2 shadow-sm" do %>
<i class="bi bi-bookmark-plus me-2"></i> <p style="margin: 0; font-size: 14px;">Cadastrar Visitante/Funcionário</p>
<% end %>
<%= link_to new_resident_visitor_path(current_resident), class:"btn btn-dark rounded-pill d-flex mb-2 shadow-sm" do %>
<i class="bi bi-bookmark-plus me-2"></i> <p style="margin: 0; font-size: 14px;">Cadastrar Visitante/Funcionário</p>
<% end %>
<%= link_to (resident_signed_in? ? resident_visitors_path(current_resident) : root_path), 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 mb-2 shadow-sm" do %>
<p style="margin: 0; font-size: 14px;">Ver todos</p> <i class="bi bi-search ms-1"></i>
<% end %>
</div>
Expand Down
10 changes: 8 additions & 2 deletions app/views/condos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -40,8 +40,14 @@
<strong>Lista de Entradas</strong>
<i class="bi bi-search ms-1"></i>
<% end %>
<% end %>
<%= render 'condos/dashboard/todays_visitors' %>
<%= link_to find_condo_visitors_path(@condo), class:"btn py-2 rounded-pill d-flex justify-content-center align-items-center mb-2 shadow-sm", style: "width: 100%; background-color: #FDE879;" do %>
<strong>Agenda de visitantes/funcionários</strong>
<i class="bi bi-search ms-1"></i>
<% end %>
<% end %>
<% if resident_signed_in? && current_resident.residence&.condo == @condo %>
<%= render 'condos/dashboard/todays_visitors' %>
<% end %>
</div>

<div class="col">
Expand Down
55 changes: 55 additions & 0 deletions app/views/visitors/find.html.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
<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>
</div>

<div class="row justify-content-center">
<div class="col-3 d-flex justify-content-end">
<% unless @date == Time.zone.today %>
<%= link_to find_condo_visitors_path(@condo, date: @date - 1.day), class:"btn btn-dark rounded-pill d-inline-flex align-items-center mb-2 px-4 shadow-sm" do %>
<i class="bi bi-caret-left-fill mb-1 me-2"></i>
Dia Anterior
<% end %>
<% end %>
</div>
<h2 class="col-5 text-center"><%= I18n.l(@date, format: :long) %></h2>
<div class="col-3">
<%= link_to find_condo_visitors_path(@condo, date: @date + 1.day), class:"btn btn-dark rounded-pill d-inline-flex align-items-center mb-2 px-4 shadow-sm" do %>
Dia Seguinte
<i class="bi bi-caret-right-fill mb-1 ms-2"></i>
<% end %>
</div>
</div>
<div class="table-responsive">
<table class="table table-hover table-sm align-middle ">
<thead>
<tr>
<th scope="col" class="col-4">Nome Completo</th>
<th scope="col" class="col-1">RG</th>
<th scope="col" class="col-4">Responsavel</th>
<th scope="col" class="col-1">Unidade</th>
<th scope="col" class="col-2">Confirmar Entrada</th>
</tr>
</thead>
<tbody>
<% @visitors.each do |visitor| %>
<tr id="visitor-<%= visitor.id %>">
<td><%= visitor.full_name %></td>
<td class="fw-bold"><%= visitor.identity_number %></td>
<td><%= visitor.resident.full_name %></td>
<td><%= visitor.resident.residence.tower_identifier %></td>
<td>
<% if visitor.pending? %>
<%= button_to confirm_entry_visitor_path(visitor), method: :post, data: { turbo_confirm: 'Deseja registrar a entrada desse visitante?'}, class: "confirm btn btn-sm btn-light border-dark rounded-pill align-items-center justify-content-center m-0 p-0 px-6 shadow-sm" do %>
RyanOxon marked this conversation as resolved.
Show resolved Hide resolved
<i class="bi bi-check2-circle mb-1 pt-0 fs-6"></i>
<% end %>
<% else %>
<span class="badge bg-success py-2 rounded-pill">Entrada Confirmada</span>
<% end %>
</td>
</tr>
<% end %>
</tbody>
</table>
</div>
</section>
49 changes: 25 additions & 24 deletions app/views/visitors/index.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -4,29 +4,30 @@
<h1>Meus visitantes/funcionários registrados</h1>
<%= link_to 'Registrar Nova Entrada de Visitante', new_resident_visitor_path(@resident), class:'btn btn-dark rounded-pill px-4 m-2' %>
</div>

<table class="table table-sm">
<thead>
<tr>
<th scope="col">Nome Completo</th>
<th scope="col">RG</th>
<th scope="col">Categoria:</th>
<th scope="col">Proxima Data Autorizada:</th>
</tr>
</thead>
<tbody>
<% @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 class="d-flex align-items-center"><%= 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>
<div class="table-responsive">
<table class="table table-hover table-sm align-middle ">
<thead>
<tr>
<th scope="col" class="col-5">Nome Completo</th>
<th scope="col" class="col-2">RG</th>
<th scope="col" class="col-2">Categoria</th>
<th scope="col" class="col-2">Proxima Data Autorizada</th>
</tr>
<% end %>
</tbody>
</table>
</thead>
<tbody>
<% @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><%= 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 %>
</tbody>
</table>
</div>
</div>
4 changes: 4 additions & 0 deletions config/locales/breadcrumb.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -26,10 +26,14 @@ pt-BR:
new: 'Adicionar Propriedade'
tenant:
new: 'Adicionar Moradia'
visitor_entry:
new: 'Cadastrar Entrada de Visitante'
index: 'Listagem de Entradas de Visitantes'
visitor:
new: 'Cadastrar Visitante/Funcionário'
create: 'Cadastrar Visitante/Funcionário'
index: 'Listagem de Visitantes/Funcionários'
find: 'Agenda de Visitantes/Funcionários'
bill:
index: 'Lista de faturas'
show: 'Detalhes de fatura'
5 changes: 5 additions & 0 deletions config/locales/models/visitor.pt-br.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,17 @@ pt-BR:
not_created: 'Erro ao registrar visitante'
residence_registration_pending: 'Apenas moradores podem administrar visitantes'
manager_block: 'Um administrador não pode administrar visitantes para uma unidade'
resident_block: 'Você não possui autorização para essa ação'
RyanOxon marked this conversation as resolved.
Show resolved Hide resolved
not_allowed: 'Você não pode administrar visitantes para outra unidade além da sua'
only_numbers_and_letters: 'só pode ter números e letras'
already_confirmed: 'Visitante já confirmado'
invalid_date: 'Só é possível confirmar visitantes do dia atual'
invalid_list_date: 'Não é possível acessar uma data passada'
notice:
visitor:
created: 'Visitante cadastrado com sucesso'
todays_empty: 'Não há visitantes/funcionários esperados para hoje.'
entry_confirmed: 'Entrada do visitante registrada com sucesso.'
activerecord:
models:
visitor: Visitante
Expand Down
8 changes: 8 additions & 0 deletions config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
patch 'update_photo', on: :member
end

resources :visitors do
post 'confirm_entry', on: :member
end

resource :units do
get 'find_units', on: :collection
end
Expand All @@ -42,6 +46,10 @@
resources :unit_types, only: [:new, :create]
resources :visitor_entries, only: [:index, :new, :create]

resources :visitors do
get 'find', on: :collection
end

resources :towers, only: [:new, :create] do
member do
get :edit_floor_units
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240717234007_add_condo_to_visitors.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddCondoToVisitors < ActiveRecord::Migration[7.1]
def change
add_reference :visitors, :condo, null: false, foreign_key: true
end
end
Loading
Loading