From 11fe8a2a3e14abc0abb03bf78b6b40637734e8e2 Mon Sep 17 00:00:00 2001 From: Jaswinder Singh Date: Fri, 12 Oct 2018 17:25:04 -0400 Subject: [PATCH] LP#1772680: Fixed issues related to caching, page refresh, and add libraryCard number to register api call Signed-off-by: Jaswinder Singh Signed-off-by: Galen Charlton --- .../perlmods/lib/OpenILS/Application/EbookAPI.pm | 63 ++++++++++++++++++---- .../lib/OpenILS/Application/EbookAPI/RBDigital.pm | 9 ++-- .../src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm | 13 +++-- .../lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm | 4 +- .../web/js/ui/default/opac/ebook_api/rbdigital.js | 7 +++ .../web/js/ui/default/opac/ebook_api/session.js | 16 ++---- 6 files changed, 77 insertions(+), 35 deletions(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm index 10f8389c04..f2632ef09d 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI.pm @@ -102,6 +102,31 @@ sub update_cache { } } +# This method will delete cache key +sub delete_cache { + my $session_id = shift; + my $cache_key = shift; + my $new_cache = {}; + + try { + $logger->info("EbookAPI: delete_cache cache for key:$cache_key and session_id: $session_id "); + my $cached_session = retrieve_session($session_id); + if ($cached_session) { + $logger->debug("EbookAPI: delete_cache: Retrieving Cached Session for session_id: ".$session_id); + # re-authorize cached session, if applicable + my $cache_obj = new_handler($session_id, $cached_session); + + $logger->debug("EbookAPI: delete_cache cache before removing key: " . Dumper($cache_obj)); + delete $cache_obj->{$cache_key}; + $cache->delete_cache($session_id); + $cache->put_cache($session_id, $cache_obj, $cache_timeout); + $logger->debug("EbookAPI: delete_cache cache after removing key: " . Dumper($cache_obj)); + } + } catch Error with { + $logger->error("EbookAPI: error removing cache for key: ".$cache_key); + }; +} + sub retrieve_session { my $session_id = shift; unless ($session_id) { @@ -127,7 +152,7 @@ sub new_handler { return; } my $module = ref($ses); - $logger->info("EbookAPI: starting new $module handler from cached session $session_id..."); + $logger->debug("EbookAPI: starting new $module handler from cached session $session_id..."); $module->use; my $handler = $module->new($ses); return $handler; @@ -139,16 +164,16 @@ sub logout_session { my $session_id = shift; my $vendor = shift; my $result = 0; - $logger->info("EbookAPI: logout_session: Clearing Session ($vendor) for session_id: $session_id"); + $logger->debug("EbookAPI: logout_session: Clearing Session ($vendor) for session_id: $session_id"); try { # fail silently if there's no pre-existing cache to delete $cache->delete_cache($session_id); $result = 1; } catch Error with { - $logger->info("EbookAPI: logout_session: No existing session found ($vendor) for session_id: $session_id"); + $logger->error("EbookAPI: logout_session: No existing session found ($vendor) for session_id: $session_id"); }; - $logger->info("EbookAPI: logout_session: Cache is cleared ($vendor) for session_id: $session_id"); + $logger->debug("EbookAPI: logout_session: Cache is cleared ($vendor) for session_id: $session_id"); my $cached_session = retrieve_session($session_id); retrun $result; @@ -187,7 +212,7 @@ sub check_session { my $vendor = shift; my $ou = shift; - $logger->info("EbookAPI: check_session: Checking Session ($vendor) for session_id (if any): $session_id"); + $logger->debug("EbookAPI: check_session: Checking Session ($vendor) for session_id (if any): $session_id"); return start_session($self, $conn, $vendor, $ou) unless $session_id; # Check if the session requested belongs to the same vendor @@ -197,7 +222,7 @@ sub check_session { my $cached_session = retrieve_session($session_id); if ($cached_session) { - $logger->info("EbookAPI: check_session: Retrieving Cached Session ($vendor) for session_id: ".$session_id); + $logger->debug("EbookAPI: check_session: Retrieving Cached Session ($vendor) for session_id: ".$session_id); # re-authorize cached session, if applicable my $handler = new_handler($session_id, $cached_session); $handler->do_client_auth(); @@ -256,7 +281,7 @@ sub _start_session { # TODO handle API versions? if ($vendor_handlers{$vendor}) { $module = $vendor_handlers{$vendor}; - $logger->info("EbookAPI: _start_session: Module '$module' "); + $logger->debug("EbookAPI: _start_session: Module '$module' "); } else { $logger->error("EbookAPI: No handler module found for $vendor!"); return; @@ -281,7 +306,7 @@ sub _start_session { # our "session" is actually just our handler object, serialized and cached $cache->put_cache($handler->{session_id}, $handler, $cache_timeout); - $logger->info("EbookAPI: _start_session: New session created for '$vendor' : " .$handler->{session_id}); + $logger->debug("EbookAPI: _start_session: New session created for '$vendor' : " .$handler->{session_id}); return $handler->{session_id}; } @@ -615,6 +640,12 @@ sub do_xact { $res = $handler->$action($title_id, $user_token); } + if ($action eq 'checkout' || $action eq 'checkin') { + delete_cache($session_id,'checkouts'); + } elsif ($action eq 'place_hold' || $action eq 'cancel_hold') { + delete_cache($session_id,'holds'); + } + if (defined ($res)) { return $res; } else { @@ -1028,7 +1059,7 @@ sub get_patron_id { my ($self, $conn, $auth, $session_id, $barcode) = @_; my $patron_id = 0; - $logger->info("EbookAPI: getting patron id for patron $barcode"); + $logger->info("EbookAPI: getting patron id for patron $barcode "); # verify that user is authenticated in EG my $editor = new_editor(authtoken => $auth); @@ -1111,7 +1142,17 @@ sub register_patron { $logger->info("EbookAPI: User Authenticated. Now, registering the patron with RBDigital"); my $handler = new_handler($session_id); - return $handler->register_patron($editor->requestor->usrname, $password); + #Get user library card number/barcode from DB + my $user_barcode = ""; + if ($editor->requestor->card) { + my $user_library_card = $editor->retrieve_actor_card($editor->requestor->card); + + if ($user_library_card && $user_library_card->barcode) { + $user_barcode = $user_library_card->barcode; + } + } + + return $handler->register_patron($editor->requestor->usrname, $password, $user_barcode); } __PACKAGE__->register_method( @@ -1468,8 +1509,10 @@ sub do_wishlist { if ($action eq 'add') { $logger->debug("EbookAPI: Calling add_to_wishlist($patron_id, $isbn)"); + delete_cache($session_id,$cache_key); return $handler->add_to_wishlist($patron_id, $isbn); } elsif ($action eq 'delete') { + delete_cache($session_id,$cache_key); return $handler->delete_from_wishlist($patron_id, $isbn); } elsif ($action eq 'get') { # Locate wishlist from cache first diff --git a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/RBDigital.pm b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/RBDigital.pm index 1afadb19bc..69df7e1f73 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/RBDigital.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/Application/EbookAPI/RBDigital.pm @@ -339,10 +339,11 @@ sub _build_search_URI { # "email": "jsmith@yahoo.com", # "postalCode": "30904", # "isActive": false, -# "libraryId": {libraryId} +# "libraryId": {libraryId}, +# "libraryCard": {libraryCard} # } sub register_patron { - my ($self, $username, $password) = @_; + my ($self, $username, $password, $user_barcode) = @_; my $base_uri = $self->{base_uri}; my $library_id = $self->{library_id}; my $session_id = $self->{session_id}; @@ -382,6 +383,7 @@ sub register_patron { return 0; } + $logger->info("EbookAPI: Building a request for patron registration"); my $request_content = { firstName => $user_req->first_given_name, @@ -391,7 +393,8 @@ sub register_patron { email => $user_req->email, postalCode => $postal_code, isActive => $is_active, - libraryId => $library_id + libraryId => $library_id, + libraryCard => $user_barcode }; my $req = { diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm index 0594378722..62aca2ebc6 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader.pm @@ -468,24 +468,23 @@ sub load_rbdigital_common { # Load vendor tabs for authenticated patrons only if (defined($self->editor->requestor)) { - $logger->info("EbookAPI: load_rbdigital_common: Token: ". $self->ctx->{authtoken}); my $session_id = ""; - + # Get cached session based on authtoken that contains the ebook session id my $cached_session = $cache->get_cache($self->ctx->{authtoken}) || undef; - $logger->info("EbookAPI: load_rbdigital_common: Cached session result: ".Dumper($cached_session)); + $logger->debug("EbookAPI: load_rbdigital_common: Cached session retrieved: ".Dumper($cached_session)); if ($cached_session) { $session_id = $cached_session; - $logger->info("EbookAPI: load_rbdigital_common: Retrieved cached session_id: $session_id"); + $logger->debug("EbookAPI: load_rbdigital_common: Retrieved cached session_id: $session_id"); } else { $session_id = _get_ebook_session_id($session_id, $self->editor->requestor->home_ou); - $logger->info("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id"); + $logger->debug("EbookAPI: load_rbdigital_common: Retrieved new session: $session_id"); # Save session id into cache $cache->put_cache($self->ctx->{authtoken}, $session_id, $cache_timeout); } + # Assign session id to global variable so it can be saved to cookie as well. $ebook_rbdigital_session_id = $session_id; - $logger->info("EbookAPI: load_rbdigital_common: after checking session id: $session_id"); $self->ctx->{rbdigital_session_id} = $session_id; $self->ctx->{vendors} = []; $self->ctx->{wishlist} = []; @@ -499,7 +498,7 @@ sub load_rbdigital_common { # Call method to know if the user's home library is tied to RBdigital portal my $is_patron_authorized_to_register_with_rbdigital = _is_authorized($self->editor->authtoken, $session_id); $self->ctx->{is_patron_authorized_to_register_with_rbdigital} = $is_patron_authorized_to_register_with_rbdigital; - $logger->info('EbookAPI: is_patron_authorized_to_register_with_rbdigital: '.$is_patron_authorized_to_register_with_rbdigital); + $logger->debug('EbookAPI: is_patron_authorized_to_register_with_rbdigital: '.$is_patron_authorized_to_register_with_rbdigital); # Call sub to get all the vendors to be displayed on the UI $self->ctx->{vendors} = _get_authorized_vendors($self->editor->authtoken, $session_id); diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm index 0d3e7e488f..f4f05c4bcd 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/EGCatLoader/RBDigitalSearch.pm @@ -409,9 +409,9 @@ sub _get_default_vendor { sub _get_patron_id { my ($authtoken, $session_id) = @_; - $logger->info('EbookAPI: RBDigitalRecord _get_patron_id'); + $logger->info('EbookAPI: RBDigitalSearch _get_patron_id session_id: '.$session_id); my $ebook_api = OpenSRF::AppSession->create('open-ils.ebook_api'); - my $api_request = $ebook_api->request('open-ils.ebook_api.get_patron_id', $authtoken, $session_id,0); + my $api_request = $ebook_api->request('open-ils.ebook_api.get_patron_id', $authtoken, $session_id, 0); if (my $resp = $api_request->recv) { return $resp->content; diff --git a/Open-ILS/web/js/ui/default/opac/ebook_api/rbdigital.js b/Open-ILS/web/js/ui/default/opac/ebook_api/rbdigital.js index e685940fb5..2cd8498826 100644 --- a/Open-ILS/web/js/ui/default/opac/ebook_api/rbdigital.js +++ b/Open-ILS/web/js/ui/default/opac/ebook_api/rbdigital.js @@ -202,6 +202,9 @@ function setupEventHandlers() { ebook_rbdigital.id = isbn; showEbookSpinner($(this), true); ebook_rbdigital.placeHold(authtoken, rbdigitalPatronId, placeHoldCallback); + //Refresh a page + location.reload(); + }); //Cancel Hold @@ -210,6 +213,8 @@ function setupEventHandlers() { ebook_rbdigital.id = isbn; showEbookSpinner($(this), true); ebook_rbdigital.cancelHold(authtoken, rbdigitalPatronId, cancelHoldCallback); + //Refresh a page + location.reload(); }); //Return an e-item @@ -218,6 +223,8 @@ function setupEventHandlers() { ebook_rbdigital.id = isbn; showEbookSpinner($(this), true); ebook_rbdigital.checkin(authtoken, rbdigitalPatronId, checkinCallback); + //Refresh a page + location.reload(); }); //Download an ebook/eaudio diff --git a/Open-ILS/web/js/ui/default/opac/ebook_api/session.js b/Open-ILS/web/js/ui/default/opac/ebook_api/session.js index 3fac7d8f4d..f3b4496654 100755 --- a/Open-ILS/web/js/ui/default/opac/ebook_api/session.js +++ b/Open-ILS/web/js/ui/default/opac/ebook_api/session.js @@ -24,10 +24,10 @@ function startSession(vendor, callback) { // validate or initialize API session // (check_session method will fallback to start_session if no session ID is provided) function checkSession(vendor, callback) { - var ses = dojo.cookie(vendor) || null; + var ses = ebook_sessions.rbdigital_session_id || null; //Check session inside js variable defined in base_js.tt2 if (ses == null) { - ses = ebook_sessions.rbdigital_session_id; + ses = dojo.cookie(vendor); //Check if session is empty here as well if (ses == null) { return startSession(vendor,callback); @@ -53,14 +53,4 @@ function checkSession(vendor, callback) { } } }).send(); -} - - -/* -function checkSession(vendor, callback) { - - var new_session = vendor + "_" + authtoken; - - return callback(vendor,new_session); -} -*/ \ No newline at end of file +} \ No newline at end of file -- 2.11.0