[Bps-public-commit] rt-extension-rest2 02/05: Add /tickets/bulk/correspond and /tickets/bulk/comment endpoints

sunnavy sunnavy at bestpractical.com
Fri Jul 16 19:39:47 UTC 2021


This is an automated email from the git hooks/post-receive script.

jbrandt pushed a commit to branch tickets-bulk-message
in repository rt-extension-rest2.

commit ac978561ef14b8907973f6ec7c6fba1ebfb519c7
Author: sunnavy <sunnavy at bestpractical.com>
AuthorDate: Fri Jun 18 02:24:40 2021 +0800

    Add /tickets/bulk/correspond and /tickets/bulk/comment endpoints
---
 lib/RT/Extension/REST2/Resource/TicketsBulk.pm | 72 ++++++++++++++++++++++----
 1 file changed, 62 insertions(+), 10 deletions(-)

diff --git a/lib/RT/Extension/REST2/Resource/TicketsBulk.pm b/lib/RT/Extension/REST2/Resource/TicketsBulk.pm
index c9f1f2a..8194c96 100644
--- a/lib/RT/Extension/REST2/Resource/TicketsBulk.pm
+++ b/lib/RT/Extension/REST2/Resource/TicketsBulk.pm
@@ -14,9 +14,18 @@ use RT::Extension::REST2::Resource::Ticket;
 use JSON ();
 
 sub dispatch_rules {
-    Path::Dispatcher::Rule::Regex->new( regex => qr{^/tickets/bulk/?$} );
+    Path::Dispatcher::Rule::Regex->new( regex => qr{^/tickets/bulk/?$} ),
+    Path::Dispatcher::Rule::Regex->new(
+        regex => qr{^/tickets/bulk/(correspond|comment)$},
+        block => sub { { type => shift->pos(1) } },
+    )
 }
 
+has type => (
+    is       => 'ro',
+    isa      => 'Str',
+);
+
 sub post_is_create    { 1 }
 sub create_path       { '/tickets/bulk' }
 sub charsets_provided { [ 'utf-8' ] }
@@ -52,17 +61,60 @@ sub from_json {
     }
     else {
         for my $param ( @$params ) {
-            my $resource = RT::Extension::REST2::Resource::Ticket->new(
-                request      => $self->request,
-                response     => $self->response,
-                record_class => 'RT::Ticket',
-            );
-            my ( $ok, $msg ) = $resource->create_record( $param );
-            if ( ref( $ok ) || !$ok ) {
-                push @results, { message => $msg || "Create failed for unknown reason" };
+            if ( $self->type ) {
+                my $id = delete $param->{id};
+                if ( $id && $id =~ /^\d+$/ ) {
+                    my $ticket = RT::Ticket->new($self->current_user);
+                    $ticket->Load($id);
+                    my $resource = RT::Extension::REST2::Resource::Message->new(
+                        request      => $self->request,
+                        response     => $self->response,
+                        type         => $self->type,
+                        record       => $ticket,
+                    );
+
+                    my @errors;
+
+                    # Ported from RT::Extension::REST2::Resource::Message::from_json
+                    if ( $param->{Attachments} ) {
+                        foreach my $attachment ( @{ $param->{Attachments} } ) {
+                            foreach my $field ( 'FileName', 'FileType', 'FileContent' ) {
+                                push @errors, "$field is a required field for each attachment in Attachments"
+                                    unless $attachment->{$field};
+                            }
+                        }
+                    }
+
+                    $param->{NoContent} = 1 unless $param->{Content};
+                    if ( !$param->{NoContent} && !$param->{ContentType} ) {
+                        push @errors, "ContentType is a required field for application/json";
+                    }
+
+                    if (@errors) {
+                        push @results, [ $id, @errors ];
+                        next;
+                    }
+
+                    my ( $return_code, @messages ) = $resource->_add_message(%$param);
+                    push @results, [ $id, @messages ];
+                }
+                else {
+                    push @results, [ $id, 'Resource does not exist' ];
+                }
             }
             else {
-                push @results, expand_uid( $resource->record->UID );
+                my $resource = RT::Extension::REST2::Resource::Ticket->new(
+                    request      => $self->request,
+                    response     => $self->response,
+                    record_class => 'RT::Ticket',
+                );
+                my ( $ok, $msg ) = $resource->create_record($param);
+                if ( ref($ok) || !$ok ) {
+                    push @results, { message => $msg || "Create failed for unknown reason" };
+                }
+                else {
+                    push @results, expand_uid( $resource->record->UID );
+                }
             }
         }
     }

-- 
To stop receiving notification emails like this one, please contact
sysadmin at bestpractical.com.


More information about the Bps-public-commit mailing list