From 91a5e68e3fcf4ce8965c4d63dfd171f92379dc1c Mon Sep 17 00:00:00 2001
From: Tomas Cohen Arazi <tomascohen@theke.io>
Date: Thu, 13 May 2021 17:37:44 -0300
Subject: [PATCH] Bug 28377: Use the API to suspend/resume holds

This patch makes the patron page (detail and circulation) use the API to
suspend/resume holds on the holds tab.

It previously used the old svc/ scripts we plan to replace.

To test
1. Have a patron with some holds
2. Play with suspending/resuming holds. Include the indefinite
   suspension.
=> SUCCESS: Everything works as usual
3. Apply this patch
4. Repeat 2
=> SUCCESS: Nothing changed, a soft breeze surprises you
5. Sign off :-D

Signed-off-by: David Nind <david@davidnind.com>
---
 .../prog/en/modules/circ/circulation.tt       |  1 +
 .../prog/en/modules/members/moremember.tt     |  1 +
 koha-tmpl/intranet-tmpl/prog/js/holds.js      | 73 ++++++++++++-------
 3 files changed, 48 insertions(+), 27 deletions(-)

diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
index 6e7aa020de..c399336278 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/circ/circulation.tt
@@ -969,6 +969,7 @@
     [% INCLUDE 'timepicker.inc' %]
     [% INCLUDE 'select2.inc' %]
     [% Asset.js("lib/jquery/plugins/rowGroup/dataTables.rowGroup.min.js") | $raw %]
+    [% INCLUDE 'js-date-format.inc' %]
     <script>
         /* Set some variable needed in circulation.js */
         var logged_in_user_borrowernumber = "[% logged_in_user.borrowernumber | html %]";
diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
index 39024d6ffa..3237e9543d 100644
--- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
+++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/moremember.tt
@@ -859,6 +859,7 @@
     [% Asset.js("lib/jquery/plugins/jquery-ui-timepicker-addon.min.js") | $raw %]
     [% INCLUDE 'timepicker.inc' %]
     [% INCLUDE 'select2.inc' %]
+    [% INCLUDE 'js-date-format.inc' %]
     <script>
         /* Set some variable needed in circulation.js */
         var logged_in_user_borrowernumber = "[% logged_in_user.borrowernumber | html %]";
diff --git a/koha-tmpl/intranet-tmpl/prog/js/holds.js b/koha-tmpl/intranet-tmpl/prog/js/holds.js
index f068dbd80e..b844e0f33c 100644
--- a/koha-tmpl/intranet-tmpl/prog/js/holds.js
+++ b/koha-tmpl/intranet-tmpl/prog/js/holds.js
@@ -1,5 +1,23 @@
 /* global __ dataTablesDefaults borrowernumber SuspendHoldsIntranet */
 $(document).ready(function() {
+
+    function suspend_hold(hold_id, end_date) {
+        return $.ajax({
+            method: 'POST',
+            url: '/api/v1/holds/'+encodeURIComponent(hold_id)+'/suspension',
+            data: function () {
+                if ( end_date !== null ) return JSON.stringify({ "end_date": end_date })
+            }
+        });
+    }
+
+    function resume_hold(hold_id) {
+        return $.ajax({
+            method: 'DELETE',
+            url: '/api/v1/holds/'+encodeURIComponent(hold_id)+'/suspension'
+        });
+    }
+
     var holdsTable;
 
     // Don't load holds table unless it is clicked on
@@ -131,10 +149,10 @@ $(document).ready(function() {
                             if ( oObj.found ) {
                                 return "";
                             } else if ( oObj.suspend == 1 ) {
-                                return "<a class='hold-resume btn btn-default btn-xs' id='resume" + oObj.reserve_id + "'>"
+                                return "<a class='hold-resume btn btn-default btn-xs' data-hold-id='" + oObj.reserve_id + "'>"
                                      +"<i class='fa fa-play'></i> " + __("Resume") + "</a>";
                             } else {
-                                return "<a class='hold-suspend btn btn-default btn-xs' id='suspend" + oObj.reserve_id + "'>"
+                                return "<a class='hold-suspend btn btn-default btn-xs' data-hold-id='" + oObj.reserve_id + "' data-hold-title='"+ oObj.title +"'>"
                                      +"<i class='fa fa-pause'></i> " + __("Suspend") + "</a>";
                             }
                         }
@@ -197,25 +215,24 @@ $(document).ready(function() {
 
             $('#holds-table').on( 'draw.dt', function () {
                 $(".hold-suspend").on( "click", function() {
-                    var id = $(this).attr("id").replace("suspend", "");
-                    var hold = holds[id];
-                    $("#suspend-modal-title").html( hold.title );
-                    $("#suspend-modal-reserve_id").val( hold.reserve_id );
+                    var hold_id    = $(this).data('hold-id');
+                    var hold_title = $(this).data('hold-title');
+                    $("#suspend-modal-title").html( hold_title );
+                    $("#suspend-modal-submit").data( 'hold-id', hold_id );
                     $('#suspend-modal').modal('show');
                 });
 
-                $(".hold-resume").on( "click", function() {
-                    var id = $(this).attr("id").replace("resume", "");
-                    var hold = holds[id];
-                    $.post('/cgi-bin/koha/svc/hold/resume', { "reserve_id": hold.reserve_id }, function( data ){
-                      if ( data.success ) {
-                          holdsTable.api().ajax.reload();
-                      } else {
-                        if ( data.error == "HOLD_NOT_FOUND" ) {
-                            alert( __("Unable to resume, hold not found") );
+                $(".hold-resume").on("click", function () {
+                    var hold_id = $(this).data('hold-id');
+                    resume_hold(
+                        hold_id
+                    ).success(function () {
+                        holdsTable.api().ajax.reload();
+                    }).error(function (jqXHR, textStatus, errorThrown) {
+                        if (jqXHR.status === 404) {
+                            alert(__("Unable to resume, hold not found"));
                             holdsTable.api().ajax.reload();
                         }
-                      }
                     });
                 });
 
@@ -340,19 +357,21 @@ $(document).ready(function() {
 
     $("#suspend-modal-submit").on( "click", function( e ) {
         e.preventDefault();
-        $.post('/cgi-bin/koha/svc/hold/suspend', $('#suspend-modal-form').serialize(), function( data ){
-          $('#suspend-modal').modal('hide');
-          if ( data.success ) {
-              holdsTable.api().ajax.reload();
-          } else {
-            if ( data.error == "INVALID_DATE" ) {
-                alert( __("Unable to suspend hold, invalid date") );
-            }
-            else if ( data.error == "HOLD_NOT_FOUND" ) {
-                alert( __("Unable to suspend hold, hold not found") );
+        var suspend_until_date = $("#suspend-modal-until").datepicker("getDate");
+        if ( suspend_until_date !== null ) suspend_until_date = $date(suspend_until_date, {dateformat:"rfc3339"});
+        suspend_hold(
+            $(this).data('hold-id'),
+            suspend_until_date
+        ).success(function () {
+            holdsTable.api().ajax.reload();
+        }).error(function (jqXHR, textStatus, errorThrown) {
+            if (jqXHR.status === 404) {
+                alert(__("Unable to resume, hold not found"));
                 holdsTable.api().ajax.reload();
             }
-          }
+        }).done(function() {
+            $("#suspend-modal-until").val(""); // clean the input
+            $('#suspend-modal').modal('hide');
         });
     });
 });
-- 
2.20.1