From 2efd93f58bee719e8d8f85a25dd45b2b6e552a6a Mon Sep 17 00:00:00 2001 From: Jeff Davis Date: Wed, 28 Jun 2017 13:36:24 -0700 Subject: [PATCH] LP#1673870: Support placing and canceling OverDrive holds Signed-off-by: Jeff Davis Signed-off-by: Galen Charlton Signed-off-by: Bill Erickson --- .../perlmods/lib/OpenILS/Application/EbookAPI.pm | 10 +++- .../lib/OpenILS/Application/EbookAPI/OverDrive.pm | 53 ++++++++++++++++++++++ .../lib/OpenILS/Application/EbookAPI/Test.pm | 3 ++ .../src/templates/opac/parts/ebook_api/base_js.tt2 | 5 ++ Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js | 2 +- 5 files changed, 70 insertions(+), 3 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm index e09282eba9..d6961a8a01 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm @@ -481,7 +481,7 @@ __PACKAGE__->register_method( # - barcode: patron barcode # sub do_xact { - my ($self, $conn, $auth, $session_id, $title_id, $barcode) = @_; + my ($self, $conn, $auth, $session_id, $title_id, $barcode, $email) = @_; my $action; if ($self->api_name =~ /checkout/) { @@ -508,7 +508,13 @@ sub do_xact { my $user_token = $handler->do_patron_auth($barcode); # handler method constructs and submits request (and handles any external authentication) - my $res = $handler->$action($title_id, $user_token); + my $res; + # place_hold has email as optional additional param + if ($action eq 'place_hold') { + $res = $handler->place_hold($title_id, $user_token, $email); + } else { + $res = $handler->$action($title_id, $user_token); + } if (defined ($res)) { return $res; } else { diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm index 613459459c..9549fa49d9 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/OverDrive.pm @@ -555,6 +555,59 @@ sub checkin { return; } +sub place_hold { + my ($self, $title_id, $patron_token, $email) = @_; + my $fields = [ + { + name => 'reserveId', + value => $title_id + } + ]; + if ($email) { + push @$fields, { name => 'emailAddress', value => $email }; + # TODO: Use autoCheckout=true when we have a patron email? + } else { + push @$fields, { name => 'ignoreEmail', value => 'true' }; + } + my $request_content = { fields => $fields }; + my $req = { + method => 'POST', + uri => $self->{circulation_base_uri} . "/patrons/me/holds", + content => OpenSRF::Utils::JSON->perl2JSON($request_content) + }; + if (my $res = $self->handle_http_request($req, $self->{session_id})) { + if ($res->{content}->{holdPlacedDate}) { + return { + queue_position => $res->{content}->{holdListPosition}, + queue_size => $res->{content}->{numberOfHolds}, + expire_date => (defined $res->{content}->{holdExpires}) ? $res->{content}->{holdExpires} : undef + }; + } + $logger->error("EbookAPI: place hold failed for OverDrive title $title_id"); + return { error_msg => "Could not place hold." }; + } + $logger->error("EbookAPI: no response received from OverDrive server"); + return; +} + +sub cancel_hold { + my ($self, $title_id, $patron_token) = @_; + my $req = { + method => 'DELETE', + uri => $self->{circulation_base_uri} . "/patrons/me/holds/$title_id" + }; + if (my $res = $self->handle_http_request($req, $self->{session_id})) { + if ($res->{status} =~ /^204/) { + return {}; + } else { + $logger->error("EbookAPI: cancel hold failed for OverDrive title $title_id"); + return { error_msg => ( (defined $res->{content}) ? $res->{content} : 'Could not cancel hold' ) }; + } + } + $logger->error("EbookAPI: no response received from OverDrive server"); + return; +} + # List of patron checkouts: # GET http://patron.api.overdrive.com/v1/patrons/me/checkouts # User-Agent: {Your application} diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm index a6f3e3ffb8..adab52c094 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/Test.pm @@ -368,6 +368,9 @@ sub place_hold { # Patron ID or patron auth token, as returned by do_patron_auth(). my $user_token = shift; + # Email address of patron (optional, not used here). + my $email = shift; + # If hold is successfully placed, return a hashref with the following # fields: # - queue_position: this user's position in hold queue for this title diff --git a/Open-ILS/src/templates/opac/parts/ebook_api/base_js.tt2 b/Open-ILS/src/templates/opac/parts/ebook_api/base_js.tt2 index 9d8b06fd4e..30ee0f52c6 100644 --- a/Open-ILS/src/templates/opac/parts/ebook_api/base_js.tt2 +++ b/Open-ILS/src/templates/opac/parts/ebook_api/base_js.tt2 @@ -58,6 +58,11 @@ ebook_action.vendor = '[% CGI.param("vendor") %]'; // user- or login-specific vars var authtoken = '[% ctx.authtoken %]'; var patron_id = '[% ctx.active_card %]'; // using barcode of active card as patron ID +[%- IF ctx.user.email %] +var patron_email = '[% ctx.user.email | html %]'; +[%- ELSE %] +var patron_email = null; +[%- END %] var myopac_page; [% IF myopac_page %] diff --git a/Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js b/Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js index f074926f15..5119507da9 100644 --- a/Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js +++ b/Open-ILS/web/js/ui/default/opac/ebook_api/ebook.js @@ -91,7 +91,7 @@ Ebook.prototype.placeHold = function(authtoken, patron_id, callback) { var ebook = this; new OpenSRF.ClientSession('open-ils.ebook_api').request({ method: 'open-ils.ebook_api.place_hold', - params: [ authtoken, ses, ebook.id, patron_id ], + params: [ authtoken, ses, ebook.id, patron_id, patron_email ], async: true, oncomplete: function(r) { var resp = r.recv(); -- 2.11.0