From 05283f26f0e23ff19060c1ab3219411d6a1d22df Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Fri, 20 Apr 2018 14:20:11 +0100 Subject: [PATCH] Bug 20600: API - Format dates according to syspref Dates supplied by the /api/v1/illrequests API route were not conforming to the preference specified by the dateformat syspref. This patch addresses that. It has been addressed as part of this bug since we are adding filtering of requests by some date fields and, therefore, needed dates in a predictable format. To test: 1) Apply the patch 2) Ensure you have at least one ILL request created 3) Make a request to the /api/v1/illrequests endpoint 4) Observe that dates supplied for "placed" & "updated" conform to your dateformat syspref. 5) Change your date format syspref, repeat steps 3 & 4 --- Koha/REST/V1/Illrequests.pm | 21 +++++++++++++++------ 1 file changed, 15 insertions(+), 6 deletions(-) diff --git a/Koha/REST/V1/Illrequests.pm b/Koha/REST/V1/Illrequests.pm index 6e3eace01d..34fece7ed3 100644 --- a/Koha/REST/V1/Illrequests.pm +++ b/Koha/REST/V1/Illrequests.pm @@ -21,6 +21,7 @@ use Mojo::Base 'Mojolicious::Controller'; use Koha::Illrequests; use Koha::Libraries; +use Koha::DateUtils qw( format_sqldatetime ); =head1 NAME @@ -40,6 +41,7 @@ sub list { my $args = $c->req->params->to_hash // {}; my $filter; my $output = []; + my @format_dates = ( 'placed', 'updated' ); # Create a hash where all keys are embedded values # Enables easy checking @@ -54,18 +56,25 @@ sub list { $filter->{$filter_param} = \@values; } - my $requests = Koha::Illrequests->search($filter); + my @req_list = Koha::Illrequests->search($filter)->as_list; if ( scalar (keys %embed) ) { # Need to embed stuff - my @results = map { $_->TO_JSON(\%embed) } $requests->as_list; - return $c->render( status => 200, openapi => \@results ); + @req_list = map { $_->TO_JSON(\%embed) } @req_list; } - else - { - return $c->render( status => 200, openapi => $requests ); + + # Format dates as required + foreach(@req_list) { + foreach my $field(@format_dates) { + if (defined $_->{$field}) { + $_->{$field} = format_sqldatetime($_->{$field}, undef, undef, 1); + } + } + } + + return $c->render( status => 200, openapi => \@req_list ); } 1; -- 2.11.0