Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

loan: update request pickup location #935

Merged
merged 1 commit into from
Apr 28, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions rero_ils/modules/items/api_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -164,9 +164,29 @@ def automatic_checkin(item, data):
@jsonify_action
def cancel_loan(item, params):
"""HTTP request for cancel action."""
# TODO: manage transitions for complex cases
return item.cancel_loan(**params)


@api_blueprint.route("/update_loan_pickup_location", methods=['POST'])
@check_authentication
def update_loan_pickup_location():
"""HTTP request for update loan pickup location."""
# for now request pickup location update allowed for pending requests only
# TODO: manage case 'at desk' and 'in transit' (needs PO feedback)
data = flask_request.get_json()
loan_pid = data.get('loan_pid')
pickup_location_pid = data.get('pickup_location_pid')
if not loan_pid or not pickup_location_pid:
return jsonify({'status': 'error: Bad request'}), 400
loan = Loan.get_record_by_pid(loan_pid)
loan['pickup_location_pid'] = pickup_location_pid
if not loan.get('state') == 'PENDING':
return jsonify({'status': 'error: Forbidden'}), 403
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be a bad request not a forbidden

Copy link
Contributor Author

@AoNoOokami AoNoOokami Apr 27, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If loan state is not pending, updating pickup location is not allowed. That's why I use a 403 error message as response, because I check that the state chart is followed. Later, more complex cases will be managed and this part of the function will be modified.
https://en.wikipedia.org/wiki/List_of_HTTP_status_codes#4xx_Client_errors

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If loan state is not pending, updating pickup location is not allowed. That's why I use a 403 error message as response, because I check that the state chart is followed. Later, more complex cases will be managed and this part of the function will be modified.

You are right.

new_loan = loan.update(loan, dbcommit=True, reindex=True)
return new_loan


@api_blueprint.route("/lose", methods=['POST'])
@check_authentication
@jsonify_action
Expand Down
80 changes: 80 additions & 0 deletions tests/api/test_items_rest_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -670,3 +670,83 @@ def test_checkout_cancel_old_loan(
# loan will be canclled if a librarian decided to checkout the item anyway.
item, actions = item_lib_fully.prior_checkout_actions(action_params)
assert 'cancel' in actions


def test_update_loan_pickup_location(
client, librarian_martigny_no_email,
patron_martigny_no_email, loc_public_martigny, loc_public_saxon,
item3_lib_martigny, circulation_policies):
"""Test loan pickup location change."""
login_user_via_session(client, librarian_martigny_no_email.user)
item_pid = item3_lib_martigny.pid
first_loc_pid = loc_public_saxon.pid
new_loc_pid = loc_public_martigny.pid
# request an item by a librarian
res, req_data = postdata(
client,
'api_item.librarian_request',
dict(
item_pid=item_pid,
pickup_location_pid=first_loc_pid,
patron_pid=patron_martigny_no_email.pid
)
)
assert res.status_code == 200
# Update pickup location of the request, no loan pid
loan_pid = req_data.get('action_applied')[LoanAction.REQUEST].get('pid')
res, data = postdata(
client,
'api_item.update_loan_pickup_location',
dict(
item_pid=item_pid,
pickup_location_pid=new_loc_pid
)
)
assert res.status_code == 400
# Update pickup location of the request, no pickup location pid
res, data = postdata(
client,
'api_item.update_loan_pickup_location',
dict(
item_pid=item_pid,
loan_pid=loan_pid
)
)
assert res.status_code == 400
# Update pickup location of the request
res, data = postdata(
client,
'api_item.update_loan_pickup_location',
dict(
item_pid=item_pid,
pickup_location_pid=new_loc_pid,
loan_pid=loan_pid
)
)
assert res.status_code == 200
assert data.get('pickup_location_pid') == new_loc_pid
# Change loan state to 'ITEM_AT_DESK'
loans = Item.get_loans_by_item_pid(item_pid)
for loan in loans:
if loan.get('state') == 'ITEM_ON_LOAN':
res, _ = postdata(
client,
'api_item.checkin',
dict(
item_pid=item_pid,
pid=loan.get('pid')
),
)
assert res.status_code == 200
# Update pickup location of the request:
# loan state different from 'pending'
res, data = postdata(
client,
'api_item.update_loan_pickup_location',
dict(
item_pid=item_pid,
pickup_location_pid=new_loc_pid,
loan_pid=loan_pid
)
)
assert res.status_code == 403