From 17c8c59881114047cc406dd938dfb546ea7626c9 Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Fri, 17 Jun 2022 11:13:19 +0200 Subject: [PATCH] Bug 30982: Use the REST API for background job list view Signed-off-by: Martin Renvoize Signed-off-by: Marcel de Rooy Signed-off-by: Tomas Cohen Arazi --- Koha/BackgroundJob.pm | 14 ++ Koha/REST/V1/BackgroundJobs.pm | 58 ++++++ admin/background_jobs.pl | 33 ---- .../prog/en/modules/admin/background_jobs.tt | 174 ++++++++++++++++++ 4 files changed, 246 insertions(+), 33 deletions(-) create mode 100644 Koha/REST/V1/BackgroundJobs.pm diff --git a/Koha/BackgroundJob.pm b/Koha/BackgroundJob.pm index e1274ba144..c6058c999a 100644 --- a/Koha/BackgroundJob.pm +++ b/Koha/BackgroundJob.pm @@ -472,6 +472,20 @@ sub plugin_types_to_classes { return $self->{_plugin_mapping}; } +=head3 to_api_mapping + +This method returns the mapping for representing a Koha::BackgroundJob object +on the API. + +=cut + +sub to_api_mapping { + return { + id => 'background_job_id', + borrowernumber => 'patron_id', + }; +} + =head3 _type =cut diff --git a/Koha/REST/V1/BackgroundJobs.pm b/Koha/REST/V1/BackgroundJobs.pm new file mode 100644 index 0000000000..e7deaa353c --- /dev/null +++ b/Koha/REST/V1/BackgroundJobs.pm @@ -0,0 +1,58 @@ +package Koha::REST::V1::BackgroundJobs; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Mojo::Base 'Mojolicious::Controller'; + +use Koha::BackgroundJobs; + +use Try::Tiny; + +=head1 API + +=head2 Methods + +=head3 list + +=cut + +sub list { + my $c = shift->openapi->valid_input or return; + + return try { + my $patron = $c->stash('koha.user'); + + my $can_manage_background_jobs = + $patron->has_permission( { parameters => 'manage_background_jobs' } ); + + my $background_jobs_set = + $can_manage_background_jobs + ? Koha::BackgroundJobs->new + : Koha::BackgroundJobs->search( + { borrowernumber => $patron->borrowernumber } ); + + my $background_jobs = $c->objects->search( $background_jobs_set ); + return $c->render( status => 200, openapi => $background_jobs ); + } + catch { + $c->unhandled_exception($_); + }; + +} + +1; diff --git a/admin/background_jobs.pl b/admin/background_jobs.pl index 761c09d069..b85281ce72 100755 --- a/admin/background_jobs.pl +++ b/admin/background_jobs.pl @@ -77,39 +77,6 @@ if ( $op eq 'cancel' ) { $op = 'list'; } - -if ( $op eq 'list' ) { - my $queued_jobs = - $can_manage_background_jobs - ? Koha::BackgroundJobs->search( { ended_on => undef }, - { order_by => { -desc => 'enqueued_on' } } ) - : Koha::BackgroundJobs->search( - { borrowernumber => $logged_in_user->borrowernumber, ended_on => undef }, - { order_by => { -desc => 'enqueued_on' } } - ); - $template->param( queued => $queued_jobs ); - - my $ended_since = dt_from_string->subtract( minutes => '60' ); - my $dtf = Koha::Database->new->schema->storage->datetime_parser; - - my $complete_jobs = - $can_manage_background_jobs - ? Koha::BackgroundJobs->search( - { - ended_on => { '>=' => $dtf->format_datetime($ended_since) } - }, - { order_by => { -desc => 'enqueued_on' } } - ) - : Koha::BackgroundJobs->search( - { - borrowernumber => $logged_in_user->borrowernumber, - ended_on => { '>=' => $dtf->format_datetime($ended_since) } - }, - { order_by => { -desc => 'enqueued_on' } } - ); - $template->param( complete => $complete_jobs ); -} - $template->param( messages => \@messages, op => $op, diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt index 53844f7b6f..8dbc88e838 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/admin/background_jobs.tt @@ -262,6 +262,23 @@ + + + + + + + + + + + + + + + + +
Job IDStatusProgressTypeQueuedStartedEndedActions
[% END %] @@ -276,6 +293,7 @@ [% MACRO jsinclude BLOCK %] [% Asset.js("js/admin-menu.js") | $raw %] + [% INCLUDE 'js-date-format.inc' %] [% INCLUDE 'datatables.inc' %] [% IF op == 'view' %] [% PROCESS 'js' %] -- 2.39.5