View | Details | Raw Unified | Return to bug 40850
Collapse All | Expand All

(-)a/Koha/ILL/Request.pm (+51 lines)
Lines 1340-1345 sub set_copyright_clearance_confirmed { Link Here
1340
    }
1340
    }
1341
}
1341
}
1342
1342
1343
=head3 add_or_update_attributes
1344
1345
    $request->add_or_update_attributes({
1346
        title => 'Book Title',
1347
        author => 'Author Name',
1348
        isbn => '1234567890'
1349
    });
1350
1351
Adds new attributes or updates existing ones. More efficient than extended_attributes()
1352
for updating existing attributes as it avoids duplicate key errors.
1353
1354
=cut
1355
1356
sub add_or_update_attributes {
1357
    my ( $self, $attributes ) = @_;
1358
1359
    return unless $attributes && ref($attributes) eq 'HASH';
1360
1361
    my $schema = $self->_result->result_source->schema;
1362
    $schema->txn_do(
1363
        sub {
1364
            while ( my ( $type, $value ) = each %{$attributes} ) {
1365
1366
                # Skip undefined or empty values
1367
                next unless defined $value && $value ne '';
1368
1369
                my $attr = $self->extended_attributes->find( { type => $type } );
1370
1371
                if ($attr) {
1372
1373
                    # Update existing attribute only if value changed
1374
                    $attr->update( { value => $value } ) if $attr->value ne $value;
1375
                } else {
1376
1377
                    # Create new attribute
1378
                    Koha::ILL::Request::Attribute->new(
1379
                        {
1380
                            illrequest_id => $self->id,
1381
                            type          => $type,
1382
                            value         => $value,
1383
                            backend       => $self->backend,
1384
                        }
1385
                    )->store;
1386
                }
1387
            }
1388
        }
1389
    );
1390
1391
    return $self;
1392
}
1393
1343
=head3 get_copyright_clearance_confirmed
1394
=head3 get_copyright_clearance_confirmed
1344
1395
1345
    my $confirmed = $request->get_copyright_clearance_confirmed;
1396
    my $confirmed = $request->get_copyright_clearance_confirmed;
(-)a/t/db_dependent/Koha/ILL/Request.t (-2 / +67 lines)
Lines 20-26 Link Here
20
use Modern::Perl;
20
use Modern::Perl;
21
21
22
use Test::NoWarnings;
22
use Test::NoWarnings;
23
use Test::More tests => 7;
23
use Test::More tests => 8;
24
use Test::MockModule;
24
use Test::MockModule;
25
25
26
use Koha::ILL::Requests;
26
use Koha::ILL::Requests;
Lines 233-235 subtest 'copyright clearance methods tests' => sub { Link Here
233
233
234
    $schema->storage->txn_rollback;
234
    $schema->storage->txn_rollback;
235
};
235
};
236
- 
236
237
subtest 'add_or_update_attributes() tests' => sub {
238
239
    plan tests => 13;
240
241
    $schema->storage->txn_begin;
242
243
    my $request = $builder->build_object( { class => 'Koha::ILL::Requests' } );
244
245
    # Test adding new attributes
246
    $request->add_or_update_attributes(
247
        {
248
            title  => 'Test Title',
249
            author => 'Test Author',
250
            isbn   => '1234567890'
251
        }
252
    );
253
254
    my $title_attr  = $request->extended_attributes->find( { type => 'title' } );
255
    my $author_attr = $request->extended_attributes->find( { type => 'author' } );
256
    my $isbn_attr   = $request->extended_attributes->find( { type => 'isbn' } );
257
258
    ok( $title_attr, 'Title attribute created' );
259
    is( $title_attr->value, 'Test Title', 'Title value set correctly' );
260
    ok( $author_attr, 'Author attribute created' );
261
    is( $author_attr->value, 'Test Author', 'Author value set correctly' );
262
    ok( $isbn_attr, 'ISBN attribute created' );
263
    is( $isbn_attr->value, '1234567890', 'ISBN value set correctly' );
264
265
    # Test updating existing attributes
266
    $request->add_or_update_attributes(
267
        {
268
            title  => 'Updated Title',
269
            author => 'Test Author',     # Same value, should not update
270
            year   => '2023'             # New attribute
271
        }
272
    );
273
274
    $title_attr->discard_changes;
275
    $author_attr->discard_changes;
276
    my $year_attr = $request->extended_attributes->find( { type => 'year' } );
277
278
    is( $title_attr->value,  'Updated Title', 'Title attribute updated' );
279
    is( $author_attr->value, 'Test Author',   'Author attribute unchanged when same value' );
280
    ok( $year_attr, 'Year attribute created' );
281
    is( $year_attr->value, '2023', 'Year value set correctly' );
282
283
    # Test with empty/undefined values (should be skipped)
284
    $request->add_or_update_attributes(
285
        {
286
            empty_field => '',
287
            undef_field => undef,
288
            valid_field => 'valid'
289
        }
290
    );
291
292
    my $empty_attr = $request->extended_attributes->find( { type => 'empty_field' } );
293
    my $undef_attr = $request->extended_attributes->find( { type => 'undef_field' } );
294
    my $valid_attr = $request->extended_attributes->find( { type => 'valid_field' } );
295
296
    is( $empty_attr, undef, 'Empty value skipped' );
297
    is( $undef_attr, undef, 'Undefined value skipped' );
298
    ok( $valid_attr, 'Valid value processed' );
299
300
    $schema->storage->txn_rollback;
301
};

Return to bug 40850