@@ -, +, @@ --- Koha/REST/V1/Suggestions.pm | 18 ++++++++++++------ api/v1/swagger/definitions/suggestion.json | 9 ++++----- 2 files changed, 16 insertions(+), 11 deletions(-) --- a/Koha/REST/V1/Suggestions.pm +++ a/Koha/REST/V1/Suggestions.pm @@ -28,6 +28,7 @@ use Koha::Suggestions; use Koha::ItemTypes; use Koha::Libraries; +use List::MoreUtils qw/any/; use Try::Tiny; @@ -173,8 +174,8 @@ sub _validate_body { } foreach my $param ( keys %{ $body } ) { - unless (/^$param$/ ~~ @allowed_fields) { - # Ouch ! Some mandatory field is missing! + unless (any { /^$param$/ } @allowed_fields) { + # Ouch ! User trying to edit field he has no rights to edit! my $verb = 'specified '; return $c->render( status => 403 , openapi => { error => 'You ' . $verb . $param . ', but allowed fields are only ' . join(', ', @allowed_fields)} @@ -183,9 +184,14 @@ sub _validate_body { } } + # Set user's branchcode if not specified (after validation input, because user does not + # necessarily have rights to choose a branchcode) + if ( not exists $body->{branchcode} ) { + $body->{branchcode} = C4::Context->userenv->{branch}; + } # 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')); @@ -215,21 +221,21 @@ sub _validate_body { if ( exists $body->{itemtype} ) { my @item_types = map {$_->unblessed->{itemtype}} Koha::ItemTypes->search; return $c->render( status => 400 , openapi => {error => 'itemtype must be one of ' . join(', ', @item_types)} ) - 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->render( status => 400 , openapi => {error => 'branchcode must be one of ' . join(', ', @branch_codes)} ) - 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->render( status => 400, openapi => {error => 'patronreason must be one of ' . join(', ', @authorized_values)} ) - 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 @@ -6,11 +6,11 @@ "description": "unique identifier assigned automatically by Koha" }, "suggestedby": { - "type": "string", + "type": ["string", "null"], "description": "patron_id for the person making the suggestion, foreign key linking to the borrowers table" }, "suggesteddate": { - "type": "string", + "type": ["string", "null"], "description": "the suggestion was submitted" }, "managedby": { @@ -70,7 +70,7 @@ "description": "volume description" }, "publicationyear": { - "type": "string", + "type": ["string", "null"], "description": "year of publication" }, "place": { @@ -126,6 +126,5 @@ "description": "suggested total cost (price*quantity updated for currency)" } }, - "additionalProperties": false, - "required": ["title"] + "additionalProperties": false } --