LP#1673870: fix ebook checkout workflow based on OneClickdigital testing user/jeffdavis/lp1673870-ebook-transactions
authorJeff Davis <jdavis@sitka.bclibraries.ca>
Wed, 16 Aug 2017 18:31:52 +0000 (11:31 -0700)
committerJeff Davis <jdavis@sitka.bclibraries.ca>
Wed, 16 Aug 2017 18:31:52 +0000 (11:31 -0700)
Signed-off-by: Jeff Davis <jdavis@sitka.bclibraries.ca>
Open-ILS/web/js/ui/default/opac/ebook_api/loggedin.js

index f003b66..90d68a7 100644 (file)
@@ -196,6 +196,8 @@ function updateHoldView() {
 
 // set up page for user to perform a checkout
 function getReadyForCheckout() {
+    if (typeof ebook_action.type === 'undefined')
+        return;
     if (typeof active_ebook === 'undefined') {
         console.log('No active ebook specified, cannot prepare for checkout');
         dojo.removeClass('ebook_checkout_failed', "hidden");
@@ -222,6 +224,8 @@ function getReadyForCheckout() {
 
 // set up page for user to place a hold
 function getReadyForHold() {
+    if (typeof ebook_action.type === 'undefined')
+        return;
     if (typeof active_ebook === 'undefined') {
         console.log('No active ebook specified, cannot prepare for hold');
         dojo.removeClass('ebook_hold_failed', "hidden");
@@ -256,40 +260,73 @@ function cleanupAfterAction() {
 
 // check out our active ebook
 function doCheckout() {
+    var ses = dojo.cookie(active_ebook.vendor); // required when inspecting checkouts for download_url
     active_ebook.checkout(authtoken, patron_id, function(resp) {
         if (resp.error_msg) {
             console.log('Checkout failed: ' + resp.error_msg);
             dojo.removeClass('ebook_checkout_failed', "hidden");
-        } else {
-            console.log('Checkout succeeded!');
-            dojo.destroy('checkout-button');
-            dojo.destroy('checkout-format'); // remove optional format selector
-            dojo.removeClass('ebook_checkout_succeeded', "hidden");
-            // add our successful checkout to top of transaction cache
-            var new_xact = {
-                title_id: active_ebook.id,
-                title: active_ebook.title,
-                author: active_ebook.author,
-                due_date: resp.due_date
-            };
-            if (resp.download_url) {
-                new_xact.download_url = resp.download_url;
+            return;
+        }
+        console.log('Checkout succeeded!');
+        dojo.destroy('checkout-button');
+        dojo.destroy('checkout-format'); // remove optional format selector
+        dojo.removeClass('ebook_checkout_succeeded', "hidden");
+        // add our successful checkout to top of transaction cache
+        var new_xact = {
+            title_id: active_ebook.id,
+            title: active_ebook.title,
+            author: active_ebook.author,
+            due_date: resp.due_date,
+            finish: function() {
+                console.log('new_xact.finish()');
+                xacts.checkouts.unshift(this);
+                cleanupAfterAction();
+                // When we switch to jQuery, we can use .one() instead of .on(),
+                // obviating the need for an explicit disconnect here.
+                dojo.disconnect(active_ebook.conns.checkout);
             }
-            if (typeof resp.formats !== 'undefined') {
-                new_xact.ebook = new Ebook(active_ebook.vendor, active_ebook.title_id);
-                var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));
-                for (f in resp.formats) {
-                    dojo.create("option", { value: resp.formats[f], innerHTML: f }, select);
-                }
-                var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dojo.byId('checkout-button-td'));
-                new_xact.ebook.conns.download = dojo.connect(button, 'onclick', new_xact.ebook, "download");
+        };
+        if (resp.download_url) {
+            // Use download URL from checkout response, if available.
+            new_xact.download_url = resp.download_url;
+            dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
+            new_xact.finish();
+        } else if (typeof resp.formats !== 'undefined') {
+            // User must select download format from list of options.
+            new_xact.ebook = new Ebook(active_ebook.vendor, active_ebook.title_id);
+            var select = dojo.create("select", { id: "download-format" }, dojo.byId('checkout-button-td'));
+            for (f in resp.formats) {
+                dojo.create("option", { value: resp.formats[f], innerHTML: f }, select);
             }
-            xacts.checkouts.unshift(new_xact);
-            cleanupAfterAction();
+            var button = dojo.create("input", { id: "download-button", type: "button", value: l_strings.download }, dojo.byId('checkout-button-td'));
+            new_xact.ebook.conns.download = dojo.connect(button, 'onclick', new_xact.ebook, "download");
+            new_xact.finish();
+        } else if (typeof resp.xact_id !== 'undefined') {
+            // No download URL provided by API checkout response.  Grab fresh
+            // list of user checkouts from API, find the just-completed
+            // checkout by transaction ID, and get the download URL from that.
+            // We call the OpenSRF method directly because Relation.getCheckouts()
+            // results in scoping issues when retrieving the vendor session cookie.
+            new_xact.xact_id = resp.xact_id;
+            new OpenSRF.ClientSession('open-ils.ebook_api').request({
+                method: 'open-ils.ebook_api.patron.get_checkouts',
+                params: [ authtoken, ses, patron_id ],
+                async: false,
+                oncomplete: function(r) {
+                    var resp = r.recv();
+                    if (resp) {
+                        dojo.forEach(resp.content(), function(x) {
+                            if (x.xact_id === new_xact.xact_id) {
+                                new_xact.download_url = x.download_url;
+                                dojo.create("a", { href: new_xact.download_url, innerHTML: l_strings.download }, dojo.byId('checkout-button-td'));
+                                return;
+                            }
+                        });
+                        new_xact.finish();
+                    }
+                }
+            }).send();
         }
-        // When we switch to jQuery, we can use .one() instead of .on(),
-        // obviating the need for an explicit disconnect here.
-        dojo.disconnect(active_ebook.conns.checkout);
     });
 }