toward filters
authorJason Etheridge <jason@EquinoxOLI.org>
Tue, 12 Jul 2022 04:00:11 +0000 (00:00 -0400)
committerJason Etheridge <jason@EquinoxOLI.org>
Tue, 12 Jul 2022 04:00:11 +0000 (00:00 -0400)
Open-ILS/src/perlmods/lib/OpenILS/WWW/SIP2Mediator.pm

index e58d19c..c811980 100644 (file)
@@ -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;
 }