Bug 37893: SIP accounts custom patron fields

Signed-off-by: Andrew Fuerste Henry <andrew@bywatersolutions.com>
Signed-off-by: Lisette Scheer <lisette@bywatersolutions.com>
Signed-off-by: Martin Renvoize <martin.renvoize@openfifth.co.uk>
Signed-off-by: Lucas Gass <lucas@bywatersolutions.com>
This commit is contained in:
Pedro Amorim 2025-02-04 13:31:57 +00:00 committed by Lucas Gass
parent 36808bccb2
commit 15944a017b
No known key found for this signature in database
GPG Key ID: 60B447398B0D36CA
12 changed files with 396 additions and 11 deletions

View File

@ -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 );

View File

@ -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

View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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 <http://www.gnu.org/licenses>.
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;

View File

@ -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<Koha::Schema::Result::SipAccountCustomPatronField>
=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(

View File

@ -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<sip_account_custom_patron_fields>
=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</sip_account_custom_patron_field_id>
=back
=cut
__PACKAGE__->set_primary_key("sip_account_custom_patron_field_id");
=head1 UNIQUE CONSTRAINTS
=head2 C<sip_account>
=over 4
=item * L</sip_account_custom_patron_field_id>
=item * L</sip_account_id>
=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<Koha::Schema::Result::SipAccount>
=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;

View File

@ -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:

View File

@ -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:

View File

@ -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(

View File

@ -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`
--

View File

@ -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 =>

View File

@ -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",