-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #104 from TreinaDev/cadastro-visitantes-morador
Morador cadastra um visitante/funcionário
- Loading branch information
Showing
32 changed files
with
983 additions
and
38 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
class VisitorsController < ApplicationController | ||
before_action :set_resident, only: %i[index new create] | ||
before_action :authenticate_resident!, only: %i[index new create] | ||
before_action :set_breadcrumbs_for_action, only: %i[new create] | ||
|
||
def index | ||
@visitors = @resident.visitors | ||
end | ||
|
||
def new | ||
@visitor = Visitor.new | ||
end | ||
|
||
def create | ||
@visitor = Visitor.new(visitor_params) | ||
unless @visitor.save | ||
flash.now[:alert] = t('alerts.visitor.not_created') | ||
return render :new, status: :unprocessable_entity | ||
end | ||
set_visit_date_job | ||
redirect_to resident_visitors_path(@resident), notice: I18n.t('notice.visitor.created') | ||
end | ||
|
||
private | ||
|
||
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 | ||
add_breadcrumb I18n.t("breadcrumb.visitor.#{action_name}") | ||
end | ||
|
||
def authenticate_resident! | ||
return redirect_to root_path, alert: I18n.t('alerts.visitor.manager_block') if manager_signed_in? | ||
|
||
if resident_signed_in? | ||
return redirect_to root_path, alert: I18n.t('alerts.visitor.not_tenant') if @resident.residence.nil? | ||
return redirect_to root_path, alert: I18n.t('alerts.visitor.not_allowed') unless current_resident == @resident | ||
end | ||
|
||
super | ||
end | ||
|
||
def set_resident | ||
@resident = Resident.find(params[:resident_id]) | ||
end | ||
|
||
def visitor_params | ||
params.require(:visitor).permit(:full_name, :identity_number, :visit_date, :category, | ||
:recurrence).merge resident: @resident | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Controller } from "@hotwired/stimulus" | ||
|
||
// Connects to data-controller="hiddenfield" | ||
export default class extends Controller { | ||
static targets = ["role", "field"]; | ||
|
||
connect() { | ||
this.toggleField(); | ||
} | ||
|
||
toggleField() { | ||
const role = this.roleTarget.value; | ||
role === "visitor" ? this.fieldTarget.classList.add("d-none") : this.fieldTarget.classList.remove("d-none"); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
class UpdateVisitDateJob < ApplicationJob | ||
queue_as :default | ||
|
||
def perform(visitor) | ||
return unless visitor.employee? | ||
|
||
new_visit_date = visitor.next_recurrent_date | ||
|
||
return if new_visit_date.nil? | ||
|
||
visitor.update(visit_date: new_visit_date) | ||
UpdateVisitDateJob.set(wait_until: (new_visit_date + 1.day).to_datetime).perform_later(visitor) | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
class Visitor < ApplicationRecord | ||
ID_REGEX = /\A[a-zA-Z0-9]+\z/ | ||
|
||
belongs_to :resident | ||
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 } | ||
|
||
validates :visit_date, :full_name, :identity_number, :category, presence: true | ||
validate :date_is_future, on: :create | ||
validates :recurrence, presence: true, if: -> { employee? } | ||
validates :recurrence, absence: true, if: -> { visitor? } | ||
validates :identity_number, length: { in: 5..10 } | ||
validates :identity_number, | ||
format: { with: ID_REGEX, message: I18n.t('alerts.visitor.only_numbers_and_letters') } | ||
|
||
def next_recurrent_date | ||
return if once? || recurrence.nil? | ||
|
||
set_recurrence_date | ||
end | ||
|
||
private | ||
|
||
def date_is_future | ||
return unless visit_date.present? && visit_date < Time.zone.today | ||
|
||
errors.add(:visit_date, 'deve ser futura.') | ||
end | ||
|
||
def set_recurrence_date | ||
return days_recurrence if %w[daily working_days].include? recurrence | ||
return weeks_recurrence if %w[weekly biweekly].include? recurrence | ||
return months_recurrence if %w[monthly bimonthly quarterly semiannual].include? recurrence | ||
|
||
visit_date + 1.year if annual? | ||
end | ||
|
||
def days_recurrence | ||
return visit_date + 1.day if daily? | ||
|
||
visit_date.next_weekday if working_days? | ||
end | ||
|
||
def weeks_recurrence | ||
weeks_quantity = { 'weekly' => 1, 'biweekly' => 2 } | ||
visit_date + weeks_quantity[recurrence].week | ||
end | ||
|
||
def months_recurrence | ||
months_quantity = { 'monthly' => 1, 'bimonthly' => 2, 'quarterly' => 3, 'semiannual' => 6 } | ||
visit_date + months_quantity[recurrence].month | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
<div class="card rounded-5 shadow-sm" id="todays-visitors"> | ||
<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 %> | ||
<% 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 %> | ||
<p style="margin: 0; font-size: 14px;">Ver todos</p> <i class="bi bi-search ms-1"></i> | ||
<% end %> | ||
</div> | ||
|
||
<% if @todays_visitors.any? %> | ||
<% @todays_visitors.each do |visitor| %> | ||
<div class="row justify-content-between align-items-center my-1" id="visitor-<%= visitor.id %>"> | ||
<div class="col d-flex"> | ||
<i class="bi bi-person"></i> | ||
<p class="m-0 ms-1"><%= visitor.full_name %></p> | ||
</div> | ||
<span class="col-3 badge bg-dark-subtle text-dark rounded-pill me-3"><%= I18n.t("activerecord.attributes.visitor.categories.#{ visitor.category }") %></span> | ||
</div> | ||
<hr class="m-0"> | ||
<% end %> | ||
<% else %> | ||
<div class="alert alert-warning text-center mb-2"><%= I18n.t('notice.visitor.todays_empty') %></div> | ||
<% end %> | ||
</div> | ||
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
<div class='bg-white rounded-5 py-4 px-5 shadow'> | ||
|
||
<div class="d-flex mb-5 justify-content-between"> | ||
<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> | ||
</tr> | ||
<% end %> | ||
</tbody> | ||
</table> | ||
</div> |
Oops, something went wrong.