Skip to content

Commit

Permalink
Merge pull request #149 from TreinaDev/job-cadastro-sindico
Browse files Browse the repository at this point in the history
Incrementa o active Job no cadastro de sindico
  • Loading branch information
DaniloRibeiro07 authored Jul 22, 2024
2 parents 0e0f69b + 5d16998 commit b5e1ffa
Show file tree
Hide file tree
Showing 23 changed files with 349 additions and 188 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
[![Issues][issues-shield]][issues-url]
[![MIT License][license-shield]][license-url]
[![Status][status-shield]][status-url]
<img src="http://img.shields.io/static/v1?label=Test%20Coverage&message=97.77%&color=green&style=for-the-badge"/>
<img src="http://img.shields.io/static/v1?label=Test%20Coverage&message=97.75%&color=green&style=for-the-badge"/>
<img src="http://img.shields.io/static/v1?label=Tests&message=450&color=green&style=for-the-badge"/>

</div>
Expand Down
30 changes: 11 additions & 19 deletions app/controllers/superintendents_controller.rb
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
class SuperintendentsController < ApplicationController
before_action :authenticate_manager!, only: %i[new create edit update]
before_action :set_condo, only: %i[show new create edit update]
before_action :set_superintendent, only: %i[show edit update]
before_action :set_breadcrumbs_condo, only: %i[new create edit update]
before_action :authenticate_manager!, only: %i[new create]
before_action :set_condo, only: %i[show new create destroy]
before_action :set_superintendent, only: %i[show destroy]
before_action :set_breadcrumbs_condo, only: %i[new create]
before_action :set_breadcrumbs_for_register, only: %i[new create]
before_action :set_breadcrumbs_for_edit, only: %i[edit update]
before_action -> { authorize_user(@condo) }, only: %i[show new create edit update]
before_action :set_breadcrumbs_for_edit, only: %i[]
before_action -> { authorize_condo_manager(@condo) }, only: %i[destroy]
before_action -> { authorize_user(@condo) }, only: %i[show new create]

def show
@tenant = @superintendent.tenant
Expand All @@ -23,12 +24,8 @@ def new
@tenants = @condo.tenants
end

def edit
@tenants = @condo.tenants
end

def create
@superintendent = Superintendent.new(superintendent_params.merge!(condo: @condo))
@superintendent = Superintendent.new(superintendent_params.merge!({ status: :pending, condo: @condo }))

unless @superintendent.save
@tenants = @condo.tenants
Expand All @@ -39,14 +36,9 @@ def create
redirect_to condo_superintendent_path(@condo, @superintendent), notice: t('notices.superintendent.created')
end

def update
unless @superintendent.update(superintendent_params)
@tenants = @condo.tenants
flash.now[:alert] = t('alerts.superintendent.not_updated')
return render 'new', status: :unprocessable_entity
end

redirect_to condo_superintendent_path(@condo, @superintendent), notice: t('notices.superintendent.updated')
def destroy
@superintendent.closed!
redirect_to @condo, notice: t('notices.superintendent.closed')
end

private
Expand Down
9 changes: 9 additions & 0 deletions app/jobs/active_superintendent_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class ActiveSuperintendentJob < ApplicationJob
queue_as :default

def perform(superintendent)
return unless superintendent.pending? && superintendent.start_date == Date.current

superintendent.in_action!
end
end
9 changes: 9 additions & 0 deletions app/jobs/desactive_superintendent_job.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class DesactiveSuperintendentJob < ApplicationJob
queue_as :default

def perform(superintendent)
return unless superintendent.in_action? && superintendent.end_date == Date.current

superintendent.closed!
end
end
6 changes: 5 additions & 1 deletion app/models/condo.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ class Condo < ApplicationRecord
has_many :units, through: :floors
has_many :owners, through: :units
has_many :tenants, through: :units
has_one :superintendent, dependent: :destroy
has_many :superintendents, dependent: :destroy

delegate :city, to: :address
delegate :state, to: :address
Expand All @@ -23,6 +23,10 @@ class Condo < ApplicationRecord

accepts_nested_attributes_for :address

def superintendent
superintendents.not_closed[0]
end

def residents
(tenants + owners).uniq
end
Expand Down
20 changes: 19 additions & 1 deletion app/models/superintendent.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,25 @@ class Superintendent < ApplicationRecord

validates :start_date, :end_date, presence: true

validate :start_date_must_be_valid, :end_date_must_be_valid
validate :start_date_must_be_valid, :end_date_must_be_valid, on: :create

enum status: { pending: 0, in_action: 1, closed: 2 }

after_create :program_activate_and_desactiation?

def condo_presentation
"#{tenant.full_name} (#{start_date} - #{end_date})"
end

private

def program_activate_and_desactiation?
DesactiveSuperintendentJob.set(wait_until: end_date.to_datetime).perform_later self

return in_action! if start_date == Date.current

ActiveSuperintendentJob.set(wait_until: start_date.to_datetime).perform_later self
end

def start_date_must_be_valid
errors.add :start_date, 'deve ser atual ou futura' if start_date&.past?
Expand Down
4 changes: 3 additions & 1 deletion app/views/condos/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@
<p class="mb-1 text-secondary fs-sm">CNPJ: <%= @condo.registration_number %></p>
<p class="mb-1 text-secondary fs-sm">Síndico:
<% if @condo.superintendent%>
<%= link_to @condo.superintendent.tenant.full_name, condo_superintendent_path(@condo, @condo.superintendent) %>
<%= link_to condo_superintendent_path(@condo, @condo.superintendent) do %>
<%= @condo.superintendent.condo_presentation%>
<% end %>
<% else %>
Não há um síndico ativo
<% end %>
Expand Down
3 changes: 0 additions & 3 deletions app/views/superintendents/edit.html.erb

This file was deleted.

6 changes: 3 additions & 3 deletions app/views/superintendents/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -19,16 +19,16 @@
<div>
<p><strong>Torre:</strong> <%= @tenant.residence.tower.name %></p>
<p><strong>Unidade:</strong> <%= @tenant.residence.short_identifier %></p>
<p><strong>Ínicio do mandato:</strong> <%= I18n.l(@superintendent.start_date) %></p>
<p><strong>Início do mandato:</strong> <%= I18n.l(@superintendent.start_date) %></p>
<p><strong>Fim do mandato:</strong> <%= I18n.l(@superintendent.end_date) %></p>
</div>
</div>
</div>

<% if manager_signed_in? %>
<div class="form-group d-flex justify-content-center">
<%= button_to 'Editar Síndico', edit_condo_superintendent_path(@condo, @superintendent), method: :get, class:'btn btn-dark rounded-pill px-4 mt-3' %>
</div>
<%= button_to 'Encerrar Gestão', condo_superintendent_path(@condo, @superintendent), method: :delete, class:'btn btn-dark rounded-pill px-4 mt-3' %>
</div>
<% end %>

</div>
3 changes: 2 additions & 1 deletion config/locales/models/superintendent.pt-BR.yml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ pt-BR:
superintendent:
created: 'Mandato de síndico cadastro com sucesso!'
updated: 'Mandato de síndico atualizado com sucesso!'
closed: 'Mandato de síndico encerrado com sucesso!'
alerts:
superintendent:
not_created: 'Não foi possível cadastrar o mandato.'
Expand All @@ -15,7 +16,7 @@ pt-BR:
other: 'Síndicos'
attributes:
superintendent:
start_date: 'Data de ínicio'
start_date: 'Data de início'
end_date: 'Data de conclusão'
tenant_id: 'Morador'
tenant: 'Morador'
2 changes: 1 addition & 1 deletion config/routes.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@
resources :common_areas, only: [:new, :create]
resources :unit_types, only: [:new, :create]
resources :visitor_entries, only: [:index, :new, :create]
resources :superintendents, only: [:show, :new, :create, :edit, :update]
resources :superintendents, only: [:show, :new, :create, :destroy]
resources :announcements, only: [:index, :new, :create]

resources :visitors do
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240722002636_add_status_to_superintendent.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddStatusToSuperintendent < ActiveRecord::Migration[7.1]
def change
add_column :superintendents, :status, :integer
end
end
1 change: 1 addition & 0 deletions db/schema.rb

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

39 changes: 39 additions & 0 deletions spec/jobs/active_superintendent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'

RSpec.describe ActiveSuperintendentJob, type: :job do
context 'must be activate the superintendent' do
it 'in two days' do
travel_to '2024-07-22'
superintendent = create :superintendent, :pending, start_date: '2024-07-24'.to_date,
end_date: '2024-08-25'.to_date

travel_to '2024-07-24'
ActiveSuperintendentJob.perform_now(superintendent)
expect(superintendent.in_action?).to eq true
end

it 'does not update if the accused is on another day' do
travel_to '2024-07-22'
superintendent = create :superintendent, :pending, start_date: '2024-07-24'.to_date,
end_date: '2024-08-25'.to_date

travel_to '2024-07-23'
ActiveSuperintendentJob.perform_now(superintendent)
expect(superintendent.in_action?).to eq false
end

it 'and the status needs to be pending to update' do
travel_to '2024-07-20'
superintendent_action = create :superintendent, :in_action, start_date: '2024-07-23'.to_date,
end_date: '2024-08-25'.to_date
superintendent_closed = create :superintendent, :closed, start_date: '2024-07-22'.to_date,
end_date: '2024-08-23'.to_date

travel_to '2024-07-24'
ActiveSuperintendentJob.perform_now(superintendent_action)
ActiveSuperintendentJob.perform_now(superintendent_closed)
expect(superintendent_action.in_action?).to eq true
expect(superintendent_closed.closed?).to eq true
end
end
end
39 changes: 39 additions & 0 deletions spec/jobs/desactive_superintendent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
require 'rails_helper'

RSpec.describe DesactiveSuperintendentJob, type: :job do
context 'must be desactivate the superintendent' do
it 'in two days' do
travel_to '2024-07-22'
superintendent = create :superintendent, :in_action, start_date: '2024-07-22'.to_date,
end_date: '2024-07-24'.to_date

travel_to '2024-07-24'
DesactiveSuperintendentJob.perform_now(superintendent)
expect(superintendent.closed?).to eq true
end

it 'does not update if the accused is on another day' do
travel_to '2024-07-22'
superintendent = create :superintendent, :in_action, start_date: '2024-07-22'.to_date,
end_date: '2024-07-24'.to_date

travel_to '2024-07-23'
DesactiveSuperintendentJob.perform_now(superintendent)
expect(superintendent.closed?).to eq false
end

it 'and the status needs to be in_action to update' do
travel_to '2024-07-20'
superintendent_action = create :superintendent, :pending, start_date: '2024-07-23'.to_date,
end_date: '2024-07-24'.to_date
superintendent_closed = create :superintendent, :closed, start_date: '2024-07-22'.to_date,
end_date: '2024-07-24'.to_date

travel_to '2024-07-24'
DesactiveSuperintendentJob.perform_now(superintendent_action)
DesactiveSuperintendentJob.perform_now(superintendent_closed)
expect(superintendent_action.pending?).to eq true
expect(superintendent_closed.closed?).to eq true
end
end
end
25 changes: 25 additions & 0 deletions spec/models/condo_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,29 @@
expect(condo.residents).not_to include not_resident
end
end

describe 'superintendent' do
it 'need to display only the superintendent in action or pending (action)' do
condo = create :condo
travel_to '2024-07-22'.to_date

create(:superintendent, :closed, condo:, start_date: '2024-07-24', end_date: '2024-07-25')
superintendent_in_action = create(:superintendent, condo:, start_date: '2024-07-22', end_date: '2024-07-25')
create(:superintendent, :closed, condo:, start_date: '2024-07-24', end_date: '2024-07-25')

expect(condo.superintendent).to eq superintendent_in_action
end

it 'need to display only the superintendent in action or pending (pending)' do
condo = create :condo
travel_to '2024-07-22'.to_date

create(:superintendent, :closed, condo:, start_date: '2024-07-24', end_date: '2024-07-25')
superintendent_pending = create(:superintendent, :pending, condo:, start_date: '2024-07-23',
end_date: '2024-07-25')
create(:superintendent, :closed, condo:, start_date: '2024-07-24', end_date: '2024-07-25')

expect(condo.superintendent).to eq superintendent_pending
end
end
end
34 changes: 34 additions & 0 deletions spec/models/superintendent_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -26,4 +26,38 @@
expect(superintendent.errors).to include :end_date
end
end

it '#condo_presentation' do
travel_to '2024-07-22'
condo = create :condo, name: 'Condominio X'
resident = create(:resident, :with_residence, condo:, full_name: 'Adroaldo')
superintendent = create(:superintendent, :pending, tenant: resident,
condo:, start_date: '2024-07-22',
end_date: '2024-07-25')

expect(superintendent.condo_presentation).to eq 'Adroaldo (2024-07-22 - 2024-07-25)'
end

describe '#after_create' do
it 'superintendent action now' do
travel_to '2024-07-22'
superintendent = create :superintendent, :pending, start_date: '2024-07-22', end_date: '2024-07-25'

expect(superintendent.in_action?).to eq true
end

it 'program_activate_and_desactiation' do
travel_to '2024-07-22'
active_superintendent_job_spy = spy 'ActiveSuperintendentJob'
stub_const 'ActiveSuperintendentJob', active_superintendent_job_spy
superintendent = create :superintendent, :pending, start_date: '2024-07-23', end_date: '2024-07-25'
desactive_superintendent_job_spy = spy 'DesactiveSuperintendentJob'
stub_const 'DesactiveSuperintendentJob', desactive_superintendent_job_spy
create :superintendent, start_date: '2024-07-22', end_date: '2024-07-25'

expect(superintendent.pending?).to eq true
expect(active_superintendent_job_spy).to have_received(:set).with({ wait_until: '2024-07-23'.to_datetime })
expect(desactive_superintendent_job_spy).to have_received(:set).with({ wait_until: '2024-07-25'.to_datetime })
end
end
end
41 changes: 41 additions & 0 deletions spec/requests/manager_closed_superintendent_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
require 'rails_helper'

describe 'Manager closed superintendent' do
context 'DELETE /condos/:condo_id/superintendents/:id' do
it 'must be authenticated to closed an superintendent' do
condo = create :condo
superintendent = create :superintendent

delete condo_superintendent_path(condo, superintendent)

expect(response).to redirect_to root_path
expect(Superintendent.last).not_to eq nil
end

it 'must be authenticated as condo manager to register a superintendent' do
manager = create :manager, is_super: false
condo = create :condo
superintendent = create :superintendent

login_as manager, scope: :manager

delete condo_superintendent_path(condo, superintendent)

expect(response).to redirect_to root_path
expect(Superintendent.last).not_to eq nil
end

it 'must be authenticated as manager' do
resident = create :resident
condo = create :condo
superintendent = create :superintendent

login_as resident, scope: :resident

delete condo_superintendent_path(condo, superintendent)

expect(response).to redirect_to root_path
expect(Superintendent.last).not_to eq nil
end
end
end
Loading

0 comments on commit b5e1ffa

Please sign in to comment.