FFZG: add ISSP RFID sid API 2023-10-16-rfid mjesec/2023-10-16-rfid
authorDobrica Pavlinusic <dpavlin@rot13.org>
Mon, 6 Nov 2023 17:00:00 +0000 (18:00 +0100)
committerDobrica Pavlinusic <dpavlin@rot13.org>
Tue, 7 Nov 2023 08:37:34 +0000 (09:37 +0100)
ffzg/rfid/borrower.pl

index fefe37c..4b13df3 100755 (executable)
 # with Koha; if not, write to the Free Software Foundation, Inc.,
 # 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 
+# perl -I/srv/koha_ffzg/ ./borrower-issi.pl 'RFID_SID=0492188A741590&JMBAG='
+
 use strict;
 use warnings;
 
 use CGI;
 use C4::Context;
 use JSON;
+use LWP::UserAgent qw();
+use Data::Dump qw(dump); # FIXME
+
+
+my $cert_password;
+{
+       open(my $fh, '<', '/home/dpavlin/certifikat_za_pristup_apiv2/lozinka.txt');
+       local $/ = undef;
+       $cert_password = <$fh>;
+       close($fh);
+}
 
 my $query = new CGI;
 print "Content-type: application/json; charset=utf-8\r\n\r\n";
@@ -41,6 +54,8 @@ foreach my $name (qw( OIB JMBAG RFID_SID )) {
        }
 }
 
+sub generate_sql {
+       my @where = @_;
 my $sql = qq{
 select
        distinct
@@ -49,6 +64,10 @@ from borrower_attributes ba
 join borrowers b on b.borrowernumber = ba.borrowernumber
 where (} . join(') or (', @where) . qq{)};
 
+       return $sql;
+}
+
+my $sql = generate_sql @where;
 warn "# sql $sql\n";
 
 my $sth = $dbh->prepare( $sql );
@@ -57,7 +76,56 @@ $sth->execute( @execute );
 $json->{rows} = $sth->rows;
 
 if ( $sth->rows < 1 ) {
-       $json->{error} = "borrower not found";
+       if ( exists $json->{param}->{RFID_SID} ) {
+               my $sid = $json->{param}->{RFID_SID};
+               my $l = length($sid);
+               if ( $l == 19 or $l == 14 or $l = 8 ) {
+                       warn "ISSP $sid\n";
+
+                       my $ua = LWP::UserAgent->new (
+                       ssl_opts => {
+                         SSL_cert_file => '/home/dpavlin/certifikat_za_pristup_apiv2/certifikat.pfx',
+                         SSL_passwd_cb => sub { $cert_password },
+                       }
+                       );
+                       my $response = $ua->get ("https://isspapi.issp.srce.hr/Kartice/oibjmbag/$sid", Accept => "application/json");
+
+                       if ($response->is_success) {
+                               my $json_text = $response->decoded_content;
+                               my $h = decode_json $json_text;
+
+                               @where = ();
+                               @execute = ();
+
+                               foreach my $f ( keys %$h ) {
+                                       my $name = uc($f);
+                                       push @where, "( code = '$name' and attribute = ? )";
+                                       push @execute, $h->{$f};
+                               }
+                               my $sql = generate_sql @where;
+                               warn "ISSP sql: $sql ",dump(@execute);
+                               my $sth = $dbh->prepare( $sql );
+                               $sth->execute( @execute );
+                               if ( $sth->rows == 1 ) {
+                                       $json->{borrower} = $sth->fetchrow_hashref;
+                                       warn "borrower = ",dump( $json->{borrower} );
+                                       my $sth2 = $dbh->prepare(qq{ insert into borrower_attributes (borrowernumber, code, attribute) values (?,?,?) });
+                                       $sth2->execute( $json->{borrower}->{borrowernumber}, 'RFID_SID', $sid);
+                                       warn "ISSP updated $json->{borrower}->{borrowernumber} $sid";
+                               } else {
+                                       warn "ISSP $sid not found in koha $json_text";
+                                       $json->{error} = "borrower not found";
+                               }
+                       } else {
+                               warn "ERROR ISSP ", $response->status_line;
+                               $json->{error} = "borrower not found";
+                       }
+               } else {
+                       $json->{error} = "borrower not found";
+               }
+       } else {
+               $json->{error} = "borrower not found";
+       }
 } elsif ( $sth->rows > 1 ) {
        $json->{error} = "more than one borrower found";
 } else {