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

(-)a/Koha/REST/V1/CirculationRules.pm (+86 lines)
Lines 179-182 sub list_rules { Link Here
179
    };
179
    };
180
}
180
}
181
181
182
=head3 set_rules
183
184
Set rules for the given patron/item/branch combination
185
186
=cut
187
188
sub set_rules {
189
    my $c = shift->openapi->valid_input or return;
190
191
    return try {
192
        my $body = $c->req->json;
193
194
        my $item_type       = $body->{context}->{item_type_id};
195
        my $branchcode      = $body->{context}->{library_id};
196
        my $patron_category = $body->{context}->{patron_category_id};
197
198
        if ( $item_type eq '*' ) {
199
            $item_type = undef;
200
        } else {
201
            my $type = Koha::ItemTypes->find($item_type);
202
            return $c->render_invalid_parameter_value(
203
                {
204
                    path   => '/body/context/item_type_id',
205
                    values => {
206
                        uri   => '/api/v1/item_types',
207
                        field => 'item_type_id'
208
                    }
209
                }
210
            ) unless $type;
211
        }
212
213
        if ( $branchcode eq '*' ) {
214
            $branchcode = undef;
215
        } else {
216
            my $library = Koha::Libraries->find($branchcode);
217
            return $c->render_invalid_parameter_value(
218
                {
219
                    path   => '/body/context/library_id',
220
                    values => {
221
                        uri   => '/api/v1/libraries',
222
                        field => 'library_id'
223
                    }
224
                }
225
            ) unless $library;
226
        }
227
228
        if ( $patron_category eq '*' ) {
229
            $patron_category = undef;
230
        } else {
231
            my $category = Koha::Patron::Categories->find($patron_category);
232
            return $c->render_invalid_parameter_value(
233
                {
234
                    path   => '/body/context/patron_category_id',
235
                    values => {
236
                        uri   => '/api/v1/patron_categories',
237
                        field => 'patron_category_id'
238
                    }
239
                }
240
            ) unless $category;
241
        }
242
243
        my $rules = {%$body};
244
        delete $rules->{context};
245
246
        my $new_rules = Koha::CirculationRules->set_rules(
247
            {
248
                categorycode => $patron_category,
249
                itemtype     => $item_type,
250
                branchcode   => $branchcode,
251
                rules        => $rules,
252
            }
253
        );
254
255
        # TODO: Add error handling for rule scope exceptions thrown in Koha::CirculationRules::set_rule
256
257
        my $return = { map { $_->rule_name => $_->rule_value } @{$new_rules} };
258
        $return->{context} =
259
            { library_id => $branchcode, patron_category_id => $patron_category, item_type_id => $item_type };
260
261
        return $c->render(
262
            status  => 200,
263
            openapi => $return
264
        );
265
    }
266
}
267
182
1;
268
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