webstaff: implement fetcher for pattern templates
authorGalen Charlton <gmc@equinoxinitiative.org>
Thu, 11 May 2017 21:26:43 +0000 (17:26 -0400)
committerGalen Charlton <gmc@equinoxinitiative.org>
Tue, 30 May 2017 16:06:43 +0000 (12:06 -0400)
The method open-ils.serial.pattern_template.retrieve.at
takes a org unit ID and returns the set of patterns visible
to it.

Signed-off-by: Galen Charlton <gmc@equinoxinitiative.org>
Open-ILS/src/perlmods/lib/OpenILS/Application/Serial.pm
Open-ILS/src/sql/Pg/upgrade/XXXX.schema.serial_pattern_templates.sql

index 50113eb..75f915a 100644 (file)
@@ -3894,4 +3894,40 @@ sub summary_test {
     return;
 }
 
+__PACKAGE__->register_method(
+    "method" => "fetch_pattern_templates",
+    "api_name" => "open-ils.serial.pattern_template.retrieve.at",
+    "stream" => 1,
+    "signature" => {
+        "desc" => q{Return the set of pattern templates that are
+            visible to the specified library.},
+        "params" => [
+            {"desc" => "Authtoken", "type" => "string"},
+            {"desc" => "OU ID", "type" => "number"},
+        ],
+        return => {
+            desc => "stream of pattern templates",
+            type => "object", class => "spt"
+        }
+    }
+);
+
+sub fetch_pattern_templates {
+    my ($self, $client, $auth, $org_unit)  = @_;
+
+    my $e = new_editor("authtoken" => $auth);
+    return $e->die_event unless $e->checkauth;
+
+    my $patterns = $e->json_query({
+        from => [ 'serial.pattern_templates_visible_to' => $org_unit ]
+    });
+$logger->info(Dumper($patterns)); use Data::Dumper;
+
+    $client->respond($e->retrieve_serial_pattern_template($_->{id}))
+        foreach (@$patterns);
+
+    $e->disconnect;
+    return undef;
+}
+
 1;
index 3a8ed1a..3921c35 100644 (file)
@@ -11,4 +11,15 @@ CREATE TABLE serial.pattern_template (
 );
 CREATE INDEX serial_pattern_template_name_idx ON serial.pattern_template (evergreen.lowercase(name));
 
+CREATE OR REPLACE FUNCTION serial.pattern_templates_visible_to(org_unit INT) RETURNS SETOF serial.pattern_template AS $func$
+BEGIN
+    RETURN QUERY SELECT *
+           FROM serial.pattern_template spt
+           WHERE (
+             SELECT ARRAY_AGG(id)
+             FROM actor.org_unit_descendants(spt.owning_lib, spt.share_depth)
+           ) @@ org_unit::TEXT::QUERY_INT;
+END;
+$func$ LANGUAGE PLPGSQL;
+
 -- COMMIT;