[Bps-public-commit] rt-extension-rest2 branch, transaction-cfs, created. 1.07-1-g6c318c3

Jim Brandt jbrandt at bestpractical.com
Wed Aug 7 16:10:09 EDT 2019


The branch, transaction-cfs has been created
        at  6c318c3a48259c947046fd0298638a030e223c54 (commit)

- Log -----------------------------------------------------------------
commit 6c318c3a48259c947046fd0298638a030e223c54
Author: michel <michel at bestpractical.com>
Date:   Tue Jul 30 23:48:47 2019 +0200

    Added Transaction Custom Field updates on Correspond and Comment

diff --git a/README b/README
index 40138e3..cbab6db 100644
--- a/README
+++ b/README
@@ -275,6 +275,13 @@ USAGE
             -d '{ "Content": "Testing a correspondence", "ContentType": "text/plain" }'
             'https://myrt.com/REST/2.0/ticket/6/correspond'
 
+        # Correspond a ticket with a transaction custom field
+        curl -X POST -H "Content-Type: application/json" -u 'root:password'
+            -d '{ "Content": "Testing a correspondence", "ContentType": "text/plain",
+                  "TxnCustomFields": {"MyField": "custom field value"}
+                }'
+            'https://myrt.com/REST/2.0/ticket/6/correspond'
+
         # Comment a ticket
         curl -X POST -H "Content-Type: text/plain" -u 'root:password'
             -d 'Testing a comment'
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index c4b61c6..195d883 100644
--- a/lib/RT/Extension/REST2.pm
+++ b/lib/RT/Extension/REST2.pm
@@ -311,6 +311,13 @@ Below are some examples using the endpoints above.
         -d '{ "Content": "Testing a correspondence", "ContentType": "text/plain" }'
         'https://myrt.com/REST/2.0/ticket/6/correspond'
 
+    # Correspond a ticket with a transaction custom field
+    curl -X POST -H "Content-Type: application/json" -u 'root:password'
+        -d '{ "Content": "Testing a correspondence", "ContentType": "text/plain",
+              "TxnCustomFields": {"MyField": "custom field value"}
+            }'
+        'https://myrt.com/REST/2.0/ticket/6/correspond'
+
     # Comment a ticket
     curl -X POST -H "Content-Type: text/plain" -u 'root:password'
         -d 'Testing a comment'
diff --git a/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index 57c8ef1..62cf377 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -69,6 +69,15 @@ sub add_message {
         Subject   => $args{Subject},
     );
 
+    # we use $txn_ret and $txn_msg so we don't interfere with the main $ret and $msg
+    # see also comment below on the call to UpdateCustomFields
+    my ( $txn_ret, $txn_msg, $TxnCustomFields ) = $self->_massage_txn_custom_fields(
+        $args{TxnCustomFields} || $args{TransactionCustomFields} );
+
+    if ( ! $txn_ret ) {
+        return error_as_json( $self->response, \400, $txn_msg );
+    }
+
     my ( $Trans, $msg, $TransObj ) ;
 
     if ($self->type eq 'correspond') {
@@ -93,12 +102,85 @@ sub add_message {
             \400, $msg || "Message failed for unknown reason");
     }
 
+    my ( $update_ret, $update_msg ) = $self->_update_txn_custom_fields( $TransObj, $TxnCustomFields );
+    $msg .= " - Error: transaction custom fields not updated" unless $update_ret;
+
     $self->created_transaction($TransObj);
     $self->response->body(JSON::to_json([$msg], { pretty => 1 }));
 
     return 1;
 }
 
+# takes a TxnCustomFields (or TransactionCustomFields) argument in
+# returns 
+#     a status code, 
+#    a message (in case of error)
+#     a hashref where the CF names are translated into the full
+#        form Object-RT::Transaction--CustomField-<id> that can
+#        be used by UpdateCustomFields
+#        the value is undef if no argument was passed
+sub _massage_txn_custom_fields {
+    my $self = shift;
+    my $txn_cf_by_name = shift;
+
+    # if there are no transaction custom fields we're good
+    if( ! $txn_cf_by_name ) {
+        return ( 1, '', undef);
+    }
+
+    my $user = $self->request->env->{"rt.current_user"};
+
+    # we need the queue to get the transaction custom fields that can apply to it
+    my $queue_obj = $self->record->QueueObj;
+
+    # Pre-check that the user can actually update the CFs
+    unless ( $queue_obj->CurrentUserHasRight('ModifyCustomField') ) {
+        RT->Logger->error( "Permission denied: " . $user->Name . " cannot modify transaction custom fields");
+        return ( 0, "Permission denied", undef );
+    }
+
+    # generate a hash suitable for UpdateCustomFields
+    # ie the keys are the "full names" of the custom fields 
+    my %txn_custom_fields;
+    foreach my $cf_name ( %{$txn_cf_by_name} ) {
+        my $cf_obj = $queue_obj->LoadCustomFieldByIdentifier($cf_name);
+
+        unless ( $cf_obj and $cf_obj->Id ) {
+            RT->Logger->error( "Unable to load transaction custom field: $cf_name" );
+            return ( 0, "Unable to load transaction custom field: $cf_name", undef );
+        }
+
+        my $txn_input_name = RT::Interface::Web::GetCustomFieldInputNamePrefix(
+                             Object      => $queue_obj,
+                             CustomField => $cf_obj,
+        );
+
+        $txn_custom_fields{$txn_input_name} = $txn_cf_by_name->{$cf_name};
+    }
+
+    return ( 1, "Custom fields validated", \%txn_custom_fields );
+}
+
+sub _update_txn_custom_fields {
+    my $self = shift;
+    my $TransObj = shift;
+    my $TxnCustomFields = shift;
+
+    my ( $txn_ret, $txn_msg );
+    if ( keys %$TxnCustomFields ) {
+        ( $txn_ret, $txn_msg ) = $TransObj->UpdateCustomFields( %$TxnCustomFields );
+
+        if ( !$txn_ret ) {
+            # the correspond/comment is already a success, the mails have been sent
+            # so we can't return an error here
+            RT->Logger->error( "Could not update transaction custom fields: $txn_msg" );
+            return ( 0, $txn_msg );
+        }
+    }
+
+    return ( 1, $txn_msg );
+}
+
 sub create_path {
     my $self = shift;
     my $id = $self->created_transaction->Id;

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


More information about the Bps-public-commit mailing list