From 27d3b1cf81137367f9086efe9c398c3004f0512b Mon Sep 17 00:00:00 2001 From: Kyle M Hall Date: Tue, 17 Sep 2024 09:26:49 -0400 Subject: [PATCH] Bug 37943: Log object create as JSON diff, implement for items We should store newly created objects as diffs just as we do for modifications. This enhancement will store modification diffs if the action is set to ADD or CREATE and an "original" object is passed in. Test Plan: 1) Enable CataloguingLog 2) Create an item 3) Query the database for the newest action log: select * from action_logs order by action_id desc limit 1\G 4) Note the diff column is NULL 5) Apply this patch 6) Restart all the things! 7) Create another item 8) Query the database again 9) Note the diff has been created! Signed-off-by: Sukhmandeep Benipal Signed-off-by: Martin Renvoize Signed-off-by: Katrin Fischer --- C4/Log.pm | 13 +++++++++++-- Koha/Item.pm | 5 +++-- 2 files changed, 14 insertions(+), 4 deletions(-) diff --git a/C4/Log.pm b/C4/Log.pm index cf0fd34de7..543096259a 100644 --- a/C4/Log.pm +++ b/C4/Log.pm @@ -118,9 +118,18 @@ sub logaction { } my $trace = @trace ? to_json( \@trace, { utf8 => 1, pretty => 0 } ) : undef; + my $is_object = blessed($original) && $original->isa('Koha::Object'); + + if ( $actionname =~ /^(ADD|CREATE)$/ ) { + # Log diff against empty hashref for newly created objects + $updated = $is_object ? $original->unblessed : $original; + $original = {}; + } else { + # Log diff against hashref of pre-modified object if passed in + $original = $is_object ? $original->unblessed : $original; + } + my $diff = undef; - $diff = encode_json( diff( $original->unblessed, $updated, noU => 1 ) ) - if blessed($original) && $original->isa('Koha::Object'); $diff //= encode_json( diff( $original, $updated, noU => 1 ) ) if $original && ref $updated eq 'HASH'; diff --git a/Koha/Item.pm b/Koha/Item.pm index e098b05b8e..70cc78f50e 100644 --- a/Koha/Item.pm +++ b/Koha/Item.pm @@ -222,15 +222,16 @@ sub store { my $original = Koha::Items->find( $self->itemnumber ); # $original will be undef if $action eq 'create' my $result = $self->SUPER::store; + $self->discard_changes; if ( $log_action && C4::Context->preference("CataloguingLog") ) { $action eq 'create' - ? logaction( "CATALOGUING", "ADD", $self->itemnumber, "item" ) + ? logaction( "CATALOGUING", "ADD", $self->itemnumber, 'item', undef, $self ) : logaction( "CATALOGUING", "MODIFY", $self->itemnumber, $self, undef, $original ); } my $indexer = Koha::SearchEngine::Indexer->new({ index => $Koha::SearchEngine::BIBLIOS_INDEX }); $indexer->index_records( $self->biblionumber, "specialUpdate", "biblioserver" ) unless $params->{skip_record_index}; - $self->get_from_storage->_after_item_action_hooks({ action => $action }); + $self->_after_item_action_hooks({ action => $action }); Koha::BackgroundJob::BatchUpdateBiblioHoldsQueue->new->enqueue( { -- 2.39.5