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

(-)a/Koha/Hold/CancellationRequest.pm (+119 lines)
Line 0 Link Here
1
package Koha::Hold::CancellationRequest;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
21
use Koha::Database;
22
use Koha::Exceptions;
23
24
use base qw(Koha::Object);
25
26
=head1 NAME
27
28
Koha::Hold::CancellationRequest - Koha hold cancellation request Object class
29
30
=head1 API
31
32
=head2 Class methods
33
34
=head3 store
35
36
    $request->store;
37
38
Overloaded I<store> method that performs some checks.
39
40
=cut
41
42
sub store {
43
    my ($self) = @_;
44
45
    if ( !$self->in_storage ) { # new request
46
        $self->creation_date( \'NOW()' )
47
          unless $self->creation_date;
48
    }
49
50
    return $self->SUPER::store;
51
}
52
53
=head3 accept
54
55
    $request->accept( $resolver_id );
56
57
Mark the cancellation request as accepted.
58
59
=cut
60
61
sub accept {
62
    my ($self, $resolver_id) = @_;
63
64
    Koha::Exceptions::InvalidStatus->throw( invalid_status => $self->status )
65
      unless $self->status eq 'requested';
66
67
    $resolver_id //= C4::Context->userenv ? C4::Context->userenv->{number} : undef;
68
69
    return $self->set(
70
        {
71
            resolution_date => \'NOW()',
72
            resolver_id     => $resolver_id,
73
            status          => 'accepted',
74
        }
75
    )->store;
76
}
77
78
=head3 reject
79
80
    $request->reject( $resolver_id );
81
82
Mark the cancellation request as rejected.
83
84
=cut
85
86
sub reject {
87
    my ($self, $resolver_id) = @_;
88
89
    Koha::Exceptions::InvalidStatus->throw( invalid_status => $self->status )
90
      unless $self->status eq 'requested';
91
92
    $resolver_id //= C4::Context->userenv ? C4::Context->userenv->{number} : undef;
93
94
    return $self->set(
95
        {
96
            resolution_date => \'NOW()',
97
            resolver_id     => $resolver_id,
98
            status          => 'rejected',
99
        }
100
    )->store;
101
}
102
103
=head2 Internal methods
104
105
=head3 _type
106
107
=cut
108
109
sub _type {
110
    return 'HoldCancellationRequest';
111
}
112
113
=head1 AUTHORS
114
115
Tomas Cohen Arazi <tomascohen@theke.io>
116
117
=cut
118
119
1;
(-)a/Koha/Hold/CancellationRequests.pm (+117 lines)
Line 0 Link Here
1
package Koha::Hold::CancellationRequests;
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
21
use Koha::Database;
22
23
use Koha::Hold::CancellationRequest;
24
25
use base qw(Koha::Objects);
26
27
=head1 NAME
28
29
Koha::Hold::CancellationRequests - Koha hold cancellation requests Object set class
30
31
=head1 API
32
33
=head2 Class methods
34
35
=head3 filter_by_current
36
37
=cut
38
39
sub filter_by_current {
40
    my ($self) = @_;
41
42
    return $self->search({ status => 'requested' });
43
}
44
45
=head3 accept
46
47
    $hold->cancellation_requests->filter_by_current->accept( $resolver_id );
48
49
This method accepts all cancellation requests in the resultset.
50
51
=cut
52
53
sub accept {
54
    my ( $self, $resolver_id ) = @_;
55
56
    $resolver_id //= C4::Context->userenv ? C4::Context->userenv->{number} : undef;
57
58
    $self->_resultset->result_source->schema->txn_do(
59
        sub {
60
            while ( my $req = $self->next ) {
61
                $req->accept($resolver_id);
62
            }
63
        }
64
    );
65
66
    return $self;
67
}
68
69
=head3 reject
70
71
    $hold->cancellation_requests->filter_by_current->reject( $resolver_id );
72
73
This method rejects all cancellation requests in the resultset.
74
75
=cut
76
77
sub reject {
78
    my ( $self, $resolver_id ) = @_;
79
80
    $resolver_id //= C4::Context->userenv ? C4::Context->userenv->{number} : undef;
81
82
    $self->_resultset->result_source->schema->txn_do(
83
        sub {
84
            while ( my $req = $self->next ) {
85
                $req->reject($resolver_id);
86
            }
87
        }
88
    );
89
90
    return $self;
91
}
92
93
=head2 Internal methods
94
95
=head3 _type
96
97
=cut
98
99
sub _type {
100
    return 'HoldCancellationRequest';
101
}
102
103
=head3 object_class
104
105
=cut
106
107
sub object_class {
108
    return 'Koha::Hold::CancellationRequest';
109
}
110
111
=head1 AUTHORS
112
113
Tomas Cohen Arazi <tomascohen@theke.io>
114
115
=cut
116
117
1;
(-)a/t/db_dependent/Koha/Hold/CancellationRequest.t (+145 lines)
Line 0 Link Here
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 3;
21
use Test::Exception;
22
23
use Koha::Hold::CancellationRequests;
24
25
use t::lib::TestBuilder;
26
27
my $schema  = Koha::Database->new->schema;
28
my $builder = t::lib::TestBuilder->new;
29
30
subtest 'store() tests' => sub {
31
32
    plan tests => 2;
33
34
    $schema->storage->txn_begin;
35
36
    my $item = $builder->build_sample_item;
37
    my $hold = $builder->build_object(
38
        {
39
            class => 'Koha::Holds',
40
            value => {
41
                found      => 'W', # waiting hold
42
                itemnumber => $item->id,
43
            }
44
        }
45
    );
46
47
    my $creation_date = '2021-06-25 14:05:35';
48
49
    my $request = Koha::Hold::CancellationRequest->new(
50
        {
51
            creation_date => undef,
52
            hold_id       => $hold->id
53
        }
54
    )->store;
55
56
    isnt( $request->creation_date, undef, 'creation_date is always set' );
57
58
    $request = Koha::Hold::CancellationRequest->new(
59
        {
60
            creation_date => $creation_date,
61
            hold_id       => $hold->id
62
        }
63
    )->store;
64
65
    is( $request->creation_date, $creation_date, 'Passed creation_date set' );
66
67
    $schema->storage->txn_rollback;
68
};
69
70
subtest 'accept() tests' => sub {
71
72
    plan tests => 3;
73
74
    $schema->storage->txn_begin;
75
76
    my $item = $builder->build_sample_item;
77
    my $hold = $builder->build_object(
78
        {
79
            class => 'Koha::Holds',
80
            value => {
81
                found      => 'W', # waiting hold
82
                itemnumber => $item->id,
83
            }
84
        }
85
    );
86
87
    # add a request with an unacceptable status
88
    my $request = Koha::Hold::CancellationRequest->new( { hold_id => $hold->id, status => 'accepted' } )->store;
89
90
    throws_ok
91
      { $request->accept; }
92
      'Koha::Exceptions::InvalidStatus',
93
      "Exception thrown when the request status is not 'requested'";
94
95
    is( $@->invalid_status, 'accepted' );
96
97
    # make the request status acceptable
98
    $request->status('requested')->store;
99
    $request->discard_changes;
100
101
    # accept it
102
    $request->accept;
103
104
    is( $request->status, 'accepted' );
105
106
    $schema->storage->txn_rollback;
107
};
108
109
subtest 'reject() tests' => sub {
110
111
    plan tests => 3;
112
113
    $schema->storage->txn_begin;
114
115
    my $item = $builder->build_sample_item;
116
    my $hold = $builder->build_object(
117
        {
118
            class => 'Koha::Holds',
119
            value => {
120
                found      => 'W', # waiting hold
121
                itemnumber => $item->id,
122
            }
123
        }
124
    );
125
126
    # add a request with an unrejectable status
127
    my $request = Koha::Hold::CancellationRequest->new( { hold_id => $hold->id, status => 'accepted' } )->store;
128
129
    throws_ok
130
      { $request->reject; }
131
      'Koha::Exceptions::InvalidStatus',
132
      "Exception thrown when the request status is not 'requested'";
133
134
    is( $@->invalid_status, 'accepted' );
135
136
    # make the request status acceptable
137
    $request->status('requested')->store;
138
139
    # accept it
140
    $request->reject;
141
142
    is( $request->status, 'rejected' );
143
144
    $schema->storage->txn_rollback;
145
};
(-)a/t/db_dependent/Koha/Hold/CancellationRequests.t (-1 / +273 lines)
Line 0 Link Here
0
- 
1
#!/usr/bin/perl
2
3
# This file is part of Koha.
4
#
5
# Koha is free software; you can redistribute it and/or modify it
6
# under the terms of the GNU General Public License as published by
7
# the Free Software Foundation; either version 3 of the License, or
8
# (at your option) any later version.
9
#
10
# Koha is distributed in the hope that it will be useful, but
11
# WITHOUT ANY WARRANTY; without even the implied warranty of
12
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13
# GNU General Public License for more details.
14
#
15
# You should have received a copy of the GNU General Public License
16
# along with Koha; if not, see <http://www.gnu.org/licenses>.
17
18
use Modern::Perl;
19
20
use Test::More tests => 3;
21
use Test::Exception;
22
23
use Koha::Database;
24
25
use t::lib::TestBuilder;
26
27
my $schema  = Koha::Database->new->schema;
28
my $builder = t::lib::TestBuilder->new;
29
30
subtest 'filter_by_current() tests' => sub {
31
32
    plan tests => 3;
33
34
    $schema->storage->txn_begin;
35
36
    my $requested = $builder->build_object(
37
        {
38
            class => 'Koha::Hold::CancellationRequests',
39
            value => { status => 'requested' }
40
        }
41
    );
42
    my $accepted = $builder->build_object(
43
        {
44
            class => 'Koha::Hold::CancellationRequests',
45
            value => { status => 'accepted' }
46
        }
47
    );
48
    my $rejected = $builder->build_object(
49
        {
50
            class => 'Koha::Hold::CancellationRequests',
51
            value => { status => 'rejected' }
52
        }
53
    );
54
55
    my $rs = Koha::Hold::CancellationRequests->search(
56
        {
57
            hold_cancellation_request_id => [
58
                $requested->id,
59
                $accepted->id,
60
                $rejected->id,
61
            ]
62
        }
63
    );
64
65
    is( $rs->count, 3 );
66
67
    my $rs_current = $rs->filter_by_current;
68
69
    is( $rs_current->count, 1 );
70
    is( $rs_current->next->status, 'requested' );
71
72
    $schema->storage->txn_rollback;
73
};
74
75
subtest 'reject() tests' => sub {
76
77
    plan tests => 12;
78
79
    $schema->storage->txn_begin;
80
81
    my $requested_1 = $builder->build_object(
82
        {
83
            class => 'Koha::Hold::CancellationRequests',
84
            value => { status => 'requested' }
85
        }
86
    );
87
    my $requested_2 = $builder->build_object(
88
        {
89
            class => 'Koha::Hold::CancellationRequests',
90
            value => { status => 'requested' }
91
        }
92
    );
93
    my $accepted = $builder->build_object(
94
        {
95
            class => 'Koha::Hold::CancellationRequests',
96
            value => { status => 'accepted' }
97
        }
98
    );
99
    my $rejected = $builder->build_object(
100
        {
101
            class => 'Koha::Hold::CancellationRequests',
102
            value => { status => 'rejected' }
103
        }
104
    );
105
106
    my $requests = [ $requested_1, $requested_2, $accepted ];
107
108
    my $rs = Koha::Hold::CancellationRequests->search(
109
        {
110
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
111
        }
112
    );
113
114
    throws_ok
115
      { $rs->reject; }
116
      'Koha::Exceptions::InvalidStatus',
117
      'Exception thrown for accepted request';
118
119
    is( $@->invalid_status, 'accepted', "'invalid_status' parameter set to the conflicting value" );
120
121
    # reset iterator
122
    $rs->reset;
123
124
    my $i = 0;
125
    while ( my $req = $rs->next ) {
126
        is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" );
127
        $i++;
128
    }
129
130
    $requests = [ $requested_1, $requested_2, $rejected ];
131
132
    $rs = Koha::Hold::CancellationRequests->search(
133
        {
134
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
135
        }
136
    );
137
138
    throws_ok
139
      { $rs->reject; }
140
      'Koha::Exceptions::InvalidStatus',
141
      'Exception thrown for accepted request';
142
143
    is( $@->invalid_status, 'rejected', "'invalid_status' parameter set to the conflicting value" );
144
145
    # reset iterator
146
    $rs->reset;
147
148
    $i = 0;
149
    while ( my $req = $rs->next ) {
150
        is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" );
151
        $i++;
152
    }
153
154
    $requests = [ $requested_1, $requested_2 ];
155
156
    $rs = Koha::Hold::CancellationRequests->search(
157
        {
158
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
159
        }
160
    );
161
162
    $rs = $rs->reject;
163
164
    $rs->reset;
165
166
    $i = 0;
167
    while ( my $req = $rs->next ) {
168
        is( $req->status, 'rejected', "status is 'rejected'" );
169
        $i++;
170
    }
171
172
    $schema->storage->txn_rollback;
173
};
174
175
subtest 'accept() tests' => sub {
176
177
    plan tests => 12;
178
179
    $schema->storage->txn_begin;
180
181
    my $requested_1 = $builder->build_object(
182
        {
183
            class => 'Koha::Hold::CancellationRequests',
184
            value => { status => 'requested' }
185
        }
186
    );
187
    my $requested_2 = $builder->build_object(
188
        {
189
            class => 'Koha::Hold::CancellationRequests',
190
            value => { status => 'requested' }
191
        }
192
    );
193
    my $accepted = $builder->build_object(
194
        {
195
            class => 'Koha::Hold::CancellationRequests',
196
            value => { status => 'accepted' }
197
        }
198
    );
199
    my $rejected = $builder->build_object(
200
        {
201
            class => 'Koha::Hold::CancellationRequests',
202
            value => { status => 'rejected' }
203
        }
204
    );
205
206
    my $requests = [ $requested_1, $requested_2, $accepted ];
207
208
    my $rs = Koha::Hold::CancellationRequests->search(
209
        {
210
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
211
        }
212
    );
213
214
    throws_ok
215
      { $rs->accept; }
216
      'Koha::Exceptions::InvalidStatus',
217
      'Exception thrown for accepted request';
218
219
    is( $@->invalid_status, 'accepted', "'invalid_status' parameter set to the conflicting value" );
220
221
    # reset iterator
222
    $rs->reset;
223
224
    my $i = 0;
225
    while ( my $req = $rs->next ) {
226
        is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" );
227
        $i++;
228
    }
229
230
    $requests = [ $requested_1, $requested_2, $rejected ];
231
232
    $rs = Koha::Hold::CancellationRequests->search(
233
        {
234
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
235
        }
236
    );
237
238
    throws_ok
239
      { $rs->accept; }
240
      'Koha::Exceptions::InvalidStatus',
241
      'Exception thrown for accepted request';
242
243
    is( $@->invalid_status, 'rejected', "'invalid_status' parameter set to the conflicting value" );
244
245
    # reset iterator
246
    $rs->reset;
247
248
    $i = 0;
249
    while ( my $req = $rs->next ) {
250
        is( $req->status, $requests->[$i]->status, "After exception, status is kept (" . $req->status . ")" );
251
        $i++;
252
    }
253
254
    $requests = [ $requested_1, $requested_2 ];
255
256
    $rs = Koha::Hold::CancellationRequests->search(
257
        {
258
            hold_cancellation_request_id => [ map { $_->id } @{ $requests } ]
259
        }
260
    );
261
262
    $rs = $rs->accept;
263
264
    $rs->reset;
265
266
    $i = 0;
267
    while ( my $req = $rs->next ) {
268
        is( $req->status, 'accepted', "status is 'accepted'" );
269
        $i++;
270
    }
271
272
    $schema->storage->txn_rollback;
273
};

Return to bug 22456