From 2a9c8147f4685321407f67686859bc8a40710b86 Mon Sep 17 00:00:00 2001 From: Dobrica Pavlinusic Date: Mon, 6 Nov 2023 18:00:00 +0100 Subject: [PATCH] FFZG: add ISSP RFID sid API --- ffzg/rfid/borrower.pl | 70 ++++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 69 insertions(+), 1 deletion(-) diff --git a/ffzg/rfid/borrower.pl b/ffzg/rfid/borrower.pl index fefe37c040..4b13df3356 100755 --- a/ffzg/rfid/borrower.pl +++ b/ffzg/rfid/borrower.pl @@ -17,12 +17,25 @@ # 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 { -- 2.11.0