From 2283c2650ee0cdaa2d1f3d3564ef792a46b35359 Mon Sep 17 00:00:00 2001 From: Paul Derscheid Date: Thu, 10 Apr 2025 15:17:18 +0000 Subject: [PATCH] Bug 37829: Module, Controller, DBIx::Class changes --- Koha/AdditionalField.pm | 3 ++- Koha/Booking.pm | 2 +- Koha/Bookings.pm | 2 +- Koha/REST/V1/Bookings.pm | 12 ++++++++++++ Koha/REST/V1/ExtendedAttributeTypes.pm | 3 ++- Koha/Schema/Result/Booking.pm | 27 ++++++++++++++++++++++++++ 6 files changed, 45 insertions(+), 4 deletions(-) diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm index 82a16a86725..02c4b328e11 100644 --- a/Koha/AdditionalField.pm +++ b/Koha/AdditionalField.pm @@ -65,7 +65,8 @@ sub to_api { 'aqorders' => 'order', 'aqbooksellers:vendor' => 'vendor', 'erm_titles' => 'title', - 'erm_agreement_periods' => 'agreement_period' + 'erm_agreement_periods' => 'agreement_period', + 'bookings' => 'booking', }; my $json = $self->SUPER::to_api($params); diff --git a/Koha/Booking.pm b/Koha/Booking.pm index 4e62da2a73e..20d8c2952c2 100644 --- a/Koha/Booking.pm +++ b/Koha/Booking.pm @@ -29,7 +29,7 @@ use C4::Letters; use List::Util qw(any); -use base qw(Koha::Object); +use base qw( Koha::Object Koha::Object::Mixin::AdditionalFields ); =head1 NAME diff --git a/Koha/Bookings.pm b/Koha/Bookings.pm index 39fb118e014..00ef4595ac5 100644 --- a/Koha/Bookings.pm +++ b/Koha/Bookings.pm @@ -22,7 +22,7 @@ use Modern::Perl; use Koha::Database; use Koha::Booking; -use base qw(Koha::Objects); +use base qw( Koha::Objects::Mixin::AdditionalFields Koha::Objects ); =head1 NAME diff --git a/Koha/REST/V1/Bookings.pm b/Koha/REST/V1/Bookings.pm index 6807ebb65dd..3f062167a57 100644 --- a/Koha/REST/V1/Bookings.pm +++ b/Koha/REST/V1/Bookings.pm @@ -76,6 +76,7 @@ sub add { return try { my $body = $c->req->json; + my $extended_attributes = delete $body->{extended_attributes} // []; # Validate that exactly one of item_id or itemtype_id is provided my $has_item_id = defined $body->{item_id}; @@ -106,7 +107,12 @@ sub add { } $booking->store; + + my @extended_attributes = map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $booking->extended_attributes( \@extended_attributes ); + $booking->discard_changes; + $c->res->headers->location( $c->req->url->to_string . '/' . $booking->booking_id ); return $c->render( status => 201, @@ -147,6 +153,7 @@ sub update { return try { my $body = $c->req->json; + my $extended_attributes = delete $body->{extended_attributes} // []; # Extract and remove itemtype_id from body (it's not a database column) my $itemtype_id = delete $body->{itemtype_id}; @@ -159,7 +166,12 @@ sub update { } $booking->store(); + + my @extended_attributes = map { { 'id' => $_->{field_id}, 'value' => $_->{value} } } @{$extended_attributes}; + $booking->extended_attributes( \@extended_attributes ); + $booking->discard_changes; + return $c->render( status => 200, openapi => $c->objects->to_api($booking) ); } catch { $c->unhandled_exception($_); diff --git a/Koha/REST/V1/ExtendedAttributeTypes.pm b/Koha/REST/V1/ExtendedAttributeTypes.pm index 029f944674d..e80980ecc38 100644 --- a/Koha/REST/V1/ExtendedAttributeTypes.pm +++ b/Koha/REST/V1/ExtendedAttributeTypes.pm @@ -54,7 +54,8 @@ sub _list { order => 'aqorders', vendor => 'aqbooksellers:vendor', title => 'erm_titles', - agreement_period => 'erm_agreement_periods' + agreement_period => 'erm_agreement_periods', + booking => 'bookings', }; my @tables; diff --git a/Koha/Schema/Result/Booking.pm b/Koha/Schema/Result/Booking.pm index 22265fc9e63..c40208d0e08 100644 --- a/Koha/Schema/Result/Booking.pm +++ b/Koha/Schema/Result/Booking.pm @@ -290,6 +290,33 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-09-05 20:52:03 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:MzP+qYbMutu0qNgndBHHFQ +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.booking_id" }, + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="bookings")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + +__PACKAGE__->has_many( + "extended_attributes", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.booking_id" }, + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="bookings")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); # You can replace this text with custom code or comments, and it will be preserved on regeneration 1; -- 2.39.5