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

(-)a/Koha/REST/V1/CirculationRules.pm (-12 / +35 lines)
Lines 40-59 sub get_kinds { Link Here
40
    );
40
    );
41
}
41
}
42
42
43
=head3 list_effective_rules
43
=head3 list_rules
44
44
45
List all effective rules for the requested patron/item/branch combination
45
Get effective rules for the requested patron/item/branch combination
46
46
47
=cut
47
=cut
48
48
49
sub list_effective_rules {
49
sub list_rules {
50
    my $c = shift->openapi->valid_input or return;
50
    my $c = shift->openapi->valid_input or return;
51
51
52
    return try {
52
    return try {
53
        my $effective       = $c->param('effective') // 1;
53
        my $item_type       = $c->param('item_type_id');
54
        my $item_type       = $c->param('item_type_id');
54
        my $branchcode      = $c->param('library_id');
55
        my $branchcode      = $c->param('library_id');
55
        my $patron_category = $c->param('patron_category_id');
56
        my $patron_category = $c->param('patron_category_id');
56
        my $rules           = $c->param('rules') // [ keys %{ Koha::CirculationRules->rule_kinds } ];
57
        my $kinds           = $c->param('rules') // [ keys %{ Koha::CirculationRules->rule_kinds } ];
57
58
58
        if ($item_type) {
59
        if ($item_type) {
59
            my $type = Koha::ItemTypes->find($item_type);
60
            my $type = Koha::ItemTypes->find($item_type);
Lines 94-111 sub list_effective_rules { Link Here
94
            ) unless $category;
95
            ) unless $category;
95
        }
96
        }
96
97
97
        my $effective_rules = Koha::CirculationRules->get_effective_rules(
98
        my $rules;
98
            {
99
        if ($effective) {
99
                categorycode => $patron_category,
100
100
                itemtype     => $item_type,
101
            my $effective_rules = Koha::CirculationRules->get_effective_rules(
101
                branchcode   => $branchcode,
102
                {
102
                rules        => $rules
103
                    categorycode => $patron_category,
104
                    itemtype     => $item_type,
105
                    branchcode   => $branchcode,
106
                    rules        => $kinds
107
                }
108
            ) // {};
109
            push @{$rules}, $effective_rules;
110
        } else {
111
            my $select = [ 'branchcode', 'categorycode', 'itemtype' ];
112
            my $as     = [ 'branchcode', 'categorycode', 'itemtype' ];
113
            for my $kind ( @{$kinds} ) {
114
                push @{$select}, { max => \[ "CASE WHEN rule_name = ? THEN rule_value END", $kind ], -as => $kind };
115
                push @{$as}, $kind;
103
            }
116
            }
104
        );
117
118
            $rules = Koha::CirculationRules->search(
119
                {},
120
                {
121
                    select   => $select,
122
                    as       => $as,
123
                    group_by => [ 'branchcode', 'categorycode', 'itemtype' ]
124
                }
125
            )->unblessed;
126
127
        }
105
128
106
        return $c->render(
129
        return $c->render(
107
            status  => 200,
130
            status  => 200,
108
            openapi => $effective_rules ? $effective_rules : {}
131
            openapi => $rules
109
        );
132
        );
110
    } catch {
133
    } catch {
111
        $c->unhandled_exception($_);
134
        $c->unhandled_exception($_);
(-)a/api/v1/swagger/paths/circulation_rules.yaml (-4 / +9 lines)
Lines 1-7 Link Here
1
---
1
---
2
/circulation_rules:
2
/circulation_rules:
3
  get:
3
  get:
4
    x-mojo-to: CirculationRules#list_effective_rules
4
    x-mojo-to: CirculationRules#list_rules
5
    operationId: listCirculationRules
5
    operationId: listCirculationRules
6
    tags:
6
    tags:
7
      - circulation_rules
7
      - circulation_rules
Lines 9-14 Link Here
9
    produces:
9
    produces:
10
      - application/json
10
      - application/json
11
    parameters:
11
    parameters:
12
      - name: effective
13
        in: query
14
        description: Boolean indicating whether to return effective rules or all rules. Defaults to true.
15
        required: false
16
        type: boolean
12
      - name: item_type_id
17
      - name: item_type_id
13
        in: query
18
        in: query
14
        description: The item type identifier
19
        description: The item type identifier
Lines 36-44 Link Here
36
      "200":
41
      "200":
37
        description: A list of rules for this item type, library and patron category combination
42
        description: A list of rules for this item type, library and patron category combination
38
        schema:
43
        schema:
39
          type: object
44
          type: array
40
          additionalProperties:
45
          items:
41
            type: [ 'string', 'integer' ]
46
            type: object
42
      "400":
47
      "400":
43
        description: Bad request
48
        description: Bad request
44
        schema:
49
        schema:
(-)a/t/db_dependent/api/v1/circulation_rules.t (-8 / +6 lines)
Lines 32-39 my $builder = t::lib::TestBuilder->new; Link Here
32
my $t = Test::Mojo->new('Koha::REST::V1');
32
my $t = Test::Mojo->new('Koha::REST::V1');
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
33
t::lib::Mocks::mock_preference( 'RESTBasicAuth', 1 );
34
34
35
subtest 'list_effective_rules() tests' => sub {
35
subtest 'list_rules() tests' => sub {
36
37
    plan tests => 32;
36
    plan tests => 32;
38
37
39
    $schema->storage->txn_begin;
38
    $schema->storage->txn_begin;
Lines 65-71 subtest 'list_effective_rules() tests' => sub { Link Here
65
64
66
    ## Authorized user tests
65
    ## Authorized user tests
67
    # No circulation_rules, so empty hash should be returned
66
    # No circulation_rules, so empty hash should be returned
68
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( {} );
67
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is( '/0' => {} );
69
68
70
    # One rule created, should get returned
69
    # One rule created, should get returned
71
    ok(
70
    ok(
Lines 82-88 subtest 'list_effective_rules() tests' => sub { Link Here
82
    );
81
    );
83
82
84
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
83
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)
85
        ->json_is( '' => { 'fine' => 2 }, "Our single rule is returned" );
84
        ->json_is( '/0' => { 'fine' => 2 }, "Our single rule is returned" );
86
85
87
    # Two circulation_rules created, they should both be returned
86
    # Two circulation_rules created, they should both be returned
88
    ok(
87
    ok(
Lines 99-105 subtest 'list_effective_rules() tests' => sub { Link Here
99
    );
98
    );
100
99
101
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
100
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
102
        '' => {
101
        '/0' => {
103
            fine     => 2,
102
            fine     => 2,
104
            finedays => 5,
103
            finedays => 5,
105
        },
104
        },
Lines 121-127 subtest 'list_effective_rules() tests' => sub { Link Here
121
    );
120
    );
122
121
123
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200)->json_is(
122
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules?library_id=$branchcode")->status_is(200)->json_is(
124
        '' => {
123
        '/0' => {
125
            fine     => 4,
124
            fine     => 4,
126
            finedays => 5,
125
            finedays => 5,
127
        },
126
        },
Lines 129-135 subtest 'list_effective_rules() tests' => sub { Link Here
129
    );
128
    );
130
129
131
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
130
    $t->get_ok("//$userid:$password@/api/v1/circulation_rules")->status_is(200)->json_is(
132
        '' => {
131
        '/0' => {
133
            fine     => 2,
132
            fine     => 2,
134
            finedays => 5,
133
            finedays => 5,
135
        },
134
        },
136
- 

Return to bug 36641