Bug 36084: svc - members/add_to_list

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2024-02-22 12:01:31 +01:00
parent dfe3fcde90
commit e3e288c7be
Signed by: jonathan.druart
GPG key ID: A085E712BEF0E0F0
4 changed files with 68 additions and 37 deletions

View file

@ -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('<li><a class="patron-list-add" href="#" data-listid="' + data.patron_list.patron_list_id + '">' + data.patron_list.name + '</li>');
$("#patronlist-dropdown .divider").before('<li><a class="patron-list-add" href="#" data-listid="' + success.patron_list.patron_list_id + '">' + success.patron_list.name + '</li>');
}
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!") );

View file

@ -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(),
};

View file

@ -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;

View file

@ -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];
}
my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
$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 );
}
binmode STDOUT, ":encoding(UTF-8)";
print $input->header(
-type => 'application/json',
-charset => 'UTF-8'
);
if ($input->request_method eq "POST") {
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];
}
my @patrons_added_to_list = AddPatronsToList( { list => $patron_list, borrowernumbers => \@borrowernumbers } );
$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 );