Bug 17755: Introduce Koha::Patron::Attribute::Type(s)
[srvgit] / Koha / Patron / Modification.pm
1 package Koha::Patron::Modification;
2
3 # Copyright ByWater Solutions 2014
4 #
5 # This file is part of Koha.
6 #
7 # Koha is free software; you can redistribute it and/or modify it under the
8 # terms of the GNU General Public License as published by the Free Software
9 # Foundation; either version 3 of the License, or (at your option) any later
10 # version.
11 #
12 # Koha is distributed in the hope that it will be useful, but WITHOUT ANY
13 # WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
14 # A PARTICULAR PURPOSE.  See the GNU General Public License for more details.
15 #
16 # You should have received a copy of the GNU General Public License along
17 # with Koha; if not, write to the Free Software Foundation, Inc.,
18 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
19
20 use Modern::Perl;
21
22 use Carp;
23
24 use Koha::Database;
25 use Koha::Exceptions::Patron::Modification;
26 use Koha::Patron::Modifications;
27 # TODO: Remove once Koha::Patron::Attribute(s) is implemented
28 use C4::Members::Attributes qw( SetBorrowerAttributes );
29
30 use JSON;
31 use Try::Tiny;
32
33 use base qw(Koha::Object);
34
35 =head1 NAME
36
37 Koha::Patron::Modification - Class represents a request to modify or create a patron
38
39 =head2 Class Methods
40
41 =cut
42
43 =head2 store
44
45 =cut
46
47 sub store {
48     my ($self) = @_;
49
50     if ( $self->verification_token ) {
51         if (Koha::Patron::Modifications->search(
52                 { verification_token => $self->verification_token }
53             )->count()
54             )
55         {
56             Koha::Exceptions::Patron::Modification::DuplicateVerificationToken
57                 ->throw(
58                 "Duplicate verification token " . $self->verification_token );
59         }
60     }
61
62     if ( $self->extended_attributes ) {
63         try {
64             my $json_parser = JSON->new;
65             $json_parser->decode( $self->extended_attributes );
66         }
67         catch {
68             Koha::Exceptions::Patron::Modification::InvalidData->throw(
69                 'The passed extended_attributes is not valid JSON');
70         };
71     }
72
73     return $self->SUPER::store();
74 }
75
76
77 =head2 approve
78
79 $m->approve();
80
81 Commits the pending modifications to the borrower record and removes
82 them from the modifications table.
83
84 =cut
85
86 sub approve {
87     my ($self) = @_;
88
89     my $data = $self->unblessed();
90
91     delete $data->{timestamp};
92     delete $data->{verification_token};
93     delete $data->{extended_attributes};
94
95     foreach my $key ( keys %$data ) {
96         delete $data->{$key} unless ( defined( $data->{$key} ) );
97     }
98
99     my $patron = Koha::Patrons->find( $self->borrowernumber );
100
101     return unless $patron;
102
103     $patron->set($data);
104
105     # Take care of extended attributes
106     if ( $self->extended_attributes ) {
107         our $extended_attributes
108             = try { decode_json( $self->extended_attributes ) }
109         catch {
110             Koha::Exceptions::Patron::Modification::InvalidData->throw(
111                 'The passed extended_attributes is not valid JSON');
112         };
113     }
114
115     $self->_result->result_source->schema->txn_do(
116         sub {
117             try {
118                 $patron->store();
119
120                 # Take care of extended attributes
121                 if ( $self->extended_attributes ) {
122                     my $extended_attributes
123                         = decode_json( $self->extended_attributes );
124                     SetBorrowerAttributes( $patron->borrowernumber,
125                         $extended_attributes );
126                 }
127             }
128             catch {
129                 if ( $_->isa('DBIx::Class::Exception') ) {
130                     Koha::Exceptions::Patron::Modification->throw(
131                         $_->{msg} );
132                 }
133                 else {
134                     Koha::Exceptions::Patron::Modification->throw($@);
135                 }
136             };
137         }
138     );
139
140     return $self->delete();
141 }
142
143
144
145
146 =head3 type
147
148 =cut
149
150 sub _type {
151     return 'BorrowerModification';
152 }
153
154 =head1 AUTHOR
155
156 Kyle M Hall <kyle@bywatersolutions.com>
157
158 =cut
159
160 1;