From 15944a017b90eb9ea982ff95fecf7dd7a8633b5a Mon Sep 17 00:00:00 2001 From: Pedro Amorim Date: Tue, 4 Feb 2025 13:31:57 +0000 Subject: [PATCH] Bug 37893: SIP accounts custom patron fields Signed-off-by: Andrew Fuerste Henry Signed-off-by: Lisette Scheer Signed-off-by: Martin Renvoize Signed-off-by: Lucas Gass --- Koha/REST/V1/SIP2/Accounts.pm | 16 ++- Koha/SIP2/Account.pm | 33 +++++ Koha/SIP2/Account/CustomPatronField.pm | 59 +++++++++ Koha/SIP2/Account/CustomPatronFields.pm | 52 ++++++++ Koha/Schema/Result/SipAccount.pm | 19 ++- .../Result/SipAccountCustomPatronField.pm | 123 ++++++++++++++++++ api/v1/swagger/definitions/sip2_account.yaml | 5 + api/v1/swagger/paths/sip2_accounts.yaml | 2 + .../data/mysql/atomicupdate/bug_37893.pl | 23 +++- installer/data/mysql/kohastructure.sql | 19 +++ .../prog/js/fetch/sip2-api-client.js | 2 +- .../components/SIP2/SIP2AccountResource.vue | 54 ++++++++ 12 files changed, 396 insertions(+), 11 deletions(-) create mode 100644 Koha/SIP2/Account/CustomPatronField.pm create mode 100644 Koha/SIP2/Account/CustomPatronFields.pm create mode 100644 Koha/Schema/Result/SipAccountCustomPatronField.pm diff --git a/Koha/REST/V1/SIP2/Accounts.pm b/Koha/REST/V1/SIP2/Accounts.pm index 7bf3363ada..5010c53f90 100644 --- a/Koha/REST/V1/SIP2/Accounts.pm +++ b/Koha/REST/V1/SIP2/Accounts.pm @@ -83,13 +83,15 @@ sub add { my $body = $c->req->json; - my $custom_item_fields = delete $body->{custom_item_fields} // []; - my $item_fields = delete $body->{item_fields} // []; - my $patron_attributes = delete $body->{patron_attributes} // []; + my $custom_item_fields = delete $body->{custom_item_fields} // []; + my $item_fields = delete $body->{item_fields} // []; + my $custom_patron_fields = delete $body->{custom_patron_fields} // []; + my $patron_attributes = delete $body->{patron_attributes} // []; my $account = Koha::SIP2::Account->new_from_api($body)->store; $account->custom_item_fields($custom_item_fields); $account->item_fields($item_fields); + $account->custom_patron_fields($custom_patron_fields); $account->patron_attributes($patron_attributes); $c->res->headers->location( $c->req->url->to_string . '/' . $account->sip_account_id ); @@ -141,13 +143,15 @@ sub update { my $body = $c->req->json; - my $custom_item_fields = delete $body->{custom_item_fields} // []; - my $item_fields = delete $body->{item_fields} // []; - my $patron_attributes = delete $body->{patron_attributes} // []; + my $custom_item_fields = delete $body->{custom_item_fields} // []; + my $item_fields = delete $body->{item_fields} // []; + my $custom_patron_fields = delete $body->{custom_patron_fields} // []; + my $patron_attributes = delete $body->{patron_attributes} // []; $account->set_from_api($body)->store; $account->custom_item_fields($custom_item_fields); $account->item_fields($item_fields); + $account->custom_patron_fields($custom_patron_fields); $account->patron_attributes($patron_attributes); $c->res->headers->location( $c->req->url->to_string . '/' . $account->sip_account_id ); diff --git a/Koha/SIP2/Account.pm b/Koha/SIP2/Account.pm index 3eec8bc710..21894bb692 100644 --- a/Koha/SIP2/Account.pm +++ b/Koha/SIP2/Account.pm @@ -21,6 +21,7 @@ use base qw(Koha::SIP2::Object Koha::Object); use Koha::SIP2::Account::CustomItemFields; use Koha::SIP2::Account::ItemFields; +use Koha::SIP2::Account::CustomPatronFields; use Koha::SIP2::Account::PatronAttributes; use Koha::SIP2::Institution; @@ -85,6 +86,12 @@ sub get_for_config { $unblessed->{item_field} = scalar(@item_fields) == 1 ? $item_fields[0] : \@item_fields; } + my @custom_patron_fields = map { $_->get_for_config } @{ $self->custom_patron_fields->as_list }; + if (@custom_patron_fields) { + $unblessed->{custom_patron_field} = + scalar(@custom_patron_fields) == 1 ? $custom_patron_fields[0] : \@custom_patron_fields; + } + my @patron_attributes = map { $_->get_for_config } @{ $self->patron_attributes->as_list }; if (@patron_attributes) { $unblessed->{patron_attribute} = scalar(@patron_attributes) == 1 ? $patron_attributes[0] : \@patron_attributes; @@ -158,6 +165,32 @@ sub item_fields { return Koha::SIP2::Account::ItemFields->_new_from_dbic($item_fields_rs); } +=head3 custom_patron_fields + +Returns the custom patron fields for this SIP account + +=cut + +sub custom_patron_fields { + my ( $self, $custom_patron_fields ) = @_; + + if ($custom_patron_fields) { + my $schema = $self->_result->result_source->schema; + $schema->txn_do( + sub { + $self->custom_patron_fields->delete; + + for my $custom_patron_field (@$custom_patron_fields) { + $self->_result->add_to_sip_account_custom_patron_fields($custom_patron_field); + } + } + ); + } + + my $custom_patron_fields_rs = $self->_result->sip_account_custom_patron_fields; + return Koha::SIP2::Account::CustomPatronFields->_new_from_dbic($custom_patron_fields_rs); +} + =head3 patron_attributes Returns the patron attributes for this SIP account diff --git a/Koha/SIP2/Account/CustomPatronField.pm b/Koha/SIP2/Account/CustomPatronField.pm new file mode 100644 index 0000000000..919aa4e526 --- /dev/null +++ b/Koha/SIP2/Account/CustomPatronField.pm @@ -0,0 +1,59 @@ +package Koha::SIP2::Account::CustomPatronField; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Database; + +use base qw(Koha::Object); + +=head1 NAME + +Koha::SIP2::Account::CustomPatronField - Koha SIP2 Account Patron Custom Field Object class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 get_for_config + +Returns the patron custom field hashref as expected by C4/SIP/Sip/Configuration->new; + +=cut + +sub get_for_config { + my ($self) = @_; + + return { + 'field' => $self->field, + 'template' => $self->template, + }; +} + +=head2 Internal methods + +=head3 _type + +=cut + +sub _type { + return 'SipAccountCustomPatronField'; +} + +1; diff --git a/Koha/SIP2/Account/CustomPatronFields.pm b/Koha/SIP2/Account/CustomPatronFields.pm new file mode 100644 index 0000000000..6e9c0b1945 --- /dev/null +++ b/Koha/SIP2/Account/CustomPatronFields.pm @@ -0,0 +1,52 @@ +package Koha::SIP2::Account::CustomPatronFields; + +# This file is part of Koha. +# +# Koha is free software; you can redistribute it and/or modify it +# under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 3 of the License, or +# (at your option) any later version. +# +# Koha is distributed in the hope that it will be useful, but +# WITHOUT ANY WARRANTY; without even the implied warranty of +# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +# GNU General Public License for more details. +# +# You should have received a copy of the GNU General Public License +# along with Koha; if not, see . + +use Modern::Perl; + +use Koha::Database; + +use Koha::SIP2::Account::CustomPatronField; + +use base qw(Koha::Objects); + +=head1 NAME + +Koha::SIP2::Account::CustomPatronFields - Koha SIP2 Account Custom Patron Fields Object set class + +=head1 API + +=head2 Class Methods + +=cut + +=head3 type + +=cut + +sub _type { + return 'SipAccountCustomPatronField'; +} + +=head3 object_class + +=cut + +sub object_class { + return 'Koha::SIP2::Account::CustomPatronField'; +} + +1; diff --git a/Koha/Schema/Result/SipAccount.pm b/Koha/Schema/Result/SipAccount.pm index a2dc01595d..32c3592a37 100644 --- a/Koha/Schema/Result/SipAccount.pm +++ b/Koha/Schema/Result/SipAccount.pm @@ -361,6 +361,21 @@ __PACKAGE__->has_many( { cascade_copy => 0, cascade_delete => 0 }, ); +=head2 sip_account_custom_patron_fields + +Type: has_many + +Related object: L + +=cut + +__PACKAGE__->has_many( + "sip_account_custom_patron_fields", + "Koha::Schema::Result::SipAccountCustomPatronField", + { "foreign.sip_account_id" => "self.sip_account_id" }, + { cascade_copy => 0, cascade_delete => 0 }, +); + =head2 sip_account_item_fields Type: has_many @@ -407,8 +422,8 @@ __PACKAGE__->belongs_to( ); -# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-01-31 16:01:39 -# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:4oEQ+Ijd2cbYHszQPLRldw +# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-02-04 13:31:16 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:UHTiGnqzk7zyAXgBlBUSxg __PACKAGE__->add_columns( diff --git a/Koha/Schema/Result/SipAccountCustomPatronField.pm b/Koha/Schema/Result/SipAccountCustomPatronField.pm new file mode 100644 index 0000000000..67d7b68b3c --- /dev/null +++ b/Koha/Schema/Result/SipAccountCustomPatronField.pm @@ -0,0 +1,123 @@ +use utf8; +package Koha::Schema::Result::SipAccountCustomPatronField; + +# Created by DBIx::Class::Schema::Loader +# DO NOT MODIFY THE FIRST PART OF THIS FILE + +=head1 NAME + +Koha::Schema::Result::SipAccountCustomPatronField + +=cut + +use strict; +use warnings; + +use base 'DBIx::Class::Core'; + +=head1 TABLE: C + +=cut + +__PACKAGE__->table("sip_account_custom_patron_fields"); + +=head1 ACCESSORS + +=head2 sip_account_custom_patron_field_id + + data_type: 'integer' + is_auto_increment: 1 + is_nullable: 0 + +=head2 sip_account_id + + data_type: 'integer' + is_foreign_key: 1 + is_nullable: 0 + +Foreign key to sip_accounts.sip_account_id + +=head2 field + + data_type: 'varchar' + is_nullable: 0 + size: 80 + +SIP field name e.g. XY + +=head2 template + + data_type: 'varchar' + is_nullable: 0 + size: 80 + +Template toolkit template + +=cut + +__PACKAGE__->add_columns( + "sip_account_custom_patron_field_id", + { data_type => "integer", is_auto_increment => 1, is_nullable => 0 }, + "sip_account_id", + { data_type => "integer", is_foreign_key => 1, is_nullable => 0 }, + "field", + { data_type => "varchar", is_nullable => 0, size => 80 }, + "template", + { data_type => "varchar", is_nullable => 0, size => 80 }, +); + +=head1 PRIMARY KEY + +=over 4 + +=item * L + +=back + +=cut + +__PACKAGE__->set_primary_key("sip_account_custom_patron_field_id"); + +=head1 UNIQUE CONSTRAINTS + +=head2 C + +=over 4 + +=item * L + +=item * L + +=back + +=cut + +__PACKAGE__->add_unique_constraint( + "sip_account", + ["sip_account_custom_patron_field_id", "sip_account_id"], +); + +=head1 RELATIONS + +=head2 sip_account + +Type: belongs_to + +Related object: L + +=cut + +__PACKAGE__->belongs_to( + "sip_account", + "Koha::Schema::Result::SipAccount", + { sip_account_id => "sip_account_id" }, + { is_deferrable => 1, on_delete => "CASCADE", on_update => "CASCADE" }, +); + + +# Created by DBIx::Class::Schema::Loader v0.07051 @ 2025-02-04 13:31:16 +# DO NOT MODIFY THIS OR ANYTHING ABOVE! md5sum:1mg3Ed2+AR3Hbzg89mN/NA + + +# You can replace this text with custom code or comments, and it will be preserved on regeneration +1; diff --git a/api/v1/swagger/definitions/sip2_account.yaml b/api/v1/swagger/definitions/sip2_account.yaml index 61a0919d08..b141f42111 100644 --- a/api/v1/swagger/definitions/sip2_account.yaml +++ b/api/v1/swagger/definitions/sip2_account.yaml @@ -199,6 +199,11 @@ properties: type: - array - 'null' + custom_patron_fields: + description: Related custom_patron_fields embed + type: + - array + - 'null' patron_attributes: description: Related patron_attributes embed type: diff --git a/api/v1/swagger/paths/sip2_accounts.yaml b/api/v1/swagger/paths/sip2_accounts.yaml index a2212d5619..144cb798d2 100644 --- a/api/v1/swagger/paths/sip2_accounts.yaml +++ b/api/v1/swagger/paths/sip2_accounts.yaml @@ -25,6 +25,7 @@ - institution - custom_item_fields - item_fields + - custom_patron_fields - patron_attributes collectionFormat: csv - $ref: "../swagger.yaml#/parameters/match" @@ -145,6 +146,7 @@ enum: - custom_item_fields - item_fields + - custom_patron_fields - patron_attributes collectionFormat: csv responses: diff --git a/installer/data/mysql/atomicupdate/bug_37893.pl b/installer/data/mysql/atomicupdate/bug_37893.pl index 75eb8e3333..07896b2e4c 100755 --- a/installer/data/mysql/atomicupdate/bug_37893.pl +++ b/installer/data/mysql/atomicupdate/bug_37893.pl @@ -159,6 +159,24 @@ return { my $new_account_id = $dbh->last_insert_id( undef, undef, "sip_accounts", "sip_account_id" ); + # Accounts custom patron fields + my @custom_patron_fields = + ref $SIPconfig->{accounts}->{$account_key}->{custom_patron_field} eq "ARRAY" + ? @{ $SIPconfig->{accounts}->{$account_key}->{custom_patron_field} } + : ( $SIPconfig->{accounts}->{$account_key}->{custom_patron_field} ); + + my $insert_custom_patron_fields = $dbh->prepare( + q{INSERT IGNORE INTO sip_account_custom_patron_fields (sip_account_id, field, template) VALUES (?, ?, ?)} + ); + + foreach my $custom_patron_field (@custom_patron_fields) { + $insert_custom_patron_fields->execute( + $new_account_id, + $custom_patron_field->{field}, + $custom_patron_field->{template} + ) if $custom_patron_field; + } + # Accounts patron attributes my @patron_attributes = ref $SIPconfig->{accounts}->{$account_key}->{patron_attribute} eq "ARRAY" @@ -180,10 +198,11 @@ return { my @custom_item_fields = ref $SIPconfig->{accounts}->{$account_key}->{custom_item_field} eq "ARRAY" ? @{ $SIPconfig->{accounts}->{$account_key}->{custom_item_field} } - : ( $SIPconfig->{accounts}->{$account_key}->{custom_item_field} ); + : ( $SIPconfig->{accounts}->{$account_key}->{custom_item_field} ); my $insert_custom_item_fields = $dbh->prepare( - q{INSERT IGNORE INTO sip_account_custom_item_fields (sip_account_id, field, template) VALUES (?, ?, ?)}); + q{INSERT IGNORE INTO sip_account_custom_item_fields (sip_account_id, field, template) VALUES (?, ?, ?)} + ); foreach my $custom_item_field (@custom_item_fields) { $insert_custom_item_fields->execute( diff --git a/installer/data/mysql/kohastructure.sql b/installer/data/mysql/kohastructure.sql index 6886988a64..c3e897ce03 100644 --- a/installer/data/mysql/kohastructure.sql +++ b/installer/data/mysql/kohastructure.sql @@ -6130,6 +6130,25 @@ CREATE TABLE `sip_accounts` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; /*!40101 SET character_set_client = @saved_cs_client */; +-- +-- Table structure for table `sip_account_custom_patron_fields` +-- + +DROP TABLE IF EXISTS `sip_account_custom_patron_fields`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `sip_account_custom_patron_fields` ( + `sip_account_custom_patron_field_id` int(11) NOT NULL AUTO_INCREMENT, + `sip_account_id` int(11) NOT NULL COMMENT 'Foreign key to sip_accounts.sip_account_id', + `field` varchar(80) NOT NULL COMMENT 'SIP field name e.g. XY', + `template` varchar(80) NOT NULL COMMENT 'Template toolkit template', + PRIMARY KEY (`sip_account_custom_patron_field_id`), + UNIQUE KEY `sip_account` (`sip_account_custom_patron_field_id`,`sip_account_id`), + KEY `sip_account_custom_patron_fields_ibfk_1` (`sip_account_id`), + CONSTRAINT `sip_account_custom_patron_fields_ibfk_1` FOREIGN KEY (`sip_account_id`) REFERENCES `sip_accounts` (`sip_account_id`) ON DELETE CASCADE ON UPDATE CASCADE +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; + -- -- Table structure for table `sip_account_patron_attributes` -- diff --git a/koha-tmpl/intranet-tmpl/prog/js/fetch/sip2-api-client.js b/koha-tmpl/intranet-tmpl/prog/js/fetch/sip2-api-client.js index 302377bc86..4cc74cc9ab 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/fetch/sip2-api-client.js +++ b/koha-tmpl/intranet-tmpl/prog/js/fetch/sip2-api-client.js @@ -49,7 +49,7 @@ export class SIP2APIClient { endpoint: "accounts/" + id, headers: { "x-koha-embed": - "custom_item_fields,item_fields,patron_attributes", + "custom_item_fields,item_fields,custom_patron_fields,patron_attributes", }, }), getAll: params => diff --git a/koha-tmpl/intranet-tmpl/prog/js/vue/components/SIP2/SIP2AccountResource.vue b/koha-tmpl/intranet-tmpl/prog/js/vue/components/SIP2/SIP2AccountResource.vue index 43d6e1d7c9..a762c5813f 100644 --- a/koha-tmpl/intranet-tmpl/prog/js/vue/components/SIP2/SIP2AccountResource.vue +++ b/koha-tmpl/intranet-tmpl/prog/js/vue/components/SIP2/SIP2AccountResource.vue @@ -392,6 +392,60 @@ export default { placeholder: "CRLF", group: "Details", }, + { + name: "custom_patron_fields", + type: "relationshipWidget", + showElement: { + type: "table", + columnData: "custom_patron_fields", + hidden: account => + !!account.custom_patron_fields?.length, + columns: [ + { + name: __("Field"), + value: "field", + }, + { + name: __("Template"), + value: "template", + }, + ], + }, + group: __("SIP response mappings"), + componentProps: { + resourceRelationships: { + resourceProperty: "custom_patron_fields", + }, + relationshipStrings: { + nameLowerCase: __("custom patron field"), + nameUpperCase: __("Custom patron field"), + namePlural: __("custom patron fields"), + }, + newRelationshipDefaultAttrs: { + type: "object", + value: { + field: null, + template: null, + }, + }, + }, + relationshipFields: [ + { + name: "field", + required: true, + type: "text", + placeholder: "XY", + label: __("Field"), + }, + { + name: "template", + required: true, + type: "text", + placeholder: "[% patron.dateexpiry %]", + label: __("Template"), + }, + ], + }, { name: "patron_attributes", type: "relationshipWidget",