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

(-)a/Koha/REST/V1/CirculationRules.pm (+86 lines)
Lines 178-181 sub list_rules { Link Here
178
    };
178
    };
179
}
179
}
180
180
181
=head3 set_rules
182
183
Set rules for the given patron/item/branch combination
184
185
=cut
186
187
sub set_rules {
188
    my $c = shift->openapi->valid_input or return;
189
190
    return try {
191
        my $body = $c->req->json;
192
193
        my $item_type       = $body->{context}->{item_type_id};
194
        my $branchcode      = $body->{context}->{library_id};
195
        my $patron_category = $body->{context}->{patron_category_id};
196
197
        if ( $item_type eq '*' ) {
198
            $item_type = undef;
199
        } else {
200
            my $type = Koha::ItemTypes->find($item_type);
201
            return $c->render_invalid_parameter_value(
202
                {
203
                    path   => '/body/context/item_type_id',
204
                    values => {
205
                        uri   => '/api/v1/item_types',
206
                        field => 'item_type_id'
207
                    }
208
                }
209
            ) unless $type;
210
        }
211
212
        if ( $branchcode eq '*' ) {
213
            $branchcode = undef;
214
        } else {
215
            my $library = Koha::Libraries->find($branchcode);
216
            return $c->render_invalid_parameter_value(
217
                {
218
                    path   => '/body/context/library_id',
219
                    values => {
220
                        uri   => '/api/v1/libraries',
221
                        field => 'library_id'
222
                    }
223
                }
224
            ) unless $library;
225
        }
226
227
        if ( $patron_category eq '*' ) {
228
            $patron_category = undef;
229
        } else {
230
            my $category = Koha::Patron::Categories->find($patron_category);
231
            return $c->render_invalid_parameter_value(
232
                {
233
                    path   => '/body/context/patron_category_id',
234
                    values => {
235
                        uri   => '/api/v1/patron_categories',
236
                        field => 'patron_category_id'
237
                    }
238
                }
239
            ) unless $category;
240
        }
241
242
        my $rules = {%$body};
243
        delete $rules->{context};
244
245
        my $new_rules = Koha::CirculationRules->set_rules(
246
            {
247
                categorycode => $patron_category,
248
                itemtype     => $item_type,
249
                branchcode   => $branchcode,
250
                rules        => $rules,
251
            }
252
        );
253
254
        # TODO: Add error handling for rule scope exceptions thrown in Koha::CirculationRules::set_rule
255
256
        my $return = { map { $_->rule_name => $_->rule_value } @{$new_rules} };
257
        $return->{context} =
258
            { library_id => $branchcode, patron_category_id => $patron_category, item_type_id => $item_type };
259
260
        return $c->render(
261
            status  => 200,
262
            openapi => $return
263
        );
264
    }
265
}
266
181
1;
267
1;
(-)a/api/v1/swagger/paths/circulation_rules.yaml (-1 / +54 lines)
Lines 67-72 Link Here
67
    x-koha-authorization:
67
    x-koha-authorization:
68
      permissions:
68
      permissions:
69
        - circulate: circulate_remaining_permissions
69
        - circulate: circulate_remaining_permissions
70
  put:
71
    x-mojo-to: CirculationRules#set_rules
72
    operationId: setCirculationRules
73
    tags:
74
      - circulation_rules
75
    summary: Update circulation rules
76
    parameters:
77
      - name: body
78
        in: body
79
        description: A JSON object containing new information about circulation rules
80
        required: true
81
        schema:
82
          $ref: "../swagger.yaml#/definitions/circulation_rules"
83
    consumes:
84
      - application/json
85
    produces:
86
      - application/json
87
    responses:
88
      "200":
89
        description: A successfully updated circulation rules set
90
        schema:
91
          items:
92
            $ref: "../swagger.yaml#/definitions/circulation_rules"
93
      "400":
94
        description: |
95
          Bad request.
96
        schema:
97
          $ref: "../swagger.yaml#/definitions/error"
98
      "403":
99
        description: Access forbidden
100
        schema:
101
          $ref: "../swagger.yaml#/definitions/error"
102
      "404":
103
        description: Resource not found
104
        schema:
105
          $ref: "../swagger.yaml#/definitions/error"
106
      "409":
107
        description: Conflict in updating resource
108
        schema:
109
          $ref: "../swagger.yaml#/definitions/error"
110
      "500":
111
        description: |
112
          Internal server error. Possible `error_code` attribute values:
113
114
          * `internal_server_error`
115
        schema:
116
          $ref: "../swagger.yaml#/definitions/error"
117
      "503":
118
        description: Under maintenance
119
        schema:
120
          $ref: "../swagger.yaml#/definitions/error"
121
    x-koha-authorization:
122
      permissions:
123
        - circulate: circulate_remaining_permissions
70
/circulation_rules/kinds:
124
/circulation_rules/kinds:
71
  get:
125
  get:
72
    x-mojo-to: CirculationRules#get_kinds
126
    x-mojo-to: CirculationRules#get_kinds
73
- 

Return to bug 37256