Skip to content

305 Admin View of Tables #117

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

Open
wants to merge 17 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
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
1 change: 1 addition & 0 deletions data/updates/0024_add_checkable_tool_user.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
ALTER TABLE `tools` ADD COLUMN `current_user_id` int(11) DEFAULT NULL;
9 changes: 9 additions & 0 deletions models/tool.rb
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,15 @@ def update_last_checked_out()
self.save
end

def update_current_user(user:, is_checking_in:)
if is_checking_in
self.current_user_id = nil
else
self.current_user_id = user.id
end
self.save
end

def set_data(params)
self.tool_name = params[:name]
self.category_id = params[:category_id].to_i
Expand Down
68 changes: 57 additions & 11 deletions routes/checkout.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,17 +62,15 @@
available_tools = Tool.all.select { |tool| tool.last_checked_out.nil? || tool.last_checked_in > tool.last_checked_out}

# select the logs from tool logs where the user is checkout_user and the is_checking_in is false and is the most recent log for each tool id
user_checked_out = ToolLog.where(checkout_user_id: checkout_user.id, is_checking_in: false)
tools_user_checked_out = Tool.where(current_user_id: checkout_user.id)

if user_checked_out.nil?
user_checked_out = []
if tools_user_checked_out.nil?
tools_user_checked_out = []
end

tools_checked_out = Tool.where(id: user_checked_out.pluck(:tool_id))
tools_checked_out = tools_checked_out.select { |tool| tool.last_checked_out > tool.last_checked_in }

if search_tool_id && !search_tool_id.strip.empty?
available_tools = available_tools.select { |tool| tool.serial_number == search_tool_id }
tools_checked_out = tools_checked_out.select { |tool| tool.serial_number == search_tool_id }
tools_user_checked_out = tools_checked_out.select { |tool| tool.serial_number == search_tool_id }
end

user_event_signups = EventSignup.where(user_id: checkout_user.id, attended: 0)
Expand All @@ -82,13 +80,55 @@
erb :"engineering_garage/checkout_user", :layout => :fixed, locals: {
:user => checkout_user,
:nuid => nuid,
:checked_out => tools_checked_out,
:checked_out => tools_user_checked_out,
:projects => projects,
:tools => available_tools,
:events => user_events,
}
end

get "/checkout/warehouse/" do
@breadcrumbs << { :text => "Checkout" }
# currently: Several things in the checkout use nuid to find the project. Recommended approach: For each of the projects, make a dictonary entry that gives the nuid, and pass that into the all.
require_login
search_project_id = params[:search_project_id]
search_tool_id = params[:search_tool_id]

projects = Project.all

if search_project_id && !search_project_id.strip.empty?
projects = projects.where(bin_id: search_project_id.strip)
end

checked_out_tools = Tool.all.select { |tool| tool.is_checked_out }

checked_out_tools = checked_out_tools.sort_by { |tool| tool.last_checked_out }.reverse

if search_tool_id && !search_tool_id.strip.empty?
search_tool_id = search_tool_id.downcase
checked_out_tools = checked_out_tools.select { |tool| tool.serial_number.downcase.strip == search_tool_id.downcase.strip }
end

project_owners = {}

current_tool_users = {}

projects.each do |project|
project_owners[project.id] = User.find_by(id: project.owner_user_id)
end

checked_out_tools.each do |tool|
current_tool_users[tool.id] = User.find_by(id: tool.current_user_id)
end

erb :"engineering_garage/checkout_all", :layout => :fixed, locals: {
:checked_out => checked_out_tools,
:projects => projects,
:project_owners => project_owners,
:current_tool_users => current_tool_users
}
end

post '/checkout/events/:event_id/:user_id/' do
new_member_orientation_id = EventType.find_by(:description => 'New Member Orientation', :service_space_id => SS_ID).id
tool_training_event_id = EventType.find_by(:description => 'Machine Training', :service_space_id => SS_ID).id
Expand Down Expand Up @@ -291,6 +331,10 @@
user = User.find_by(user_nuid: params[:nuid])
owner = User.find_by(id: project.owner_user_id)
teammates = ProjectTeammate.where("project_id = ?", params[:project_id])
return_to_warehouse = false
if !params[:returning_to_warehouse].nil?
return_to_warehouse = true
end

erb :'engineering_garage/edit_project', :layout => :fixed, :locals => {
:owner => owner,
Expand All @@ -300,6 +344,7 @@
:bin_id => project.bin_id,
:teammates => teammates,
:project_id => project.id,
:return_to_warehouse => return_to_warehouse
}
end

Expand Down Expand Up @@ -439,6 +484,7 @@
end

tool.update_last_checked_out
tool.update_current_user(user: user, is_checking_in: false)
tool_log = ToolLog.new
tool_log.set_data(user: user, tool: tool, is_checking_in: false)

Expand All @@ -448,15 +494,14 @@

# Tool Checkin
post "/checkout/tool_checkin/?" do
nuid = params[:nuid]
tool_id = params[:tool_id]
if nuid.nil? || tool_id.nil?
if tool_id.nil?
flash :danger, "Error", "NUID or Tool ID not found"
redirect "/checkout/"
end

tool = Tool.find_by(id: tool_id)
user = User.find_by(user_nuid: nuid)
user = User.find_by(id: tool.current_user_id)
if tool.nil?
flash :danger, "Error", "Tool not found"
redirect "/checkout/"
Expand All @@ -466,6 +511,7 @@
end

tool.update_last_checked_in
tool.update_current_user(user: user, is_checking_in: true)
tool_log = ToolLog.new
tool_log.set_data(user: user, tool: tool, is_checking_in: true)
flash :success, "Success", "Tool checked in"
Expand Down
25 changes: 25 additions & 0 deletions scripts/adapt_tools_to_current_user.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
require 'bundler/setup'

require_relative '../utils/language'
ENV['RACK_ENV'] ||= 'development'

require_relative '../utils/config_loader'
require 'utils/database'

require 'models/tool'
require 'models/tool_log'

userless_tools = Tool.where("current_user_id IS NULL AND last_checked_out > last_checked_in")

userless_tools.each do |tool|
tool_checkouts = ToolLog.where(tool_id: tool.id, is_checking_in: 0)
most_recent_checkout = tool_checkouts.max_by(&:checked_date)

if most_recent_checkout
tool.current_user_id = most_recent_checkout.checkout_user_id
tool.save
puts "Updated Tool ##{tool.id} with User ##{most_recent_checkout.checkout_user_id}"
else
puts "No valid checkouts found for Tool ##{tool.id}"
end
end
6 changes: 5 additions & 1 deletion views/engineering_garage/checkout.erb
Original file line number Diff line number Diff line change
@@ -1,12 +1,16 @@
<h1 class="dcf-txt-h3" id="Checkout">Warehouse Check In / Check Out</h1>
<form class="dcf-form dcf-w-25%" id="find-nuid" action="/checkout/user/" method="GET" enctype="application/x-www-form-urlencoded">
<form class="dcf-form dcf-w-25% dcf-mb-6" id="find-nuid" action="/checkout/user/" method="GET" enctype="application/x-www-form-urlencoded">
<label for="nuid-input">Scan NUID</label>
<div class="dcf-input-group">
<input id="nuid-input" autocomplete="off" name="nuid">
<button class="dcf-btn dcf-btn-primary" type="submit">Submit</button>
</div>
</form>

<form class="dcf-form dcf-w-25%" id="find-nuid" action="/checkout/warehouse/" method="GET" enctype="application/x-www-form-urlencoded">
<button class="dcf-btn dcf-btn-primary" type="submit">View Warehouse</button>
</form>

<script>
document.addEventListener('DOMContentLoaded', function() {
document.getElementById('nuid-input').focus();
Expand Down
181 changes: 181 additions & 0 deletions views/engineering_garage/checkout_all.erb
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
<h1 class="dcf-txt-h3" id="Checkout">Warehouse Inventory</h1>
<a href="/checkout" class="dcf-btn dcf-btn-secondary dcf-mb-4" type="button">Return to Scan Page</a>
<div class="dcf-grid dcf-col-gap-vw">
<fieldset class="dcf-form dcf-col-100% unl-b-darker-gray dcf-rounded dcf-b-1 dcf-b-solid dcf-pt-4 dcf-pb-4 dcf-p dcf-pr-7 dcf-pl-7">
<legend>Warehouse</legend>
<div class="dcf-d-flex dcf-flex-col dcf-gap-2">
<div class="dcf-tabs dcf-tabs-responsive">
<ul>
<li><a href="#user-projects-panel">User Projects</a></li>
<li><a href="#user-tools-panel">Tools</a></li>
</ul>
<div id="user-projects-panel">
<h2>User Projects</h2>
<% if defined?(projects) && projects.count > 0 %>
<form class="dcf-form dcf-pb-4" style = "width: 14em" id="filter-input" action="/checkout/warehouse/?#user-projects-panel" method="GET" enctype="application/x-www-form-urlencoded">
<label for="search_project_id">Filter Projects</label>
<div class="dcf-input-group">
<input id="search_project_id" name="search_project_id" value="<%= params[:search_project_id] if params[:search_project_id] %>">
<button class="dcf-btn dcf-btn-primary" type="submit">Submit</button>
</div>
</form>
<table class="dcf-table dcf-w-100% dcf-table-striped dcf-table-responsive dcf-mt-6 dcf-mb-6">
<thead>
<tr>
<th scope="col">Location</th>
<th scope="col">Project Name</th>
<th scope="col">Owner Username</th>
<th scope="col">Status</th>
<th scope="col">Last Accessed</th>
<th scope="col" class="dcf-txt-right">Project Actions</th>
</tr>
</thead>
<tbody>
<% projects.each do |project| %>
<tr>
<td><%= project[:bin_id] %></td>
<td><%= project[:title] %></td>
<td><%= project_owners[project.id].username %></td>
<% if project[:last_checked_out] != nil && project[:last_checked_in] != nil %>
<% if project[:last_checked_out] >= project[:last_checked_in] %>
<td><%= "Checked Out" %></td>
<td><%= project[:last_checked_out].in_time_zone.strftime("%b %-d, %Y %-I:%M%P") %></td>
<% else %>
<td><%= "Checked In" %></td>
<td><%= project[:last_checked_in].in_time_zone.strftime("%b %-d, %Y %-I:%M%P") %></td>
<% end %>
<% else %>
<% if project[:last_checked_out] != nil %>
<td><%= "Checked Out" %></td>
<td><%= project[:last_checked_out].in_time_zone.strftime("%b %-d, %Y %-I:%M%P") %></td>
<% end %>
<% if project[:last_checked_in] != nil %>
<td><%= "Checked In" %></td>
<td><%= project[:last_checked_in].in_time_zone.strftime("%b %-d, %Y %-I:%M%P") %></td>
<% end %>
<% end %>
<td class="dcf-d-flex dcf-flex-row-rev dcf-flex-shrink-1 dcf-gap-2">
<button class="dcf-btn-toggle-modal dcf-btn dcf-btn-secondary" data-toggles-modal="delete-<%= project[:id] %>" type="button">Delete</button>
<div class="dcf-modal" id="delete-<%= project[:id] %>" hidden>
<div class="dcf-modal-wrapper">
<div class="dcf-modal-header">
<h3>Delete Project</h3>
<button class="dcf-btn-close-modal">Close</button>
</div>
<div class="dcf-modal-content">
<form class="dcf-form dcf-grid" action="/checkout/project_delete/?bin_id=<%= project[:bin_id] %>" method="POST" enctype="application/x-www-form-urlencoded">
<div class="dcf-col-50%-start">
<p id="project_name"><strong>Name:</strong> <%= project[:title] %></p>
<p id="bin_id"><strong>Location:</strong> <%= project[:bin_id] %></p>
<p><strong>Description:</strong> <%= project[:description] %></p>
<div class="dcf-form-controls-inline dcf-gap-2">
<button class="dcf-btn dcf-btn-secondary" type="submit">Delete</button>
</div>
</div>
</form>
</div>
</div>
</div>
<a href="/checkout/project/edit/?project_id=<%= project[:id] %>&returning_to_warehouse=true" class="dcf-btn dcf-btn-primary">Edit</a>
</td>
</tr>
<% end %>
</tbody>
</table>
<% elsif !params[:search_project_id].nil? %>
<form class="dcf-form dcf-pb-4" style = "width: 14em" id="filter-input" action="/checkout/warehouse/?#user-projects-panel" method="GET" enctype="application/x-www-form-urlencoded">
<label for="search_project_id">Filter Projects</label>
<div class="dcf-input-group">
<input id="search_project_id" name="search_project_id" value="<%= params[:search_project_id] if params[:search_project_id] %>">
<button class="dcf-btn dcf-btn-primary" type="submit">Submit</button>
</div>
</form>
<p>No projects meet that filter</p>
<% else %>
<p>No projects found.</p>
<% end %>
</div>
<div id="user-tools-panel">
<div class="dcf-w-100% dcf-gap-4">
<div>
<h2>Checked Out Tools</h2>
<% if checked_out.count > 0 %>
<form class="dcf-form dcf-pb-4" style = "width: 14em" id="tool-input" action="/checkout/warehouse/?#user-tools-panel" method="GET" enctype="application/x-www-form-urlencoded">
<label for="search_tool_id">Scan Tool</label>
<div class="dcf-input-group">
<input id="search_tool_id" name="search_tool_id" value="<%= params[:search_tool_id] if params[:search_tool_id] %>">
<button class="dcf-btn dcf-btn-primary" type="submit">Submit</button>
</div>
</form>
<table class="dcf-table dcf-w-100% dcf-table-striped dcf-table-responsive dcf-mt-6 dcf-mb-6">
<thead>
<tr>
<th scope="col">Tool Name</th>
<th scope="col">Tool User</th>
<th scope="col">Checked Out</th>
<th scope="col">Tool Actions</th>
</tr>
</thead>
<tbody>
<% checked_out.each do |tool| %>
<tr>
<td><%= tool[:tool_name] %></td>
<td><%= current_tool_users[tool.id].username %></td>
<td><%= tool[:last_checked_out].in_time_zone.strftime("%b %-d, %Y %-I:%M%P") %></td>
<td class="table-actions">
<button class="dcf-btn-toggle-modal dcf-btn dcf-btn-primary" data-toggles-modal="checkin-tool-<%= tool[:id] %>" type="button">Check In</button>
<div class="dcf-modal" id="checkin-tool-<%= tool[:id] %>" hidden>
<div class="dcf-modal-wrapper">
<div class="dcf-modal-header">
<h3>Check In Tool</h3>
<button class="dcf-btn-close-modal">Close</button>
</div>
<div class="dcf-modal-content">
<form class="dcf-form dcf-grid" action="/checkout/tool_checkin/?tool_id=<%= tool[:id] %>" method="POST" enctype="application/x-www-form-urlencoded">
<div class="dcf-col-100% dcf-col-50%@md">
<p id="tool_name_<%= tool[:id]%>"><strong>Name:</strong> <%= tool[:tool_name] %></p>
<p id="serial_number_<%= tool[:id]%>"><strong>Serial Number:</strong> <%= tool[:serial_number] %></p>
<p id="model_number_<%= tool[:id]%>"><strong>Model Number:</strong> <%= tool[:model_number] %></p>
<div class="dcf-form-controls-inline dcf-gap-2">
<button class="dcf-btn dcf-btn-primary" type="submit">Check In</button>
</div>
</div>
<div class="dcf-col-100% dcf-col-50%@md dcf-overflow-y-auto" style = "max-height: 30rem;">
<fieldset>
<legend><strong>Description:</strong></legend>
<p><%= tool[:description] %></p>
</fieldset>
</div>
</form>
</div>
</div>
</div>
</td>
</tr>
<% end %>
</tbody>
</table>
<% elsif !params[:search_tool_id].nil? %>
<form class="dcf-form dcf-pb-4" style = "width: 14em" id="tool-input" action="/checkout/warehouse/?#user-tools-panel" method="GET" enctype="application/x-www-form-urlencoded">
<label for="search_tool_id">Scan Tool</label>
<div class="dcf-input-group">
<input id="search_tool_id" name="search_tool_id" value="<%= params[:search_tool_id] if params[:search_tool_id] %>">
<button class="dcf-btn dcf-btn-primary" type="submit">Submit</button>
</div>
</form>
<p>No tools meet that filter</p>
<% else %>
<p>Currently no tools checked out.</p>
<% end %>
</div>
</div>
</div>
</div>
<script>
window.addEventListener('inlineJSReady', function() {
WDN.initializePlugin('tabs');
}, false);
</script>
</div>
</fieldset>
</div>
Loading