Bug 22440: Move backend statuses api endpoint

Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
(cherry picked from commit 50909d2266)
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
This commit is contained in:
Pedro Amorim 2023-04-27 16:08:56 +00:00 committed by Martin Renvoize
parent 7d122a4cf0
commit feef2edba8
Signed by: martin.renvoize
GPG key ID: 422B469130441A0F
8 changed files with 180 additions and 119 deletions

124
Koha/Illbackend.pm Normal file
View file

@ -0,0 +1,124 @@
package Koha::Illbackend;
# Copyright PTFS Europe 2023
#
# 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 <http://www.gnu.org/licenses>.
use Modern::Perl;
use base qw(Koha::Object);
=head1 NAME
Koha::Illbackend - Koha Illbackend Object class
=head2 Class methods
=head3 new
New illbackend
=cut
sub new {
my $class = shift;
my $self = {};
return bless $self, $class;
}
=head3 existing_statuses
Return a list of existing ILL statuses
=cut
sub existing_statuses {
my ( $self, $backend_id ) = @_;
#FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status).
# Even doing it with distinct status, we need the ILL request object, so that strings_map works and
# the ILL request returns the correct status and info respective to its backend.
my $ill_requests = Koha::Illrequests->search(
{backend => $backend_id},
# {
# columns => [ qw/status/ ],
# group_by => [ qw/status/ ],
# }
);
my @data;
while (my $request = $ill_requests->next) {
my $status_data = $request->strings_map;
foreach my $status_class ( qw(status_alias status) ){
if ($status_data->{$status_class}){
push @data, {
$status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) :
$status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (),
$status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (),
}
}
}
}
# Remove duplicate statuses
my %seen;
@data = grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data;
return \@data;
}
=head3 embed
Embed info in backend for API response
=cut
sub embed {
my ( $self, $backend_id, $embed_header ) = @_;
$embed_header ||= q{};
my $return_embed;
foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
if ( $embed_req eq 'statuses+strings' ) {
$return_embed->{statuses} = $self->existing_statuses( $backend_id );
}
}
return $return_embed;
}
=head2 Internal methods
=head3 _type
my $type = Koha::Illbackend->_type;
Return this object's type
=cut
sub _type {
return 'Illbackend';
}
=head1 AUTHOR
Pedro Amorim <pedro.amorim@ptfs-europe.com>
=cut
1;

View file

@ -21,6 +21,7 @@ use Mojo::Base 'Mojolicious::Controller';
use Koha::Illrequest::Config;
use Koha::Illrequests;
use Koha::Illbackend;
=head1 NAME
@ -37,64 +38,21 @@ Return a list of available ILL backends and its capabilities
sub list {
my $c = shift->openapi->valid_input;
my $config = Koha::Illrequest::Config->new;
my $config = Koha::Illrequest::Config->new;
my $backends = $config->available_backends;
my @data;
foreach my $b ( @$backends ) {
my $backend = Koha::Illrequest->new->load_backend( $b );
push @data, {
foreach my $b (@$backends) {
my $backend = Koha::Illrequest->new->load_backend($b);
push @data,
{
ill_backend_id => $b,
capabilities => $backend->capabilities,
};
capabilities => $backend->capabilities,
};
}
return $c->render( status => 200, openapi => \@data );
}
=head3 list_statuses
Return a list of existing ILL statuses
=cut
sub list_statuses {
my $c = shift->openapi->valid_input;
my $backend_id = $c->validation->param('ill_backend_id');
#FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status).
# Even doing it with distinct status, we need the ILL request object, so that strings_map works and
# the ILL request returns the correct status and info respective to its backend.
my $ill_requests = Koha::Illrequests->search(
{backend => $backend_id},
# {
# columns => [ qw/status/ ],
# group_by => [ qw/status/ ],
# }
);
my @data;
while (my $request = $ill_requests->next) {
my $status_data = $request->strings_map;
foreach my $status_class ( qw(status_alias status) ){
if ($status_data->{$status_class}){
push @data, {
$status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) :
$status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (),
$status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (),
}
}
}
}
# Remove duplicate statuses
my %seen;
@data = grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data;
return $c->render( status => 200, openapi => \@data );
}
=head3 get
Get one backend
@ -107,17 +65,32 @@ sub get {
my $backend_id = $c->validation->param('ill_backend_id');
return try {
my $backend = Koha::Illrequest->new->load_backend( $backend_id );
#FIXME: Should we move load_backend into Koha::Illbackend...
# or maybe make Koha::Ill::Backend a base class for all
# backends?
my $backend = Koha::Illrequest->new->load_backend($backend_id);
my $backend_module = Koha::Illbackend->new;
my $embed =
$backend_module->embed( $backend_id,
$c->req->headers->header('x-koha-embed') );
#TODO: We need a to_api method in Koha::Illbackend
my $return = {
ill_backend_id => $backend_id,
capabilities => $backend->capabilities,
};
return $c->render(
status => 200,
openapi => {
ill_backend_id => $backend_id,
capabilities => $backend->capabilities
}
status => 200,
openapi => $embed ? { %$return, %$embed } : $return,
);
} catch {
}
catch {
return $c->render(
status => 404,
status => 404,
openapi => { error => "ILL backend does not exist" }
);
};

View file

@ -7,4 +7,9 @@ properties:
capabilities:
type: object
description: List of capabilities
statuses:
type: array
description: existing statuses
items:
$ref: ill_status.yaml
additionalProperties: false

View file

@ -1,5 +0,0 @@
---
type: array
items:
$ref: "ill_status.yaml"
additionalProperties: false

View file

@ -53,6 +53,16 @@
description: ILL backend id/name
required: true
type: string
- name: x-koha-embed
in: header
required: false
description: Embed list sent as a request header
type: array
items:
type: string
enum:
- statuses+strings
collectionFormat: csv
produces:
- application/json
responses:
@ -83,52 +93,6 @@
description: Under maintenance
schema:
$ref: "../swagger.yaml#/definitions/error"
x-koha-authorization:
permissions:
ill: "1"
"/ill/backends/{ill_backend_id}/statuses":
get:
x-mojo-to: Illbackends#list_statuses
operationId: getIllbackendsStatuses
tags:
- ill_backends
summary: Get existing ILL statuses
parameters:
- name: ill_backend_id
in: path
description: ILL backend id/name
required: true
type: string
produces:
- application/json
responses:
"200":
description: A list of existing ILL statuses
schema:
$ref: "../swagger.yaml#/definitions/ill_statuses"
"401":
description: Authentication required
schema:
$ref: "../swagger.yaml#/definitions/error"
"403":
description: Access forbidden
schema:
$ref: "../swagger.yaml#/definitions/error"
"404":
description: ILL backends not found
schema:
$ref: "../swagger.yaml#/definitions/error"
"500":
description: |
Internal server error. Possible `error_code` attribute values:
* `internal_server_error`
schema:
$ref: "../swagger.yaml#/definitions/error"
"503":
description: Under maintenance
schema:
$ref: "../swagger.yaml#/definitions/error"
x-koha-authorization:
permissions:
ill: "1"

View file

@ -54,8 +54,6 @@ definitions:
$ref: ./definitions/ill_backends.yaml
ill_status:
$ref: ./definitions/ill_status.yaml
ill_statuses:
$ref: ./definitions/ill_statuses.yaml
ill_request:
$ref: ./definitions/ill_request.yaml
import_batch_profile:
@ -243,8 +241,6 @@ paths:
$ref: ./paths/ill_backends.yaml#/~1ill~1backends
"/ill/backends/{ill_backend_id}":
$ref: "./paths/ill_backends.yaml#/~1ill~1backends~1{ill_backend_id}"
"/ill/backends/{ill_backend_id}/statuses":
$ref: "./paths/ill_backends.yaml#/~1ill~1backends~1{ill_backend_id}~1statuses"
/ill/requests:
$ref: ./paths/ill_requests.yaml#/~1ill~1requests
"/import_batches/{import_batch_id}/records/{import_record_id}/matches/chosen":

View file

@ -430,8 +430,12 @@ $(document).ready(function() {
function populateStatusFilter(backend) {
$.ajax({
type: "GET",
url: "/api/v1/ill/backends/"+backend+"/statuses",
success: function(statuses){
url: "/api/v1/ill/backends/"+backend,
headers: {
'x-koha-embed': 'statuses+strings'
},
success: function(response){
let statuses = response.statuses
$('#illfilter_status').append(
'<option value="">'+ill_all_statuses+'</option>'
);

View file

@ -193,14 +193,14 @@ subtest 'list() tests' => sub {
);
#Check for backend existing statuses
$t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock/statuses")
$t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock" => {'x-koha-embed' => 'statuses+strings'} )
->status_is(200)
->json_is( [ $backend_status, $core_status, $alias_status ] );
->json_has( '/statuses', [ $backend_status, $core_status, $alias_status ] );
#Check for backend existing statuses of a backend that doesn't exist
$t->get_ok("//$userid:$password@/api/v1/ill/backends/GhostBackend/statuses")
$t->get_ok("//$userid:$password@/api/v1/ill/backends/GhostBackend" => {'x-koha-embed' => 'statuses+strings'} )
->status_is(200)
->json_is( [] );
->json_hasnt( 'statuses' );
# Unauthorized attempt to list
$t->get_ok("//$unauth_userid:$password@/api/v1/ill/backends")