View | Details | Raw Unified | Return to bug 22440
Collapse All | Expand All

(-)a/Koha/Illbackend.pm (+124 lines)
Line 0 Link Here
1
package Koha::Illbackend;
2
3
# Copyright PTFS Europe 2023
4
#
5
# This file is part of Koha.
6
#
7
# Koha is free software; you can redistribute it and/or modify it
8
# under the terms of the GNU General Public License as published by
9
# the Free Software Foundation; either version 3 of the License, or
10
# (at your option) any later version.
11
#
12
# Koha is distributed in the hope that it will be useful, but
13
# WITHOUT ANY WARRANTY; without even the implied warranty of
14
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15
# GNU General Public License for more details.
16
#
17
# You should have received a copy of the GNU General Public License
18
# along with Koha; if not, see <http://www.gnu.org/licenses>.
19
20
use Modern::Perl;
21
22
use base qw(Koha::Object);
23
24
=head1 NAME
25
26
Koha::Illbackend - Koha Illbackend Object class
27
28
=head2 Class methods
29
30
=head3 new
31
32
New illbackend
33
34
=cut
35
36
sub new {
37
    my $class = shift;
38
    my $self = {};
39
    return bless $self, $class;
40
}
41
42
=head3 existing_statuses
43
44
Return a list of existing ILL statuses
45
46
=cut
47
48
sub existing_statuses {
49
    my ( $self, $backend_id ) = @_;
50
51
    #FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status).
52
    # Even doing it with distinct status, we need the ILL request object, so that strings_map works and
53
    # the ILL request returns the correct status and info respective to its backend.
54
    my $ill_requests = Koha::Illrequests->search(
55
            {backend => $backend_id},
56
            # {
57
            #     columns => [ qw/status/ ],
58
            #     group_by => [ qw/status/ ],
59
            # }
60
        );
61
62
    my @data;
63
    while (my $request = $ill_requests->next) {
64
        my $status_data = $request->strings_map;
65
66
        foreach my $status_class ( qw(status_alias status) ){
67
            if ($status_data->{$status_class}){
68
                push @data, {
69
                    $status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) :
70
                        $status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (),
71
                    $status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (),
72
                }
73
            }
74
        }
75
    }
76
77
    # Remove duplicate statuses
78
    my %seen;
79
    @data =  grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data;
80
81
    return \@data;
82
}
83
84
=head3 embed
85
86
    Embed info in backend for API response
87
88
=cut
89
90
sub embed {
91
    my ( $self, $backend_id, $embed_header ) = @_;
92
    $embed_header ||= q{};
93
94
    my $return_embed;
95
96
    foreach my $embed_req ( split /\s*,\s*/, $embed_header ) {
97
        if ( $embed_req eq 'statuses+strings' ) {
98
            $return_embed->{statuses} = $self->existing_statuses( $backend_id );
99
        }
100
    }
101
    return $return_embed;
102
}
103
104
=head2 Internal methods
105
106
=head3 _type
107
108
    my $type = Koha::Illbackend->_type;
109
110
Return this object's type
111
112
=cut
113
114
sub _type {
115
    return 'Illbackend';
116
}
117
118
=head1 AUTHOR
119
120
Pedro Amorim <pedro.amorim@ptfs-europe.com>
121
122
=cut
123
124
1;
(-)a/Koha/REST/V1/Illbackends.pm (-57 / +30 lines)
Lines 21-26 use Mojo::Base 'Mojolicious::Controller'; Link Here
21
21
22
use Koha::Illrequest::Config;
22
use Koha::Illrequest::Config;
23
use Koha::Illrequests;
23
use Koha::Illrequests;
24
use Koha::Illbackend;
24
25
25
=head1 NAME
26
=head1 NAME
26
27
Lines 37-123 Return a list of available ILL backends and its capabilities Link Here
37
sub list {
38
sub list {
38
    my $c = shift->openapi->valid_input;
39
    my $c = shift->openapi->valid_input;
39
40
40
    my $config = Koha::Illrequest::Config->new;
41
    my $config   = Koha::Illrequest::Config->new;
41
    my $backends = $config->available_backends;
42
    my $backends = $config->available_backends;
42
43
43
    my @data;
44
    my @data;
44
    foreach my $b ( @$backends ) {
45
    foreach my $b (@$backends) {
45
        my $backend = Koha::Illrequest->new->load_backend( $b );
46
        my $backend = Koha::Illrequest->new->load_backend($b);
46
        push @data, {
47
        push @data,
48
          {
47
            ill_backend_id => $b,
49
            ill_backend_id => $b,
48
            capabilities => $backend->capabilities,
50
            capabilities   => $backend->capabilities,
49
        };
51
          };
50
    }
52
    }
51
    return $c->render( status => 200, openapi => \@data );
53
    return $c->render( status => 200, openapi => \@data );
52
}
54
}
53
55
54
=head3 list_statuses
56
=head3 get
55
57
56
Return a list of existing ILL statuses
58
Get one backend
57
59
58
=cut
60
=cut
59
61
60
sub list_statuses {
62
sub get {
61
    my $c = shift->openapi->valid_input;
63
    my $c = shift->openapi->valid_input;
62
64
63
    my $backend_id = $c->validation->param('ill_backend_id');
65
    my $backend_id = $c->validation->param('ill_backend_id');
64
66
65
    #FIXME: Currently fetching all requests, it'd be great if we could fetch distinct(status).
67
    return try {
66
    # Even doing it with distinct status, we need the ILL request object, so that strings_map works and
67
    # the ILL request returns the correct status and info respective to its backend.
68
    my $ill_requests = Koha::Illrequests->search(
69
            {backend => $backend_id},
70
            # {
71
            #     columns => [ qw/status/ ],
72
            #     group_by => [ qw/status/ ],
73
            # }
74
        );
75
76
    my @data;
77
    while (my $request = $ill_requests->next) {
78
        my $status_data = $request->strings_map;
79
80
        foreach my $status_class ( qw(status_alias status) ){
81
            if ($status_data->{$status_class}){
82
                push @data, {
83
                    $status_data->{$status_class}->{str} ? (str => $status_data->{$status_class}->{str}) :
84
                        $status_data->{$status_class}->{code} ? (str => $status_data->{$status_class}->{code}) : (),
85
                    $status_data->{$status_class}->{code} ? (code => $status_data->{$status_class}->{code}) : (),
86
                }
87
            }
88
        }
89
    }
90
91
    # Remove duplicate statuses
92
    my %seen;
93
    @data =  grep { my $e = $_; my $key = join '___', map { $e->{$_}; } sort keys %$_;!$seen{$key}++ } @data;
94
95
    return $c->render( status => 200, openapi => \@data );
96
}
97
68
98
=head3 get
69
        #FIXME: Should we move load_backend into Koha::Illbackend...
70
        #       or maybe make Koha::Ill::Backend a base class for all
71
        #       backends?
72
        my $backend = Koha::Illrequest->new->load_backend($backend_id);
99
73
100
Get one backend
74
        my $backend_module = Koha::Illbackend->new;
101
75
102
=cut
76
        my $embed =
77
          $backend_module->embed( $backend_id,
78
            $c->req->headers->header('x-koha-embed') );
103
79
104
sub get {
80
        #TODO: We need a to_api method in Koha::Illbackend
105
    my $c = shift->openapi->valid_input;
81
        my $return = {
106
82
            ill_backend_id => $backend_id,
107
    my $backend_id = $c->validation->param('ill_backend_id');
83
            capabilities   => $backend->capabilities,
84
        };
108
85
109
    return try {
110
        my $backend = Koha::Illrequest->new->load_backend( $backend_id );
111
        return $c->render(
86
        return $c->render(
112
            status => 200,
87
            status  => 200,
113
            openapi => {
88
            openapi => $embed ? { %$return, %$embed } : $return,
114
                ill_backend_id => $backend_id,
115
                capabilities => $backend->capabilities
116
            }
117
        );
89
        );
118
    } catch {
90
    }
91
    catch {
119
        return $c->render(
92
        return $c->render(
120
            status => 404,
93
            status  => 404,
121
            openapi => { error => "ILL backend does not exist" }
94
            openapi => { error => "ILL backend does not exist" }
122
        );
95
        );
123
    };
96
    };
(-)a/api/v1/swagger/definitions/ill_backend.yaml (+5 lines)
Lines 7-10 properties: Link Here
7
  capabilities:
7
  capabilities:
8
    type: object
8
    type: object
9
    description: List of capabilities
9
    description: List of capabilities
10
  statuses:
11
    type: array
12
    description: existing statuses
13
    items:
14
      $ref: ill_status.yaml
10
additionalProperties: false
15
additionalProperties: false
(-)a/api/v1/swagger/definitions/ill_statuses.yaml (-5 lines)
Lines 1-5 Link Here
1
---
2
type: array
3
items:
4
  $ref: "ill_status.yaml"
5
additionalProperties: false
(-)a/api/v1/swagger/paths/ill_backends.yaml (-46 / +10 lines)
Lines 53-58 Link Here
53
        description: ILL backend id/name
53
        description: ILL backend id/name
54
        required: true
54
        required: true
55
        type: string
55
        type: string
56
      - name: x-koha-embed
57
        in: header
58
        required: false
59
        description: Embed list sent as a request header
60
        type: array
61
        items:
62
          type: string
63
          enum:
64
            - statuses+strings
65
        collectionFormat: csv
56
    produces:
66
    produces:
57
      - application/json
67
      - application/json
58
    responses:
68
    responses:
Lines 83-134 Link Here
83
        description: Under maintenance
93
        description: Under maintenance
84
        schema:
94
        schema:
85
          $ref: "../swagger.yaml#/definitions/error"
95
          $ref: "../swagger.yaml#/definitions/error"
86
    x-koha-authorization:
87
      permissions:
88
        ill: "1"
89
"/ill/backends/{ill_backend_id}/statuses":
90
  get:
91
    x-mojo-to: Illbackends#list_statuses
92
    operationId: getIllbackendsStatuses
93
    tags:
94
      - ill_backends
95
    summary: Get existing ILL statuses
96
    parameters:
97
      - name: ill_backend_id
98
        in: path
99
        description: ILL backend id/name
100
        required: true
101
        type: string
102
    produces:
103
      - application/json
104
    responses:
105
      "200":
106
        description: A list of existing ILL statuses
107
        schema:
108
          $ref: "../swagger.yaml#/definitions/ill_statuses"
109
      "401":
110
        description: Authentication required
111
        schema:
112
          $ref: "../swagger.yaml#/definitions/error"
113
      "403":
114
        description: Access forbidden
115
        schema:
116
          $ref: "../swagger.yaml#/definitions/error"
117
      "404":
118
        description: ILL backends not found
119
        schema:
120
          $ref: "../swagger.yaml#/definitions/error"
121
      "500":
122
        description: |
123
          Internal server error. Possible `error_code` attribute values:
124
125
          * `internal_server_error`
126
        schema:
127
          $ref: "../swagger.yaml#/definitions/error"
128
      "503":
129
        description: Under maintenance
130
        schema:
131
          $ref: "../swagger.yaml#/definitions/error"
132
    x-koha-authorization:
96
    x-koha-authorization:
133
      permissions:
97
      permissions:
134
        ill: "1"
98
        ill: "1"
(-)a/api/v1/swagger/swagger.yaml (-4 lines)
Lines 54-61 definitions: Link Here
54
    $ref: ./definitions/ill_backends.yaml
54
    $ref: ./definitions/ill_backends.yaml
55
  ill_status:
55
  ill_status:
56
    $ref: ./definitions/ill_status.yaml
56
    $ref: ./definitions/ill_status.yaml
57
  ill_statuses:
58
    $ref: ./definitions/ill_statuses.yaml
59
  ill_request:
57
  ill_request:
60
    $ref: ./definitions/ill_request.yaml
58
    $ref: ./definitions/ill_request.yaml
61
  import_batch_profile:
59
  import_batch_profile:
Lines 249-256 paths: Link Here
249
    $ref: ./paths/ill_backends.yaml#/~1ill~1backends
247
    $ref: ./paths/ill_backends.yaml#/~1ill~1backends
250
  "/ill/backends/{ill_backend_id}":
248
  "/ill/backends/{ill_backend_id}":
251
    $ref: "./paths/ill_backends.yaml#/~1ill~1backends~1{ill_backend_id}"
249
    $ref: "./paths/ill_backends.yaml#/~1ill~1backends~1{ill_backend_id}"
252
  "/ill/backends/{ill_backend_id}/statuses":
253
    $ref: "./paths/ill_backends.yaml#/~1ill~1backends~1{ill_backend_id}~1statuses"
254
  /ill/requests:
250
  /ill/requests:
255
    $ref: ./paths/ill_requests.yaml#/~1ill~1requests
251
    $ref: ./paths/ill_requests.yaml#/~1ill~1requests
256
  "/import_batches/{import_batch_id}/records/{import_record_id}/matches/chosen":
252
  "/import_batches/{import_batch_id}/records/{import_record_id}/matches/chosen":
(-)a/koha-tmpl/intranet-tmpl/prog/js/ill-list-table.js (-2 / +6 lines)
Lines 430-437 $(document).ready(function() { Link Here
430
    function populateStatusFilter(backend) {
430
    function populateStatusFilter(backend) {
431
        $.ajax({
431
        $.ajax({
432
            type: "GET",
432
            type: "GET",
433
            url: "/api/v1/ill/backends/"+backend+"/statuses",
433
            url: "/api/v1/ill/backends/"+backend,
434
            success: function(statuses){
434
            headers: {
435
                'x-koha-embed': 'statuses+strings'
436
            },
437
            success: function(response){
438
                let statuses = response.statuses
435
                $('#illfilter_status').append(
439
                $('#illfilter_status').append(
436
                    '<option value="">'+ill_all_statuses+'</option>'
440
                    '<option value="">'+ill_all_statuses+'</option>'
437
                );
441
                );
(-)a/t/db_dependent/api/v1/ill_backends.t (-5 / +4 lines)
Lines 193-206 subtest 'list() tests' => sub { Link Here
193
    );
193
    );
194
194
195
    #Check for backend existing statuses
195
    #Check for backend existing statuses
196
    $t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock/statuses")
196
    $t->get_ok("//$userid:$password@/api/v1/ill/backends/Mock" => {'x-koha-embed' => 'statuses+strings'} )
197
      ->status_is(200)
197
      ->status_is(200)
198
      ->json_is( [ $backend_status, $core_status, $alias_status ] );
198
      ->json_has( '/statuses', [ $backend_status, $core_status, $alias_status ] );
199
199
200
    #Check for backend existing statuses of a backend that doesn't exist
200
    #Check for backend existing statuses of a backend that doesn't exist
201
    $t->get_ok("//$userid:$password@/api/v1/ill/backends/GhostBackend/statuses")
201
    $t->get_ok("//$userid:$password@/api/v1/ill/backends/GhostBackend"  => {'x-koha-embed' => 'statuses+strings'} )
202
      ->status_is(200)
202
      ->status_is(200)
203
      ->json_is( [] );
203
      ->json_hasnt( 'statuses' );
204
204
205
    # Unauthorized attempt to list
205
    # Unauthorized attempt to list
206
    $t->get_ok("//$unauth_userid:$password@/api/v1/ill/backends")
206
    $t->get_ok("//$unauth_userid:$password@/api/v1/ill/backends")
207
- 

Return to bug 22440