Skip to content

Commit

Permalink
Merge pull request #146 from lsa-mis/LRA-957-room-ready-delete-and-ar…
Browse files Browse the repository at this point in the history
…chive-resources

Lra 957 room ready delete and archive resources
  • Loading branch information
britaumich authored Jul 9, 2024
2 parents b59d8a9 + c0d4778 commit 467f0d4
Show file tree
Hide file tree
Showing 12 changed files with 98 additions and 63 deletions.
2 changes: 1 addition & 1 deletion app/controllers/application_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ def redirect_rover_to_correct_state(room:, room_state:, step:, mode: "new")

CommonAttribute.active.present? ? redirect["common_attributes"] = true : redirect["common_attributes"] = false
room.active_specific_attributes.present? ? redirect["specific_attributes"] = true : redirect["specific_attributes"] = false
room.resources.present? ? redirect["resources"] = true : redirect["resources"] = false
room.active_resources.present? ? redirect["resources"] = true : redirect["resources"] = false

new_path_to_redirect = {
"common_attributes" => new_common_attribute_state_path(room_state_id: room_state.id),
Expand Down
101 changes: 55 additions & 46 deletions app/controllers/reports_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -38,13 +38,14 @@ def room_issues_report
if params[:commit]
zone_id, building_id, start_time, end_time = collect_form_params

rooms = Room.joins(floor: :building).joins(:room_tickets)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_tickets: { created_at: start_time..end_time })
.group('rooms.id')
.select('rooms.*, COUNT(room_tickets.id) AS tickets_count')
.having('COUNT(room_tickets.id) > 0')
.order('tickets_count DESC')
rooms = Room.active
.joins(floor: :building).joins(:room_tickets)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_tickets: { created_at: start_time..end_time })
.group('rooms.id')
.select('rooms.*, COUNT(room_tickets.id) AS tickets_count')
.having('COUNT(room_tickets.id) > 0')
.order('tickets_count DESC')

if rooms.any?
@title = 'Room Issues Report'
Expand Down Expand Up @@ -75,22 +76,24 @@ def inspection_rate_report
if params[:commit]
zone_id, building_id, start_time, end_time = collect_form_params

rooms = Room.joins(floor: { building: :zone }).joins(:room_states)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.group('rooms.id')
.select('rooms.*')
.select('COUNT(room_states.id) AS room_check_count')
.order('room_check_count DESC')

rooms_no_room_state = Room.left_outer_joins(:room_states)
.joins(floor: { building: :zone })
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { id: nil })
.where.not(id: rooms.map(&:id))
.select('rooms.*')
.select('0 AS room_check_count')
.order('rooms.room_number')
rooms = Room.active
.joins(floor: { building: :zone }).joins(:room_states)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.group('rooms.id')
.select('rooms.*')
.select('COUNT(room_states.id) AS room_check_count')
.order('room_check_count DESC')

rooms_no_room_state = Room.active
.left_outer_joins(:room_states)
.joins(floor: { building: :zone })
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { id: nil })
.where.not(id: rooms.map(&:id))
.select('rooms.*')
.select('0 AS room_check_count')
.order('rooms.room_number')

if rooms.any?
oldest_record = rooms.min_by { |room| room.room_states.first.updated_at }
Expand Down Expand Up @@ -130,17 +133,18 @@ def no_access_report
if params[:commit]
zone_id, building_id, start_time, end_time = collect_form_params

rooms = Room.joins(floor: :building).joins(:room_states)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.where(room_states: { is_accessed: false })
.group('rooms.id')
.select('rooms.*')
.select('COUNT(room_states.id) AS na_states_count')
.select('array_agg(room_states.updated_at) as na_states_dates')
.select('array_agg(room_states.no_access_reason) as na_states_reasons')
.having('COUNT(room_states.id) > 0')
.order('na_states_count DESC')
rooms = Room.active
.joins(floor: :building).joins(:room_states)
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.where(room_states: { is_accessed: false })
.group('rooms.id')
.select('rooms.*')
.select('COUNT(room_states.id) AS na_states_count')
.select('array_agg(room_states.updated_at) as na_states_dates')
.select('array_agg(room_states.no_access_reason) as na_states_reasons')
.having('COUNT(room_states.id) > 0')
.order('na_states_count DESC')

if rooms.any?
@title = 'No Access Report'
Expand Down Expand Up @@ -177,16 +181,18 @@ def common_attribute_states_report
if params[:commit]
zone_id, building_id, start_time, end_time = collect_form_params

rooms = Room.joins(floor: { building: :zone }).joins(room_states: { common_attribute_states: :common_attribute })
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('room_states.updated_at')
.select('common_attributes.description AS common_attribute_description')
.select('common_attributes.need_checkbox as need_checkbox')
.select('common_attribute_states.checkbox_value as checkbox_value')
.select('common_attribute_states.quantity_box_value as quantity_box_value')
.order('zones.name ASC, buildings.name ASC, rooms.room_number ASC')
rooms = Room.active
.joins(floor: { building: :zone }).joins(room_states: { common_attribute_states: :common_attribute })
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
.select('room_states.updated_at')
.select('common_attributes.description AS common_attribute_description')
.select('common_attributes.need_checkbox as need_checkbox')
.select('common_attribute_states.checkbox_value as checkbox_value')
.select('common_attribute_states.quantity_box_value as quantity_box_value')
.where('common_attributes.archived = FALSE')
.order('zones.name ASC, buildings.name ASC, rooms.room_number ASC')

if rooms.any?
@grouped = true
Expand Down Expand Up @@ -223,7 +229,8 @@ def specific_attribute_states_report
if params[:commit]
zone_id, building_id, start_time, end_time = collect_form_params

rooms = Room.joins(floor: { building: :zone }).joins(room_states: { specific_attribute_states: :specific_attribute })
rooms = Room.active
.joins(floor: { building: :zone }).joins(room_states: { specific_attribute_states: :specific_attribute })
.where(buildings: { id: building_id, zone_id: zone_id })
.where(room_states: { updated_at: start_time..end_time })
.select('rooms.*')
Expand All @@ -232,6 +239,7 @@ def specific_attribute_states_report
.select('specific_attributes.need_checkbox as need_checkbox')
.select('specific_attribute_states.checkbox_value as checkbox_value')
.select('specific_attribute_states.quantity_box_value as quantity_box_value')
.where('specific_attributes.archived = FALSE')
.order('zones.name ASC, buildings.name ASC, rooms.room_number ASC, specific_attributes.description ASC')

if rooms.any?
Expand Down Expand Up @@ -280,6 +288,7 @@ def resource_states_report
.select("resources.resource_type as resource_type")
.select('room_states.updated_at')
.select('resource_states.is_checked as check_value')
.where('resources.archived = FALSE')
.where("resources.resource_type ILIKE ?", "%#{resource_type}%") # need to do a manual query for this because of circular definition of resources
.order('zones.name ASC, buildings.name ASC, rooms.room_number ASC, resources.name ASC')

Expand Down Expand Up @@ -316,7 +325,7 @@ def resource_states_report

def set_form_values
@zones = Zone.all.order(:name).map { |z| [z.name, z.id] }
@buildings = Building.where.not(zone: nil).map { |building| [building.zone_id, building.name, building.id] }
@buildings = Building.active.where.not(zone: nil).map { |building| [building.zone_id, building.name, building.id] }
end

def collect_form_params
Expand Down
2 changes: 1 addition & 1 deletion app/controllers/resource_states_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ class ResourceStatesController < ApplicationController
def new
authorize ResourceState

@resource_states = @room.resources.all.map do |resource|
@resource_states = @room.active_resources.all.map do |resource|
resource.resource_states.new
end
end
Expand Down
4 changes: 4 additions & 0 deletions app/models/resource.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# room_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
# archived :boolean default(FALSE)
#
class Resource < ApplicationRecord
belongs_to :room
Expand All @@ -17,6 +18,9 @@ class Resource < ApplicationRecord
validates :resource_type, presence: true
validates :room_id, presence: true

scope :active, -> { where(archived: false) }
scope :archived, -> { where(archived: true) }

def display_name
"#{self.name} - #{self.resource_type}"
end
Expand Down
2 changes: 2 additions & 0 deletions app/models/room.rb
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ class Room < ApplicationRecord
has_many :specific_attributes
has_many :active_specific_attributes, -> { active }, class_name: 'SpecificAttribute'
has_many :archived_specific_attributes, -> { archived }, class_name: 'SpecificAttribute'
has_many :active_resources, -> { active }, class_name: 'Resource'
has_many :archived_resources, -> { archived }, class_name: 'Resource'
has_many :room_states
has_many :notes

Expand Down
2 changes: 1 addition & 1 deletion app/models/room_status.rb
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ def specific_attributes_exist?
end

def resources_exist?
@room.resources.count > 0
@room.active_resources.count > 0
end

def status_weight
Expand Down
2 changes: 1 addition & 1 deletion app/views/rooms/show.html.erb
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
<div class="d-flex flex-row align-items-center justify-content-between">
<h2>Resources</h2>
</div>
<%= render 'resources_list', resources: @room.resources %>
<%= render 'resources_list', resources: @room.active_resources %>
</div>

<% if is_admin? %>
Expand Down
5 changes: 5 additions & 0 deletions db/migrate/20240709143456_add_archive_field_to_resource.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
class AddArchiveFieldToResource < ActiveRecord::Migration[7.1]
def change
add_column :resources, :archived, :boolean, default: false
end
end
3 changes: 2 additions & 1 deletion db/schema.rb

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

36 changes: 24 additions & 12 deletions lib/tasks/update_resources.rake
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ task update_resources: :environment do
end

begin
rooms = Room.all.pluck(:rmrecnbr)
rooms = Room.active.pluck(:rmrecnbr)

#=============================
# get oids from api #
Expand Down Expand Up @@ -41,7 +41,7 @@ task update_resources: :environment do
#=============================
# update the database #
#=============================
rooms_to_update = Room.all.pluck(:rmrecnbr)
rooms_to_update = Room.active.pluck(:rmrecnbr)

# convert wco_resources to hash of arrays: array of resources for every room
resources = wco_resources
Expand Down Expand Up @@ -80,24 +80,36 @@ def update_resources_in_db(resources, room_location, type_names, rooms_to_update
room_rmrecnbr = room_location[oid]
room = Room.find_by(rmrecnbr: room_rmrecnbr)
if room.present?
# hash of resources for the room that exist in db {name => id}
resources_in_db = room.resources.pluck(:name, :id).to_h
# hashes of resources for the room that exist in db {name => id}
active_resources_in_db = room.active_resources.pluck(:name, :id).to_h
archived_resources_in_db = room.archived_resources.pluck(:name, :id).to_h

room_resources.each do |resource_name, resource_type|
if resources_in_db.key?(resource_name)
next unless type_names.include?(resource_type)

if active_resources_in_db.key?(resource_name)
# wco resource exist in db, delete from array
resources_in_db.delete(resource_name)
active_resources_in_db.delete(resource_name)
elsif archived_resources_in_db.key?(resource_name)
# wco resource is archived in db, so unarchive it
id = archived_resources_in_db[resource_name]
room.resources.find(id).update(archived: false)
else
# create a wco resource that was not present in db
if type_names.include?(resource_type)
room.resources.create(name: resource_name, resource_type: resource_type)
end
room.resources.create(name: resource_name, resource_type: resource_type)
end
end
if resources_in_db.present?
# these resoursces are not present in sco any more - delete from db
Resource.where(id: resources_in_db.values).delete_all

active_resources_in_db.each do |_, id|
# these resoursces are not present in sco any more, so archive or delete appropriately
resource = room.resources.find(id)
if resource.resource_states.any?
resource.update(archived: true)
else
resource.destroy
end
end

# room updated by wco - delete from list
rooms_to_update.delete(room_rmrecnbr)
end
Expand Down
1 change: 1 addition & 0 deletions spec/factories/resources.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# room_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
# archived :boolean default(FALSE)
#
FactoryBot.define do
factory :resource do
Expand Down
1 change: 1 addition & 0 deletions spec/models/resource_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
# room_id :bigint not null
# created_at :datetime not null
# updated_at :datetime not null
# archived :boolean default(FALSE)
#
require 'rails_helper'

Expand Down

0 comments on commit 467f0d4

Please sign in to comment.