Bug 30663

Summary: POST /api/v1/suggestions won't honor suggestions limits
Product: Koha Reporter: Lucas Gass (lukeg) <lucas>
Component: REST APIAssignee: Tomás Cohen Arazi (tcohen) <tomascohen>
Status: CLOSED FIXED QA Contact: Martin Renvoize (ashimema) <martin.renvoize>
Severity: major    
Priority: P5 - low CC: dcook, fridolin.somers, kyle, martin.renvoize, tomascohen, victor
Version: Main   
Hardware: All   
OS: All   
See Also: https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=21889
https://bugs.koha-community.org/bugzilla3/show_bug.cgi?id=30674
Change sponsored?: --- Patch complexity: Small patch
Documentation contact: Documentation submission:
Text to go in the release notes:
Version(s) released in:
22.05.00,21.11.06,21.05.16
Circulation function:
Attachments: Bug 30663: Add Koha::Suggestions helper methods
Bug 30663: Add x-koha-override options to /suggestions
Bug 30663: Implement overrides handling in POST /suggestions
Bug 30663: Add Koha::Suggestions helper methods
Bug 30663: Add x-koha-override options to /suggestions
Bug 30663: Implement overrides handling in POST /suggestions
Bug 30663: Add Koha::Suggestions helper methods
Bug 30663: Add x-koha-override options to /suggestions
Bug 30663: Implement overrides handling in POST /suggestions
[21.11.x] Bug 30663: Add x-koha-override options to /suggestions
[21.11.x] Bug 30663: Implement overrides handling in POST /suggestions

Description Lucas Gass (lukeg) 2022-05-02 20:29:08 UTC
To recreate:

-Set MaxOpenSuggestions to something low, like 1.
-Make some suggestions via the API, making sure they are not anonymous and they include a status of 'ASKED'.
-You can make as many requests as you want, regardless of MaxOpenSuggestions.

I am using this simple jQuery snippet to test:

$.ajax({
    type: 'POST',
    url: '/api/v1/suggestions/',
    data: '{"collection_title": "dasdas", "suggested_by": 5}',
    success: function(data) { console.log('suggestion added'); },
    contentType: "application/json",
    dataType: 'json'
});
Comment 1 Tomás Cohen Arazi (tcohen) 2022-05-03 14:29:50 UTC
Created attachment 134541 [details] [review]
Bug 30663: Add Koha::Suggestions helper methods

This patch adds the following helper methods:

* filter_by_pending
* filter_by_suggested_days_range

This methods follow basically what's done in opac-suggestions.pl
I chose 'pending' as opposed to 'open' to follow what we use in the UI
which might be the case because of being more accurate for end users.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Suggestions.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 2 Tomás Cohen Arazi (tcohen) 2022-05-03 14:29:55 UTC
Created attachment 134542 [details] [review]
Bug 30663: Add x-koha-override options to /suggestions

This patch adds the x-koha-override header parameter to the route that
is used to create suggestions, POST /suggestions.

The idea is that adding suggestions will be rejected under certain
conditions unless x-koha-override is passed with appropriate values. The
added overrides are:

* any
* max_total
* max_pending

Tests are added for the expected behavior.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 3 Tomás Cohen Arazi (tcohen) 2022-05-03 14:30:00 UTC
Created attachment 134543 [details] [review]
Bug 30663: Implement overrides handling in POST /suggestions

This patch implements the override checks in the controller as expected
by the previous patch.

To test:
1. Apply this bug patches up to 'Add x-koha-override options...'
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/suggestions.t
=> FAIL: Tests fail! The controller doesn't care about overrides or
         sysprefs about suggestions limits.
3. Apply this patch
4. Repeat 2
=> SUCCESS: Things work!
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Comment 4 Lucas Gass (lukeg) 2022-05-03 14:37:26 UTC
Created attachment 134544 [details] [review]
Bug 30663: Add Koha::Suggestions helper methods

This patch adds the following helper methods:

* filter_by_pending
* filter_by_suggested_days_range

This methods follow basically what's done in opac-suggestions.pl
I chose 'pending' as opposed to 'open' to follow what we use in the UI
which might be the case because of being more accurate for end users.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Suggestions.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Comment 5 Lucas Gass (lukeg) 2022-05-03 14:37:30 UTC
Created attachment 134545 [details] [review]
Bug 30663: Add x-koha-override options to /suggestions

This patch adds the x-koha-override header parameter to the route that
is used to create suggestions, POST /suggestions.

The idea is that adding suggestions will be rejected under certain
conditions unless x-koha-override is passed with appropriate values. The
added overrides are:

* any
* max_total
* max_pending

Tests are added for the expected behavior.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Comment 6 Lucas Gass (lukeg) 2022-05-03 14:37:34 UTC
Created attachment 134546 [details] [review]
Bug 30663: Implement overrides handling in POST /suggestions

This patch implements the override checks in the controller as expected
by the previous patch.

To test:
1. Apply this bug patches up to 'Add x-koha-override options...'
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/suggestions.t
=> FAIL: Tests fail! The controller doesn't care about overrides or
         sysprefs about suggestions limits.
3. Apply this patch
4. Repeat 2
=> SUCCESS: Things work!
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>

Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Comment 7 Martin Renvoize (ashimema) 2022-05-04 12:10:10 UTC
Created attachment 134604 [details] [review]
Bug 30663: Add Koha::Suggestions helper methods

This patch adds the following helper methods:

* filter_by_pending
* filter_by_suggested_days_range

This methods follow basically what's done in opac-suggestions.pl
I chose 'pending' as opposed to 'open' to follow what we use in the UI
which might be the case because of being more accurate for end users.

To test:
1. Apply this patch
2. Run:
   $ kshell
  k$ prove t/db_dependent/Koha/Suggestions.t
=> SUCCESS: Tests pass!
3. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 8 Martin Renvoize (ashimema) 2022-05-04 12:10:14 UTC
Created attachment 134605 [details] [review]
Bug 30663: Add x-koha-override options to /suggestions

This patch adds the x-koha-override header parameter to the route that
is used to create suggestions, POST /suggestions.

The idea is that adding suggestions will be rejected under certain
conditions unless x-koha-override is passed with appropriate values. The
added overrides are:

* any
* max_total
* max_pending

Tests are added for the expected behavior.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 9 Martin Renvoize (ashimema) 2022-05-04 12:10:18 UTC
Created attachment 134606 [details] [review]
Bug 30663: Implement overrides handling in POST /suggestions

This patch implements the override checks in the controller as expected
by the previous patch.

To test:
1. Apply this bug patches up to 'Add x-koha-override options...'
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/suggestions.t
=> FAIL: Tests fail! The controller doesn't care about overrides or
         sysprefs about suggestions limits.
3. Apply this patch
4. Repeat 2
=> SUCCESS: Things work!
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 10 Martin Renvoize (ashimema) 2022-05-04 12:11:33 UTC
Great work.. as a follow-up bug I'd love to see us move these checks and override into the Object classes.. but given this is a bug and there's already work underway in those classes I think this is an acceptable middle ground.

Passing QA
Comment 11 Tomás Cohen Arazi (tcohen) 2022-05-05 13:33:27 UTC
Pushed for 22.05. Thanks everyone.
Comment 12 Lucas Gass (lukeg) 2022-05-09 19:40:25 UTC
It would be very helpful if this could be backported to 21.11, thanks!
Comment 13 Kyle M Hall (khall) 2022-05-13 17:40:54 UTC
Doesn't apply to 21.11.x, please rebase if needed!
Comment 14 Tomás Cohen Arazi (tcohen) 2022-05-13 18:58:32 UTC
Created attachment 134987 [details] [review]
[21.11.x] Bug 30663: Add x-koha-override options to /suggestions

This patch adds the x-koha-override header parameter to the route that
is used to create suggestions, POST /suggestions.

The idea is that adding suggestions will be rejected under certain
conditions unless x-koha-override is passed with appropriate values. The
added overrides are:

* any
* max_total
* max_pending

Tests are added for the expected behavior.

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 15 Tomás Cohen Arazi (tcohen) 2022-05-13 18:58:37 UTC
Created attachment 134988 [details] [review]
[21.11.x] Bug 30663: Implement overrides handling in POST /suggestions

This patch implements the override checks in the controller as expected
by the previous patch.

To test:
1. Apply this bug patches up to 'Add x-koha-override options...'
2. Run:
   $ kshell
  k$ prove t/db_dependent/api/v1/suggestions.t
=> FAIL: Tests fail! The controller doesn't care about overrides or
         sysprefs about suggestions limits.
3. Apply this patch
4. Repeat 2
=> SUCCESS: Things work!
5. Sign off :-D

Signed-off-by: Tomas Cohen Arazi <tomascohen@theke.io>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@ptfs-europe.com>
Comment 16 Kyle M Hall (khall) 2022-05-13 19:17:21 UTC
Pushed to 21.11.x for 21.11.06
Comment 17 Victor Grousset/tuxayo 2022-06-25 02:52:22 UTC
Backported: Pushed to 21.05.x branch for 21.05.16