@@ -, +, @@ --- Koha/REST/V1.pm | 4 ++-- Koha/REST/V1/Suggestions.pm | 31 +++++++++++++++++++++++++----- api/v1/swagger/definitions/suggestion.json | 3 +-- 3 files changed, 29 insertions(+), 9 deletions(-) --- a/Koha/REST/V1.pm +++ a/Koha/REST/V1.pm @@ -216,8 +216,8 @@ sub check_object_ownership { borrowernumber => \&_object_ownership_by_borrowernumber, checkout_id => \&_object_ownership_by_checkout_id, reserve_id => \&_object_ownership_by_reserve_id, - suggestionid => \&_object_ownership_by_suggestionid, - suggestedby => \&_object_ownership_by_suggestedby, + suggestionid => \&_object_ownership_by_suggestionid, + suggestedby => \&_object_ownership_by_suggestedby, }; foreach my $param ( keys %{ $parameters } ) { --- a/Koha/REST/V1/Suggestions.pm +++ a/Koha/REST/V1/Suggestions.pm @@ -184,11 +184,24 @@ sub _validate_body { join(', ', @allowed_fields)}, 403); } } + + # Set user's branchcode if not specified (after validation input, because user does not + # necessarily have rights to choose a branchcode) + if ( not $updating and not exists $body->{branchcode} ) { + $body->{branchcode} = C4::Context->userenv->{branch}; + } + } if (not $updating) { # Check for missing fields my @mandatory_fields = split /,/, C4::Context->preference('OPACSuggestionMandatoryFields'); + + # Title is mandatory by default if none specified in settings + if (not @mandatory_fields) { + push (@mandatory_fields, 'title'); + } + my @missing_fields = (); for my $mandatory_field (@mandatory_fields) { push(@missing_fields, $mandatory_field) if (not exists $body->{$mandatory_field}); @@ -200,12 +213,20 @@ sub _validate_body { } # Is suggester anonymous? - my $is_anonymous = not (defined $body->{suggestedby} and + my $is_anonymous = not (exists $body->{suggestedby} and $body->{suggestedby} ne '' and $body->{suggestedby} ne C4::Context->preference('AnonymousPatron')); + # Is updated suggestedby anonymous? (Can be true only if "suggestedby" is defined!) + my $updated_is_anonymous = ( $updating and + exists $body->{suggestedby} and ( + $body->{suggestedby} eq '' or + $body->{suggestedby} eq C4::Context->preference('AnonymousPatron') + ) + ); + # Refuse if are anonymous suggestions disabled - if ( $is_anonymous ) { + if ( ($is_anonymous and not $updating) or $updated_is_anonymous ) { return $c->$cb({error => 'Anonymous suggestions are disabled'}, 403) unless C4::Context->preference('AnonSuggestions'); } @@ -230,21 +251,21 @@ sub _validate_body { if ( exists $body->{itemtype} ) { my @item_types = map {$_->unblessed->{itemtype}} Koha::ItemTypes->search; return $c->$cb({error => 'itemtype must be one of ' . join(', ', @item_types)}, 400) - unless /^$body->{itemtype}$/ ~~ @item_types; + unless grep(/^$body->{itemtype}$/, @item_types); } # Check branchcode is valid if ( exists $body->{branchcode} ) { my @branch_codes = map {$_->unblessed->{branchcode}} Koha::Libraries->search; return $c->$cb({error => 'branchcode must be one of ' . join(', ', @branch_codes)}, 400) - unless /^$body->{branchcode}$/ ~~ @branch_codes; + unless grep(/^$body->{branchcode}$/, @branch_codes); } # Check patron reason is valid if ( exists $body->{patronreason} ) { my @authorized_values = map { $_->{authorised_value} } @{ C4::Koha::GetAuthorisedValues('OPAC_SUG') }; return $c->$cb({error => 'patronreason must be one of ' . join(', ', @authorized_values)}, 400) - unless /^$body->{patronreason}$/ ~~ @authorized_values; + unless grep(/^$body->{patronreason}$/, @authorized_values); } # Check suggestedby patron exists --- a/api/v1/swagger/definitions/suggestion.json +++ a/api/v1/swagger/definitions/suggestion.json @@ -126,6 +126,5 @@ "description": "suggested total cost (price*quantity updated for currency)" } }, - "additionalProperties": false, - "required": ["title"] + "additionalProperties": false } --