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

(-)a/Koha/Illrequests.pm (-17 / +51 lines)
Lines 31-53 Koha::Illrequests - Koha Illrequests Object class Link Here
31
31
32
=head1 API
32
=head1 API
33
33
34
=head2 Class Methods
34
=head2 Class methods
35
36
=head3 _type
37
38
=cut
39
40
sub _type {
41
    return 'Illrequest';
42
}
43
44
=head3 object_class
45
46
=cut
47
48
sub object_class {
49
    return 'Koha::Illrequest';
50
}
51
35
52
##### To be implemented Facade
36
##### To be implemented Facade
53
37
Lines 72-77 sub new { Link Here
72
    return $self;
56
    return $self;
73
}
57
}
74
58
59
=head3 filter_by_visible
60
61
    my $visible_requests = $requests->filter_by_visible;
62
63
Returns a I<Koha::Illrequests> resultset, filtered by statuses that are not listed
64
as hidden in the I<ILLHiddenRequestStatuses> system preference.
65
66
=cut
67
68
sub filter_by_visible {
69
    my ($self) = @_;
70
71
    my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{};
72
    my $hidden_statuses = [ split '\|', $hidden_statuses_string ];
73
74
    if ( scalar @{$hidden_statuses} ) {
75
        return $self->search(
76
            {
77
                -and => {
78
                    status => { 'not in' => $hidden_statuses },
79
                    status_alias => [ -or =>
80
                        { 'not in' => $hidden_statuses },
81
                        { '=' => undef }
82
                    ]
83
                }
84
            }
85
        );
86
    }
87
88
    return $self;
89
}
90
75
=head3 search_incomplete
91
=head3 search_incomplete
76
92
77
    my $requests = $illRequests->search_incomplete;
93
    my $requests = $illRequests->search_incomplete;
Lines 90-95 sub search_incomplete { Link Here
90
    } );
106
    } );
91
}
107
}
92
108
109
=head2 Internal methods
110
111
=head3 _type
112
113
=cut
114
115
sub _type {
116
    return 'Illrequest';
117
}
118
119
=head3 object_class
120
121
=cut
122
123
sub object_class {
124
    return 'Koha::Illrequest';
125
}
126
93
=head1 AUTHOR
127
=head1 AUTHOR
94
128
95
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
129
Alex Sassmannshausen <alex.sassmannshausen@ptfs-europe.com>
(-)a/Koha/REST/V1/Illrequests.pm (-154 / +9 lines)
Lines 35-203 Koha::REST::V1::Illrequests Link Here
35
35
36
=head3 list
36
=head3 list
37
37
38
Return a list of ILL requests, after applying filters.
38
Controller function that handles listing Koha::Illrequest objects
39
39
40
=cut
40
=cut
41
41
42
sub list {
42
sub list {
43
    my $c = shift->openapi->valid_input or return;
43
    my $c = shift->openapi->valid_input or return;
44
44
45
    my $args = $c->req->params->to_hash // {};
45
    return try {
46
    my $output = [];
47
    my @format_dates = ( 'placed', 'updated', 'completed' );
48
46
49
    # Create a hash where all keys are embedded values
47
        my $reqs = $c->objects->search(Koha::Illrequests->new->filter_by_visible);
50
    # Enables easy checking
51
    my %embed;
52
    my $args_arr = (ref $args->{embed} eq 'ARRAY') ? $args->{embed} : [ $args->{embed} ];
53
    if (defined $args->{embed}) {
54
        %embed = map { $_ => 1 }  @{$args_arr};
55
        delete $args->{embed};
56
    }
57
48
58
    # Get the pipe-separated string of hidden ILL statuses
49
        return $c->render(
59
    my $hidden_statuses_string = C4::Context->preference('ILLHiddenRequestStatuses') // q{};
50
            status  => 200,
60
    # Turn into arrayref
51
            openapi => $reqs,
61
    my $hidden_statuses = [ split /\|/, $hidden_statuses_string ];
52
        );
62
53
    } catch {
63
    # Get all requests
54
        $c->unhandled_exception($_);
64
    # If necessary, only get those from a specified patron
65
    my @requests = Koha::Illrequests->search({
66
        $hidden_statuses
67
        ? (
68
            -and => {
69
                status => { 'not in' => $hidden_statuses },
70
                status_alias => [ -or =>
71
                    { 'not in' => $hidden_statuses },
72
                    { '=' => undef }
73
                ]
74
            }
75
        )
76
        : (),
77
        $args->{borrowernumber}
78
        ? ( borrowernumber => $args->{borrowernumber} )
79
        : ()
80
    })->as_list;
81
82
    my $fetch_backends = {};
83
    foreach my $request (@requests) {
84
        $fetch_backends->{ $request->backend } ||=
85
          Koha::Illrequest->new->load_backend( $request->backend );
86
    }
87
88
    # Pre-load the backend object to avoid useless backend lookup/loads
89
    @requests = map { $_->_backend( $fetch_backends->{ $_->backend } ); $_ } @requests;
90
91
    # Identify patrons & branches that
92
    # we're going to need and get them
93
    my $to_fetch = {
94
        patrons      => {},
95
        branches     => {},
96
        capabilities => {}
97
    };
55
    };
98
    foreach my $req (@requests) {
99
        $to_fetch->{patrons}->{$req->borrowernumber} = 1 if $embed{patron};
100
        $to_fetch->{branches}->{$req->branchcode} = 1 if $embed{library};
101
        $to_fetch->{capabilities}->{$req->backend} = 1 if $embed{capabilities};
102
    }
103
104
    # Fetch the patrons we need
105
    my $patron_arr = [];
106
    if ($embed{patron}) {
107
        my @patron_ids = keys %{$to_fetch->{patrons}};
108
        if (scalar @patron_ids > 0) {
109
            my $where = {
110
                borrowernumber => { -in => \@patron_ids }
111
            };
112
            $patron_arr = Koha::Patrons->search($where)->unblessed;
113
        }
114
    }
115
116
    # Fetch the branches we need
117
    my $branch_arr = [];
118
    if ($embed{library}) {
119
        my @branchcodes = keys %{$to_fetch->{branches}};
120
        if (scalar @branchcodes > 0) {
121
            my $where = {
122
                branchcode => { -in => \@branchcodes }
123
            };
124
            $branch_arr = Koha::Libraries->search($where)->unblessed;
125
        }
126
    }
127
128
    # Fetch the capabilities we need
129
    if ($embed{capabilities}) {
130
        my @backends = keys %{$to_fetch->{capabilities}};
131
        if (scalar @backends > 0) {
132
            foreach my $bc(@backends) {
133
                $to_fetch->{$bc} = $fetch_backends->{$bc}->capabilities;
134
            }
135
        }
136
    }
137
138
    # Now we've got all associated users and branches,
139
    # we can augment the request objects
140
    my @output = ();
141
    foreach my $req(@requests) {
142
        my $to_push = $req->unblessed;
143
        $to_push->{id_prefix} = $req->id_prefix;
144
        # Create new "formatted" columns for each date column
145
        # that needs formatting
146
        foreach my $field(@format_dates) {
147
            if (defined $to_push->{$field}) {
148
                $to_push->{$field . "_formatted"} = format_sqldatetime(
149
                    $to_push->{$field},
150
                    undef,
151
                    undef,
152
                    1
153
                );
154
            }
155
        }
156
157
        foreach my $p(@{$patron_arr}) {
158
            if ($p->{borrowernumber} == $req->borrowernumber) {
159
                $to_push->{patron} = {
160
                    patron_id => $p->{borrowernumber},
161
                    firstname      => $p->{firstname},
162
                    surname        => $p->{surname},
163
                    cardnumber     => $p->{cardnumber}
164
                };
165
                last;
166
            }
167
        }
168
        foreach my $b(@{$branch_arr}) {
169
            if ($b->{branchcode} eq $req->branchcode) {
170
                $to_push->{library} = $b;
171
                last;
172
            }
173
        }
174
        if ($embed{metadata}) {
175
            my $metadata = Koha::Illrequestattributes->search(
176
                { illrequest_id => $req->illrequest_id },
177
                { columns => [qw/type value/] }
178
            )->unblessed;
179
            my $meta_hash = {};
180
            foreach my $meta(@{$metadata}) {
181
                $meta_hash->{$meta->{type}} = $meta->{value};
182
            }
183
            $to_push->{metadata} = $meta_hash;
184
        }
185
        if ($embed{capabilities}) {
186
            $to_push->{capabilities} = $to_fetch->{$req->backend};
187
        }
188
        if ($embed{comments}) {
189
            $to_push->{comments} = $req->illcomments->count;
190
        }
191
        if ($embed{status_alias}) {
192
            $to_push->{status_alias} = $req->statusalias;
193
        }
194
        if ($embed{requested_partners}) {
195
            $to_push->{requested_partners} = $req->requested_partners;
196
        }
197
        push @output, $to_push;
198
    }
199
200
    return $c->render( status => 200, openapi => \@output );
201
}
56
}
202
57
203
1;
58
1;
(-)a/t/db_dependent/Koha/Illrequests.t (-1 / +62 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# Copyright 2023 Koha Development team
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 Test::More tests => 1;
23
24
use Koha::Illrequests;
25
26
use t::lib::TestBuilder;
27
use t::lib::Mocks;
28
29
my $builder = t::lib::TestBuilder->new;
30
my $schema = Koha::Database->new->schema;
31
32
subtest 'filter_by_visible() tests' => sub {
33
34
    plan tests => 4;
35
36
    $schema->storage->txn_begin;
37
38
    my $req = $builder->build_object({ class => 'Koha::Illrequests', value => { status => 'REQ', biblio_id => undef } });
39
    my $chk = $builder->build_object({ class => 'Koha::Illrequests', value => { status => 'CHK', biblio_id => undef } });
40
    my $ret = $builder->build_object({ class => 'Koha::Illrequests', value => { status => 'RET', biblio_id => undef } });
41
42
    my $reqs_rs = Koha::Illrequests->search(
43
        {
44
            illrequest_id => [ $req->id, $chk->id, $ret->id ]
45
        }
46
    );
47
48
    is( $reqs_rs->count, 3, 'Count is correct' );
49
50
    t::lib::Mocks::mock_preference('ILLHiddenRequestStatuses', '');
51
52
    is( $reqs_rs->filter_by_visible->count, 3, 'No hidden statuses, same count' );
53
54
    t::lib::Mocks::mock_preference('ILLHiddenRequestStatuses', 'CHK|RET');
55
56
    my $filtered_reqs = $reqs_rs->filter_by_visible;
57
58
    is( $filtered_reqs->count, 1, 'Count is correct' );
59
    is( $filtered_reqs->next->status, 'REQ' );
60
61
    $schema->storage->txn_rollback;
62
};

Return to bug 22440