From e3e288c7be2c1772ed4e70101ddee7ec93bd124a Mon Sep 17 00:00:00 2001 From: Jonathan Druart Date: Thu, 22 Feb 2024 12:01:31 +0100 Subject: [PATCH] Bug 36084: svc - members/add_to_list Signed-off-by: Jonathan Druart --- .../prog/en/modules/members/member.tt | 26 +++++------- .../intranet-tmpl/prog/js/fetch/api-client.js | 2 + .../prog/js/fetch/patron-api-client.js | 31 ++++++++++++++ svc/members/add_to_list | 42 ++++++++++--------- 4 files changed, 66 insertions(+), 35 deletions(-) create mode 100644 koha-tmpl/intranet-tmpl/prog/js/fetch/patron-api-client.js diff --git a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt b/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt index 0d7e354721..c8e8545caa 100644 --- a/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt +++ b/koha-tmpl/intranet-tmpl/prog/en/modules/members/member.tt @@ -316,32 +316,26 @@ function patronListAdd(){ var borrowernumbers = JSON.parse( localStorage.getItem("patron_search_selections") ) || []; if ( borrowernumbers.length > 0 ){ - var data = { - add_to_patron_list: $("#add_to_patron_list").val(), - new_patron_list: $("#new_patron_list").val(), - borrowernumbers: borrowernumbers - }; - $.ajax({ - data: data, - type: 'POST', - url: '/cgi-bin/koha/svc/members/add_to_list', - success: function(data) { + const client = APIClient.patron; + client.lists.add_patrons({patron_ids: borrowernumbers, new_list_name: $("#new_patron_list").val(), list_id: $("#add_to_patron_list").val()}).then( + success => { $("#patron_list_dialog").show(); - $("#patron_list_dialog > span.patrons-length").html(data.patrons_added_to_list); - $("#patron_list_dialog > a").attr("href", "/cgi-bin/koha/patron_lists/list.pl?patron_list_id=" + data.patron_list.patron_list_id); - $("#patron_list_dialog > a").html(data.patron_list.name); + $("#patron_list_dialog > span.patrons-length").html(success.patrons_added_to_list); + $("#patron_list_dialog > a").attr("href", "/cgi-bin/koha/patron_lists/list.pl?patron_list_id=" + success.patron_list.patron_list_id); + $("#patron_list_dialog > a").html(success.patron_list.name); if ( $('#add_to_patron_list').val() == 'new' ) { /* Add a new entry to the menu */ - $("#patronlist-dropdown .divider").before('
  • ' + data.patron_list.name + '
  • '); + $("#patronlist-dropdown .divider").before('
  • ' + success.patron_list.name + '
  • '); } persistPatronSelections('new-patron-list_form'); prepSelections(); }, - error: function() { + error => { alert( _("An error occurred. Patron list could not be updated.") ); + console.warn("Something wrong happened: %s".format(error)); } - }); + ); return true; } else { alert( _("You have not selected any patrons to add to a list!") ); diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js index 223da55aa8..6d616768a5 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/api-client.js @@ -4,6 +4,7 @@ import CirculationAPIClient from "./circulation-api-client.js"; import ClubAPIClient from "./club-api-client.js"; import CoverImageAPIClient from "./cover-image-api-client.js"; import LocalizationAPIClient from "./localization-api-client.js"; +import PatronAPIClient from "./patron-api-client.js"; import SysprefAPIClient from "./system-preferences-api-client.js"; export const APIClient = { @@ -13,5 +14,6 @@ export const APIClient = { club: new ClubAPIClient(), cover_image: new CoverImageAPIClient(), localization: new LocalizationAPIClient(), + patron: new PatronAPIClient(), syspref: new SysprefAPIClient(), }; diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/patron-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/patron-api-client.js new file mode 100644 index 0000000000..da8a510711 --- /dev/null +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/patron-api-client.js @@ -0,0 +1,31 @@ +import HttpClient from "./http-client.js"; + +export class PatronAPIClient extends HttpClient { + constructor() { + super({ + baseURL: "/cgi-bin/koha/svc/", + }); + } + + get lists() { + return { + add_patrons: ({ patron_ids, list_id, new_list_name }) => + this.post({ + endpoint: "members/add_to_list", + body: "add_to_patron_list=%s&new_patron_list=%s&%s".format( + list_id, + new_list_name, + patron_ids + .map(id => "borrowernumber=%s".format(id)) + .join("&") + ), + headers: { + "Content-Type": + "application/x-www-form-urlencoded;charset=utf-8", + }, + }), + }; + } +} + +export default PatronAPIClient; diff --git a/svc/members/add_to_list b/svc/members/add_to_list index 2219717003..9c53be4629 100755 --- a/svc/members/add_to_list +++ b/svc/members/add_to_list @@ -34,30 +34,34 @@ my ( $auth_status ) = check_cookie_auth( exit 0 if $auth_status ne "ok"; my $add_to_patron_list = $input->param('add_to_patron_list'); my $new_patron_list = $input->param('new_patron_list'); -my @borrowernumbers = $input->multi_param('borrowernumbers[]'); +my @borrowernumbers = $input->multi_param('borrowernumber'); my $response; -if ($add_to_patron_list) { - my $patron_list = []; - if ( $add_to_patron_list eq 'new' ) { - $patron_list = AddPatronList( { name => $new_patron_list } ); - } - else { - $patron_list = - [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0]; - } +binmode STDOUT, ":encoding(UTF-8)"; +if ($input->request_method eq "POST") { + if ($add_to_patron_list) { + my $patron_list = []; - my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } ); + if ( $add_to_patron_list eq 'new' ) { + $patron_list = AddPatronList( { name => $new_patron_list } ); + } + else { + $patron_list = + [ GetPatronLists( { patron_list_id => $add_to_patron_list } ) ]->[0]; + } - $response->{patron_list} = { patron_list_id => $patron_list->patron_list_id, name => $patron_list->name }; - $response->{patrons_added_to_list} = scalar( @patrons_added_to_list ); -} + my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } ); -binmode STDOUT, ":encoding(UTF-8)"; -print $input->header( - -type => 'application/json', - -charset => 'UTF-8' -); + $response->{patron_list} = { patron_list_id => $patron_list->patron_list_id, name => $patron_list->name }; + $response->{patrons_added_to_list} = scalar( @patrons_added_to_list ); + } + print $input->header( + -type => 'application/json', + -charset => 'UTF-8' + ); +} else { + print $input->header(-type => 'text/xml', -status => '400 Bad Request'); +} print to_json( $response ); -- 2.39.5