| 
      
            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;  |