Skip to content

Commit

Permalink
Add the retired status to tickets (#1753)
Browse files Browse the repository at this point in the history
# What it does

Add the retired status to tickets, always change an item's status to be
retired when a retired ticket is created or a ticket is updated to
retired.

# Why it is important

#1745 

# UI Change Screenshot

Item's ticket page with no tickets:
![Screenshot 2024-11-13 at 2 51
57 PM](https://github.com/user-attachments/assets/f2019d17-abd0-42de-97a5-7715bf96e70d)

Item's ticket page after creating a "retired" ticket:
![Screenshot 2024-11-13 at 2 52
26 PM](https://github.com/user-attachments/assets/3ac0f2d2-3fd3-4652-ac7a-60736432cf60)

Item's ticket page with an "assess" ticket:
![Screenshot 2024-11-13 at 2 53
16 PM](https://github.com/user-attachments/assets/d592b21d-8d1d-4a74-80af-83ac407f2239)

Item's ticket page after updating the "assess" ticket to "retired":
![Screenshot 2024-11-13 at 2 53
33 PM](https://github.com/user-attachments/assets/df38241b-8b11-4dbe-a1db-5b4220c8c8de)

# Implementation notes

I suppose it's possible for the item update to fail (although I don't
see how), but I used `update!` so it should surface if it does.
  • Loading branch information
crismali authored Nov 15, 2024
1 parent 4483522 commit 6b407b5
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 12 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,6 @@ class TicketUpdatesController < BaseController
before_action :set_ticket
before_action :set_ticket_update, only: [:edit, :update, :destroy]

# def index
# @maintenance_reports = @item.maintenance_reports.includes(:audit)

# @events = @maintenance_reports.to_a.concat(@item.audits.includes(:maintenance_report)).sort_by(&:created_at)
# end

def new
@ticket_update_form = TicketUpdateForm.new(@ticket)
end
Expand Down
2 changes: 2 additions & 0 deletions app/controllers/admin/items/tickets_controller.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def create
@ticket = @item.tickets.new(ticket_params)

if @ticket.save
@ticket.item.update!(status: Item.statuses["retired"]) if @ticket.retired?
redirect_to admin_item_ticket_url(@item, @ticket), success: "Ticket was successfully created.", status: :see_other
else
render :new, status: :unprocessable_entity
Expand All @@ -32,6 +33,7 @@ def create

def update
if @ticket.update(ticket_params)
@ticket.item.update!(status: Item.statuses["retired"]) if @ticket.retired?
redirect_to admin_item_ticket_url(@item, @ticket), success: "Ticket was successfully updated.", status: :see_other
else
render :edit, status: :unprocessable_entity
Expand Down
9 changes: 6 additions & 3 deletions app/models/ticket.rb
Original file line number Diff line number Diff line change
Expand Up @@ -3,21 +3,24 @@ class Ticket < ApplicationRecord
"assess" => "Assess",
"repairing" => "Repair in Progress",
"parts" => "Parts on Order",
"resolved" => "Resolved"
"resolved" => "Resolved",
"retired" => "Retired"
}

STATUS_DESCRIPTIONS = {
"assess" => "newly created; needs examination by maintenance team",
"parts" => nil,
"repairing" => nil,
"resolved" => "the problem has been fixed"
"resolved" => "the problem has been fixed",
"retired" => "removed from inventory"
}

enum :status, {
assess: "assess",
parts: "parts",
repairing: "repairing",
resolved: "resolved"
resolved: "resolved",
retired: "retired"
}

belongs_to :item
Expand Down
9 changes: 9 additions & 0 deletions db/migrate/20241113202700_add_retired_to_ticket_status.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
class AddRetiredToTicketStatus < ActiveRecord::Migration[7.2]
def up
add_enum_value :ticket_status, "retired"
end

def down
raise ActiveRecord::IrreversibleMigration
end
end
3 changes: 2 additions & 1 deletion db/schema.rb
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.

ActiveRecord::Schema[7.2].define(version: 2024_11_05_021006) do
ActiveRecord::Schema[7.2].define(version: 2024_11_13_202700) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"

Expand Down Expand Up @@ -89,6 +89,7 @@
"parts",
"repairing",
"resolved",
"retired",
], force: :cascade

create_enum :user_role, [
Expand Down
47 changes: 45 additions & 2 deletions test/controllers/admin/items/tickets_controller_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,35 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest
end

test "should create ticket" do
status = "assess"
title = "A ticket title"
body = "A ticket body"

assert_difference("Ticket.count") do
post admin_item_tickets_url(@item), params: {ticket: {status: "assess", title: "A ticket title", body: "A ticket body"}}
post admin_item_tickets_url(@item), params: {ticket: {status:, title:, body:}}
end

assert_redirected_to admin_item_ticket_url(@item, Ticket.last)

ticket = Ticket.first!

assert_equal status, ticket.status
assert_equal title, ticket.title
assert_equal body, ticket.body.to_plain_text
end

test "creating a retired ticket updates the item's status to retired too" do
retired = Item.statuses["retired"]

refute_equal retired, @item.status

assert_difference("Ticket.count") do
post admin_item_tickets_url(@item), params: {ticket: {status: retired, title: "foo", body: ""}}
end

ticket = Ticket.first!
assert_equal retired, ticket.status
assert_equal retired, @item.reload.status
end

test "should show ticket" do
Expand All @@ -54,10 +78,29 @@ class TicketsControllerTest < ActionDispatch::IntegrationTest

test "should update ticket" do
@ticket = create(:ticket, item: @item)
status = "parts"
body = "Waiting on parts"

patch admin_item_ticket_url(@item, @ticket), params: {ticket: {status: "parts", time_spent: "15", body: "Waiting on parts"}}
patch admin_item_ticket_url(@item, @ticket), params: {ticket: {status:, body:}}

assert_redirected_to admin_item_ticket_url(@item, @ticket)

@ticket.reload
assert_equal status, @ticket.status
assert_equal body, @ticket.body.to_plain_text
end

test "updating a ticket to retired makes the item retired" do
@ticket = create(:ticket, item: @item)
retired = Item.statuses["retired"]

refute_equal retired, @item.status
refute_equal retired, @ticket.status

patch admin_item_ticket_url(@item, @ticket), params: {ticket: {status: retired, body: ""}}

assert_equal retired, @ticket.reload.status
assert_equal retired, @item.reload.status
end

test "should destroy ticket" do
Expand Down

0 comments on commit 6b407b5

Please sign in to comment.