From 0fda5fc79f1c83d58345d3949e96e07e49b3249b Mon Sep 17 00:00:00 2001 From: Andrew Isherwood Date: Mon, 13 Jun 2022 11:21:28 +0100 Subject: [PATCH] Bug 30719: Logging of batch operations This commit adds logging of the following batch operations: - Batch creation - Batch update (Updating of the batch itself, not its contents) - Batch delete Signed-off-by: Pedro Amorim --- Koha/Illbatch.pm | 85 ++++++++++++++++++++++++++++++++++++++ Koha/REST/V1/Illbatches.pm | 7 ++-- 2 files changed, 88 insertions(+), 4 deletions(-) diff --git a/Koha/Illbatch.pm b/Koha/Illbatch.pm index edb2feb029..c604661617 100644 --- a/Koha/Illbatch.pm +++ b/Koha/Illbatch.pm @@ -19,6 +19,8 @@ package Koha::Illbatch; use Modern::Perl; use Koha::Database; +use Koha::Illrequest::Logger; +use JSON qw( to_json ); use base qw(Koha::Object); =head1 NAME @@ -72,6 +74,89 @@ sub requests_count { })->count; } +=head3 create_and_log + + $batch->create_and_log; + +Log batch creation following storage + +=cut + +sub create_and_log { + my ( $self ) = @_; + + $self->store; + + my $logger = Koha::Illrequest::Logger->new; + + $logger->log_something({ + modulename => 'ILL', + actionname => 'batch_create', + objectnumber => $self->id, + infos => to_json({}) + }); +} + +=head3 update_and_log + + $batch->update_and_log; + +Log batch update following storage + +=cut + +sub update_and_log { + my ( $self, $params ) = @_; + + my $before = { + name => $self->name, + branchcode => $self->branchcode + }; + + $self->set( $params ); + my $update = $self->store; + + my $after = { + name => $self->name, + branchcode => $self->branchcode + }; + + my $logger = Koha::Illrequest::Logger->new; + + $logger->log_something({ + modulename => 'ILL', + actionname => 'batch_update', + objectnumber => $self->id, + infos => to_json({ + before => $before, + after => $after + }) + }); +} + +=head3 delete_and_log + + $batch->delete_and_log; + +Log batch delete + +=cut + +sub delete_and_log { + my ( $self ) = @_; + + my $logger = Koha::Illrequest::Logger->new; + + $logger->log_something({ + modulename => 'ILL', + actionname => 'batch_delete', + objectnumber => $self->id, + infos => to_json({}) + }); + + $self->delete; +} + =head2 Internal methods =head3 _type diff --git a/Koha/REST/V1/Illbatches.pm b/Koha/REST/V1/Illbatches.pm index af21e762ae..e2b175e241 100644 --- a/Koha/REST/V1/Illbatches.pm +++ b/Koha/REST/V1/Illbatches.pm @@ -129,7 +129,7 @@ sub add { return try { my $batch = Koha::Illbatch->new( $body ); - $batch->store; + $batch->create_and_log; $c->res->headers->location( $c->req->url->to_string . '/' . $batch->id ); my $ret = { @@ -171,8 +171,7 @@ sub update { delete $params->{cardnumber}; return try { - $batch->set( $params ); - $batch->store(); + $batch->update_and_log( $params ); my $ret = { %{$batch->unblessed}, @@ -208,7 +207,7 @@ sub delete { } return try { - $batch->delete; + $batch->delete_and_log; return $c->render( status => 204, openapi => ''); } catch { -- 2.25.1