From 018ae521a861fd5475edb223bea8fac679988db1 Mon Sep 17 00:00:00 2001 From: Jim Brandt Date: Mon, 29 Jan 2024 13:44:37 -0500 Subject: [PATCH] Add TotalTimeWorked to REST2 ticket response data There is some overhead to calculate this value, especially if there are many linked children tickets, so include it only if DisplayTotalTimeWorked is enabled in the RT configuration indicating they are using this field. --- lib/RT/REST2/Resource/Ticket.pm | 13 ++++++++++++- t/rest2/tickets.t | 10 ++++++++-- 2 files changed, 20 insertions(+), 3 deletions(-) diff --git a/lib/RT/REST2/Resource/Ticket.pm b/lib/RT/REST2/Resource/Ticket.pm index 64b4d67647d..f879e1136f5 100644 --- a/lib/RT/REST2/Resource/Ticket.pm +++ b/lib/RT/REST2/Resource/Ticket.pm @@ -55,7 +55,8 @@ use namespace::autoclean; extends 'RT::REST2::Resource::Record'; with ( - 'RT::REST2::Resource::Record::Readable', + 'RT::REST2::Resource::Record::Readable' + => { -alias => { serialize => '_default_serialize' } }, 'RT::REST2::Resource::Record::Hypermedia' => { -alias => { hypermedia_links => '_default_hypermedia_links' } }, 'RT::REST2::Resource::Record::Deletable', @@ -239,6 +240,16 @@ sub validate_input { return (1, "Validation passed"); } +sub serialize { + my $self = shift; + my $data = $self->_default_serialize(@_); + + if ( RT->Config->Get('DisplayTotalTimeWorked') ) { + $data->{'TotalTimeWorked'} = $self->record->TotalTimeWorked(); + } + + return $data; +} __PACKAGE__->meta->make_immutable; diff --git a/t/rest2/tickets.t b/t/rest2/tickets.t index fd563c82e73..b2988b1db30 100644 --- a/t/rest2/tickets.t +++ b/t/rest2/tickets.t @@ -8,6 +8,9 @@ use Encode qw(decode encode); # Test using integer priorities RT->Config->Set(EnablePriorityAsString => 0); + +# Include TotalTimeWorked in response data +RT->Config->Set(DisplayTotalTimeWorked => 1); my $mech = RT::Test::REST2->mech; my $auth = RT::Test::REST2->authorization_header; @@ -42,6 +45,7 @@ my ($ticket_url, $ticket_id); Subject => 'Ticket creation using REST', Queue => 'General', Content => 'Testing ticket creation using REST API.', + TimeWorked => 5, }; # Rights Test - No CreateTicket @@ -85,9 +89,11 @@ my ($ticket_url, $ticket_id); is($content->{Type}, 'ticket'); is($content->{Status}, 'new'); is($content->{Subject}, 'Ticket creation using REST'); + is($content->{TimeWorked}, 5); + is($content->{TotalTimeWorked}, 5); - ok(exists $content->{$_}) for qw(AdminCc TimeEstimated Started Cc - LastUpdated TimeWorked Resolved + ok(exists $content->{$_}, "Found $_") for qw(AdminCc TimeEstimated Started Cc + LastUpdated Resolved Created Due Priority EffectiveId); my $links = $content->{_hyperlinks};