From 1e0a05bfa3f82e8ba986c9f90eb7f37e4ad2401c Mon Sep 17 00:00:00 2001
From: Pedro Amorim <pedro.amorim@ptfs-europe.com>
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 <edith.speller@ukhsa.gov.uk>
Signed-off-by: Michaela Sieber <michaela.sieber@kit.edu>
---
 Koha/AdditionalField.pm                       |  1 +
 Koha/REST/V1/ERM/Licenses.pm                  |  8 +++++
 Koha/REST/V1/ExtendedAttributeTypes.pm        |  1 +
 Koha/Schema/Result/ErmLicense.pm              | 32 +++++++++++++++++++
 api/v1/swagger/definitions/erm_license.yaml   | 10 ++++++
 .../definitions/extended_attribute_value.yaml | 18 +++++++++++
 api/v1/swagger/paths/erm_licenses.yaml        |  7 ++++
 .../paths/extended_attribute_types.yaml       |  1 +
 api/v1/swagger/swagger.yaml                   |  2 ++
 9 files changed, 80 insertions(+)
 create mode 100644 api/v1/swagger/definitions/extended_attribute_value.yaml

diff --git a/Koha/AdditionalField.pm b/Koha/AdditionalField.pm
index a9230ff666..7e5c16a049 100644
--- a/Koha/AdditionalField.pm
+++ b/Koha/AdditionalField.pm
@@ -59,6 +59,7 @@ sub to_api {
         'accountlines:debit'  => 'debit',
         'aqbasket'            => 'basket',
         'aqinvoices'          => 'invoice',
+        'erm_licenses'        => 'license',
         'aqorders'            => 'order',
     };
 
diff --git a/Koha/REST/V1/ERM/Licenses.pm b/Koha/REST/V1/ERM/Licenses.pm
index 856386e0c1..1a233761ee 100644
--- a/Koha/REST/V1/ERM/Licenses.pm
+++ b/Koha/REST/V1/ERM/Licenses.pm
@@ -87,11 +87,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,
@@ -165,11 +169,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/REST/V1/ExtendedAttributeTypes.pm b/Koha/REST/V1/ExtendedAttributeTypes.pm
index c49fddc232..8dfa4ea6ed 100644
--- a/Koha/REST/V1/ExtendedAttributeTypes.pm
+++ b/Koha/REST/V1/ExtendedAttributeTypes.pm
@@ -50,6 +50,7 @@ sub list {
         credit  => 'accountlines:credit',
         debit   => 'accountlines:debit',
         invoice => 'aqinvoices',
+        license => 'erm_licenses',
         order   => 'aqorders',
     };
 
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/erm_license.yaml b/api/v1/swagger/definitions/erm_license.yaml
index 5a2d43cad8..0bb1222ab5 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: extended_attribute_value.yaml
+  _strings:
+    type:
+      - object
+      - "null"
 
 additionalProperties: false
 required:
diff --git a/api/v1/swagger/definitions/extended_attribute_value.yaml b/api/v1/swagger/definitions/extended_attribute_value.yaml
new file mode 100644
index 0000000000..3bdfb1acbc
--- /dev/null
+++ b/api/v1/swagger/definitions/extended_attribute_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/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/paths/extended_attribute_types.yaml b/api/v1/swagger/paths/extended_attribute_types.yaml
index f20eb2ff58..f66599c0d1 100644
--- a/api/v1/swagger/paths/extended_attribute_types.yaml
+++ b/api/v1/swagger/paths/extended_attribute_types.yaml
@@ -18,6 +18,7 @@
           - credit
           - debit
           - invoice
+          - license
           - order
           - subscription
       - $ref: "../swagger.yaml#/parameters/match"
diff --git a/api/v1/swagger/swagger.yaml b/api/v1/swagger/swagger.yaml
index 6b74cf4c54..a78c26f442 100644
--- a/api/v1/swagger/swagger.yaml
+++ b/api/v1/swagger/swagger.yaml
@@ -86,6 +86,8 @@ definitions:
     $ref: ./definitions/error.yaml
   extended_attribute_type:
     $ref: ./definitions/extended_attribute_type.yaml
+  extended_attribute_value:
+    $ref: ./definitions/extended_attribute_value.yaml
   fund:
     $ref: ./definitions/fund.yaml
   hold:
-- 
2.39.2