From 4ccdd16c4e11da84d0c936ab3871f8594f925568 Mon Sep 17 00:00:00 2001 From: Jason Etheridge Date: Tue, 12 Jul 2022 00:00:11 -0400 Subject: [PATCH] toward filters --- .../src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm | 39 +++++++++++++++++++++- 1 file changed, 38 insertions(+), 1 deletion(-) diff --git a/Open-ILS/src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm b/Open-ILS/src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm index e58d19cf67..c811980fe7 100644 --- a/Open-ILS/src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm +++ b/Open-ILS/src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm @@ -89,8 +89,45 @@ sub handler { } $r->content_type('application/json'); - $r->print($json->encode($response)); + # response = $VAR1 = {'fields' => [{'AO' => 'example'},{'BX' => 'YYYNYNYYNYYNNNYN'}],'fixed_fields' => ['Y','Y','Y','Y','N','N','999','999','20220706 154418','2.00'],'code' => '98'}; + # filters v1 = $VAR1 = { 'field' => { 'identifier' => 'AE', 'replace_with' => 'John Doe' } }; + # filters v1 = $VAR1 = { 'field' => { 'identifier' => 'AE', 'remove' => 'true' } }; + # filters v1 = $VAR1 = { 'field' => [ { 'identifier' => 'AE', 'replace_with' => 'John Doe' }, { 'replace_with' => 'Jane Doe', 'identifier' => 'AE' } ] }; + my $filters = { 'field' => [ { 'identifier' => 'AE', 'replace_with' => 'John Doe' }, { 'replace_with' => 'Jane Doe', 'identifier' => 'AE' } ] }; + + sub find_field_config { + my $field_id = shift; + my @relavent_field_configs = grep { $_->{identifier} eq $field_id } @{ $filters->{'field'} }; + # since we can't do anything complicated yet, let's just return the first match + return @relavent_field_configs ? $relavent_field_configs[0] : undef; + } + if (defined $filters && defined $response->{fields} && ref $response->{fields} eq 'ARRAY') { + $response->{fields} = [ + grep { + my $keep = 1; + my @fids = keys(%{$_}); + my $fid = $fids[0]; + my $field_config = find_field_config( $fid ); + if ($field_config && defined $field_config->{remove}) { + if (($field_config->{remove} =~ /true|y|yes/i)) { # test truthiness + $keep = 0; # strip the entire field + } + } + $keep; # or not + } + map { + my @fids = keys(%{$_}); + my $fid = $fids[0]; + my $field_config = find_field_config( $fid ); + $field_config && defined $field_config->{replace_with} + ? { $fid => $field_config->{replace_with} } + : $_; + } + @{ $response->{fields} } + ]; + } + $r->print($json->encode($response)); return Apache2::Const::OK; } -- 2.11.0