From 9003c1797b643aa1c57a9b506c10076c9ce07f03 Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Wed, 8 Nov 2023 11:33:20 -0100 Subject: [PATCH] Bug 35287: REST API - Add additional fields support to ERM Licenses Signed-off-by: Edith Speller --- Koha/REST/V1/ERM/Licenses.pm | 8 +++++ Koha/Schema/Result/ErmLicense.pm | 32 +++++++++++++++++++ .../definitions/additional_field_value.yaml | 18 +++++++++++ api/v1/swagger/definitions/erm_license.yaml | 10 ++++++ api/v1/swagger/paths/erm_licenses.yaml | 7 ++++ api/v1/swagger/swagger.yaml | 2 ++ 6 files changed, 77 insertions(+) create mode 100644 api/v1/swagger/definitions/additional_field_value.yaml diff --git a/Koha/REST/V1/ERM/Licenses.pm b/Koha/REST/V1/ERM/Licenses.pm index 616c10331c..10f1a01a81 100644 --- a/Koha/REST/V1/ERM/Licenses.pm +++ b/Koha/REST/V1/ERM/Licenses.pm @@ -91,11 +91,15 @@ sub add { my $user_roles = delete $body->{user_roles} // []; my $documents = delete $body->{documents} // []; + my $extended_attributes = delete $body->{extended_attributes} // []; my $license = Koha::ERM::License->new_from_api($body)->store; $license->user_roles($user_roles); $license->documents($documents); + my @extended_attributes = map { {'id' => $_->{field_id}, 'value' => $_->{value}} } @{$extended_attributes}; + $license->extended_attributes( \@extended_attributes ); + $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id); return $c->render( status => 201, @@ -173,11 +177,15 @@ sub update { my $user_roles = delete $body->{user_roles} // []; my $documents = delete $body->{documents} // []; + my $extended_attributes = delete $body->{extended_attributes} // []; $license->set_from_api($body)->store; $license->user_roles($user_roles); $license->documents($documents); + my @extended_attributes = map { {'id' => $_->{field_id}, 'value' => $_->{value}} } @{$extended_attributes}; + $license->extended_attributes( \@extended_attributes ); + $c->res->headers->location($c->req->url->to_string . '/' . $license->license_id); return $c->render( status => 200, diff --git a/Koha/Schema/Result/ErmLicense.pm b/Koha/Schema/Result/ErmLicense.pm index 17402c0003..bd90014e0d 100644 --- a/Koha/Schema/Result/ErmLicense.pm +++ b/Koha/Schema/Result/ErmLicense.pm @@ -190,6 +190,38 @@ __PACKAGE__->belongs_to( # Created by DBIx::Class::Schema::Loader v0.07049 @ 2022-11-01 07:44:13 # DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:Fs4bmI/N6Cvicv3RW2qwXQ +__PACKAGE__->has_many( + "additional_field_values", + "Koha::Schema::Result::AdditionalFieldValue", + sub { + my ($args) = @_; + + return { + "$args->{foreign_alias}.record_id" => { -ident => "$args->{self_alias}.license_id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="erm_licenses")' }, + }; + }, + { 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}.license_id" }, + + "$args->{foreign_alias}.field_id" => + { -in => \'(SELECT id FROM additional_fields WHERE tablename="erm_licenses")' }, + }; + }, + { cascade_copy => 0, cascade_delete => 0 }, +); + sub koha_object_class { 'Koha::ERM::License'; } diff --git a/api/v1/swagger/definitions/additional_field_value.yaml b/api/v1/swagger/definitions/additional_field_value.yaml new file mode 100644 index 0000000000..3bdfb1acbc --- /dev/null +++ b/api/v1/swagger/definitions/additional_field_value.yaml @@ -0,0 +1,18 @@ +--- +type: object +properties: + id: + description: id of the field + type: string + readOnly: true + field_id: + type: integer + description: FK in corresponding to id in additional_fields table + record_id: + description: ID corresponding to the object ID this additional field relates to + type: string + value: + description: value of the additional field + type: string + +additionalProperties: false \ No newline at end of file diff --git a/api/v1/swagger/definitions/erm_license.yaml b/api/v1/swagger/definitions/erm_license.yaml index 5a2d43cad8..939cbb68ba 100644 --- a/api/v1/swagger/definitions/erm_license.yaml +++ b/api/v1/swagger/definitions/erm_license.yaml @@ -51,6 +51,16 @@ properties: type: - object - "null" + extended_attributes: + description: Related additional field values + type: + - array + items: + $ref: additional_field_value.yaml + _strings: + type: + - object + - "null" additionalProperties: false required: diff --git a/api/v1/swagger/paths/erm_licenses.yaml b/api/v1/swagger/paths/erm_licenses.yaml index 6dc6c4ebe6..feb347a253 100644 --- a/api/v1/swagger/paths/erm_licenses.yaml +++ b/api/v1/swagger/paths/erm_licenses.yaml @@ -59,6 +59,9 @@ type: string enum: - vendor + - extended_attributes + - +strings + collectionFormat: csv responses: 200: description: A list of agreements' licenses @@ -170,6 +173,8 @@ - user_roles.patron - vendor - documents + - extended_attributes + - +strings collectionFormat: csv responses: 200: @@ -230,6 +235,8 @@ enum: - user_roles - documents + - extended_attributes + - +strings collectionFormat: csv responses: 200: diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml index 0fb422e2c3..70e0098ac2 100644 --- a/api/v1/swagger/swagger.yaml +++ b/api/v1/swagger/swagger.yaml @@ -6,6 +6,8 @@ definitions: $ref: ./definitions/account_line.yaml additional_field: $ref: ./definitions/additional_field.yaml + additional_field_value: + $ref: ./definitions/additional_field_value.yaml advancededitormacro: $ref: ./definitions/advancededitormacro.yaml allows_renewal: -- 2.30.2