Bug 36084: svc - checkout_notes

Signed-off-by: Jonathan Druart <jonathan.druart@bugs.koha-community.org>
This commit is contained in:
Jonathan Druart 2024-02-20 15:39:21 +01:00
parent 6a9ff124a5
commit 526ceeb4fa
Signed by: jonathan.druart
GPG key ID: A085E712BEF0E0F0
3 changed files with 69 additions and 46 deletions

View file

@ -179,44 +179,52 @@
}
});
function show_error(){
// FIXME Can be improved by displaying meaningful error.
$("#error").text(_("Unable to change status of note.")).show();
}
$("#notestable").on("click", "button.seen, button.notseen", function(event){
event.preventDefault(); // prevent form submission
var $op = $(this).attr("name");
var $issue_id = $(this).data('issue_id');
var ajaxData = {
'op': $op,
'issue_id': $issue_id,
};
var op = $(this).attr("name");
var issue_id = $(this).data('issue_id');
$.ajax({
url: '/cgi-bin/koha/svc/checkout_notes/',
type: 'POST',
dataType: 'json',
data: ajaxData,
})
.done(function(data){
if (data.status == 'success'){
if ( $op == 'notseen' ){
$("#status_" + $issue_id).text(_("Not seen"));
$(event.target).parent().siblings(".seen1").removeClass("seen1").addClass("seen0");
$(event.target).siblings(".seen").prop("disabled", false);
$(event.target).prop("disabled", true);
} else {
$("#status_" + $issue_id).text(_("Seen"));
$(event.target).parent().siblings(".seen0").removeClass("seen0").addClass("seen1");
$(event.target).siblings(".notseen").prop("disabled", false);
$(event.target).prop("disabled", true);
const client = APIClient.circulation;
if ( op == 'seen' ) {
client.checkouts.mark_as_seen(issue_id).then(
success => {
if (success.seen){
$("#status_" + issue_id).text(_("Seen"));
$(event.target).parent().siblings(".seen0").removeClass("seen0").addClass("seen1");
$(event.target).siblings(".notseen").prop("disabled", false);
$(event.target).prop("disabled", true);
} else {
show_error();
}
},
error => {
console.warn("Something wrong happened: %s".format(error));
show_error();
}
} else {
$("#error").text(_("Unable to change status of note."));
$("#error").show();
}
})
.error(function(data){
$("#error").text(_("Unable to change status of note."));
$("#error").show();
});
);
} else {
client.checkouts.mark_as_not_seen(issue_id).then(
success => {
if (!success.seen){
$("#status_" + issue_id).text(_("Not seen"));
$(event.target).parent().siblings(".seen1").removeClass("seen1").addClass("seen0");
$(event.target).siblings(".seen").prop("disabled", false);
$(event.target).prop("disabled", true);
} else {
show_error();
}
},
error => {
console.warn("Something wrong happened: %s".format(error));
show_error();
}
);
}
});
});
</script>

View file

@ -26,6 +26,29 @@ export class CirculationAPIClient extends HttpClient {
}),
};
}
get checkouts() {
return {
mark_as_seen: checkout_id =>
this.post({
endpoint: "checkout_notes",
body: "issue_id=%s&op=%s".format(checkout_id, "cud-seen"),
headers: {
"Content-Type":
"application/x-www-form-urlencoded;charset=utf-8",
},
}),
mark_as_not_seen: checkout_id =>
this.post({
endpoint: "checkout_notes",
body: "issue_id=%s&op=%s".format(checkout_id, "cud-notseen"),
headers: {
"Content-Type":
"application/x-www-form-urlencoded;charset=utf-8",
},
})
}
}
}
export default CirculationAPIClient;

View file

@ -21,7 +21,6 @@ use Modern::Perl;
use JSON qw( to_json );
use CGI;
use C4::Service;
use C4::Auth qw ( check_cookie_auth );
use C4::Output qw( is_ajax output_with_http_headers );
use Koha::Checkouts;
@ -41,23 +40,16 @@ my ( $auth_status ) = check_cookie_auth( $query->cookie('CGISESSID'), { circulat
if ( $auth_status ne "ok" ) {
exit 0;
}
my $op = $query->param('op');
if ($is_ajax) {
my $issue_id = $query->param('issue_id');
my $issue = Koha::Checkouts->find($issue_id);
my $op = $query->param('op');
my $status = 'success';
if ($op eq 'seen'){
if ($op eq 'cud-seen'){
$issue->set({ noteseen => 1 })->store;
if ( $issue->noteseen != 1 ) {
$status = 'failure';
}
} elsif ($op eq 'notseen'){
} elsif ($op eq 'cud-notseen'){
$issue->set({ noteseen => 0 })->store;
if ( $issue->noteseen != 0 ) {
$status = 'failure';
}
}
my $json = to_json ( { status => $status } );
my $json = to_json ( { seen => $issue->noteseen } );
output_with_http_headers $query, undef, $json, 'js';
exit;
}