[Rt-commit] rt branch, 5.0/ticket-id-in-attachment-search, created. rt-5.0.0-23-g5e19afbf80

Aaron Trevena ast at bestpractical.com
Wed Sep 23 07:25:36 EDT 2020


The branch, 5.0/ticket-id-in-attachment-search has been created
        at  5e19afbf80b24782f14c663c8d7c9dc84d90323d (commit)

- Log -----------------------------------------------------------------
commit 69528338a442082b9a660a501278307664352656
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Wed Sep 23 11:56:01 2020 +0100

    Add support for searching Attachment by ticket in REST2

diff --git a/lib/RT/REST2.pm b/lib/RT/REST2.pm
index 1079f95cd6..e8d0fb2fd2 100644
--- a/lib/RT/REST2.pm
+++ b/lib/RT/REST2.pm
@@ -530,6 +530,11 @@ Below are some examples using the endpoints above.
     -d '[{ "field" : "id", "operator" : ">=", "value" : 0 }]'
     'https://myrt.com/REST/2.0/asset'
 
+    # Search Attachments by ticket
+    curl -X POST -H "Content-Type: application/json" -u 'root:password'
+    -d '[{ "field": "ContentType", "operator": "=", "value": "image/png" }, { "field": "TicketId", "value": 6 } ]'
+    'https://myrt.com/REST/2.0/attachments'
+
 =head3 Transactions
 
     GET /transactions?query=<JSON>
diff --git a/lib/RT/REST2/Resource/Attachments.pm b/lib/RT/REST2/Resource/Attachments.pm
index d098aaa01a..666df0a260 100644
--- a/lib/RT/REST2/Resource/Attachments.pm
+++ b/lib/RT/REST2/Resource/Attachments.pm
@@ -72,6 +72,49 @@ sub dispatch_rules {
     )
 }
 
+around 'limit_collection' => sub {
+    my $orig = shift;
+    my $self = shift;
+
+    if (my $ticket_criteria = $self->_query_field('TicketId')) {
+        my $collection = $self->collection;
+        my $tx_alias = $collection->Join(
+            TYPE   => 'LEFT',
+            ALIAS1 => 'main',
+            FIELD1 => 'TransactionId',
+            TABLE2 => 'Transactions',
+            FIELD2 => 'id',
+        );
+
+        $collection->Limit(
+            ALIAS    => $tx_alias,
+            FIELD    => 'ObjectType',
+            OPERATOR => '=',
+            VALUE    => 'RT::Ticket'
+        );
+
+        $collection->Limit(
+            ALIAS    => $tx_alias,
+            FIELD    => 'ObjectId',
+            OPERATOR => $ticket_criteria->{operator} // '=',
+            VALUE    => $ticket_criteria->{value},
+        );
+    }
+
+    return $self->$orig;
+};
+
+sub _query_field {
+    my ($self, $field) = @_;
+    foreach my $condition( @{$self->query} ) {
+        if ($condition->{field} eq $field) {
+            return $condition;
+        }
+    }
+    return undef;
+}
+
+
 __PACKAGE__->meta->make_immutable;
 
 1;

commit 5e19afbf80b24782f14c663c8d7c9dc84d90323d
Author: Aaron Trevena <ast at bestpractical.com>
Date:   Wed Sep 23 12:03:54 2020 +0100

    Add test for searching attachments by ticket in REST2 api

diff --git a/t/rest2/attachments.t b/t/rest2/attachments.t
index d7aa05fac7..be0dd276dc 100644
--- a/t/rest2/attachments.t
+++ b/t/rest2/attachments.t
@@ -256,4 +256,26 @@ my $json = JSON->new->utf8;
     ok(!$attachments[2]->Subject);
 }
 
+
+# Search attachments
+{
+    my $res = $mech->post_json("$rest_base_path/attachments",
+        [
+            { field => 'ContentType', operator => '=', value => 'image/png' },
+            { field => 'TicketId', value => $ticket_id },
+        ],
+        'Authorization' => $auth,
+    );
+
+    my $content = $mech->json_response;
+    is($content->{count}, 4);
+    is($content->{page}, 1);
+    is($content->{per_page}, 20);
+    is($content->{total}, 4);
+    is($content->{items}[0]{type}, 'attachment');
+    is(scalar @{$content->{items}}, 4);
+}
+
+
+
 done_testing;

-----------------------------------------------------------------------


More information about the rt-commit mailing list