[Bps-public-commit] rt-extension-rest2 branch, transaction-custom-fields, updated. 1.07-3-gc34ef86

Michel Rodriguez michel at bestpractical.com
Fri Aug 2 08:14:01 EDT 2019


The branch, transaction-custom-fields has been updated
       via  c34ef86218cf13f2491dacf9d1cc4b67b6b1e007 (commit)
      from  11576d064d4fe660ed23d86d19331a52e3860808 (commit)

Summary of changes:
 lib/RT/Extension/REST2/Resource/Message.pm | 68 +++++++++++++++++++++++++-----
 1 file changed, 58 insertions(+), 10 deletions(-)

- Log -----------------------------------------------------------------
commit c34ef86218cf13f2491dacf9d1cc4b67b6b1e007
Author: michel <michel at bestpractical.com>
Date:   Fri Aug 2 14:13:44 2019 +0200

    Added pre-processing of arguments to allow transaction custom field names to be used, instead of the full name

diff --git a/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index d118334..dd7a634 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -93,16 +93,17 @@ sub add_message {
             \400, $msg || "Message failed for unknown reason");
     }
 
-    my @transaction_custom_field_list = grep { m{^Object-RT::Transaction--CustomField} } keys %{$args{CustomFields}};
-    if( @transaction_custom_field_list ) {
-        my %transaction_custom_fields = map { $_ => $args{CustomFields}->{$_} }  @transaction_custom_field_list;
-        my( $ret, $msg) = $TransObj->UpdateCustomFields( %transaction_custom_fields);
-        if( ! $ret ) {
-            RT->Logger->error( "cannot update transaction custom fields: $msg");
-            return error_as_json(
-                $self->response,
-                \400, $msg || "Transaction Custom Fields update failed");
-        }
+
+    # transaction custom fields can be either in TxnCustomFields or in TransactionCustomFields
+    # $msg is the result that will be returned, so we use $txn_msg in order not to clobber it 
+    my( $txn_ret, $txn_msg)= $self->_update_txn_custom_fields( 
+        TransObj => $TransObj, 
+        TxnCustomFields => $args{TxnCustomFields} || $args{TransactionCustomFields},
+      );
+    if (!$txn_ret) {
+        return error_as_json(
+            $self->response,
+            \400, $msg || "Could not update transaction custom fields");
     }
 
     $self->created_transaction($TransObj);
@@ -111,6 +112,53 @@ sub add_message {
     return 1;
 }
 
+sub _update_txn_custom_fields {
+    my $self = shift;
+    my %args = @_;
+
+    return 1 if ! $args{TxnCustomFields};
+
+    # we need the queue to get the transaction custom fields that can apply to it
+    my $queue= RT::Queue->new( $self->request->env->{"rt.current_user"} );
+    my( $ret, $msg)= $queue->Load( $self->record->Queue);
+    if( ! $ret ) {
+        RT->Logger->error( "cannot find ticket queue: $msg");
+        return ( 0, $msg || "Cannot find ticket queue" );
+    }
+
+    # build a hash <custom_field_name> => <custom_field_id>
+    my %txn_cf_name_to_id;
+    my $cfs = $queue->TicketTransactionCustomFields;
+    while( my $cf= $cfs->Next ) {
+         $txn_cf_name_to_id{$cf->Name} = $cf->Id;
+         # also allow the full name  
+         my $full_name = "Object-RT::Transaction--CustomField-" . $cf_id;
+         $txn_cf_name_to_id{$full_name} = $cf->Id; 
+    }
+
+    # generate a hash suitable for UpdateCustomFields
+    # ie the keys are the "full names" of the custom fields 
+    my %txn_custom_fields;
+    my $txn_cf_by_name = $args{TxnCustomFields};
+    foreach my $cf_name ( keys %$txn_cf_by_name) {
+        my $cf_id = $txn_cf_name_to_id{$cf_name};
+        if( ! $cf_id ) {
+            RT->Logger->error ( "unknown transaction custom field: $cf_name");
+            return ( 0, "unknown transaction custom field: $cf_name");
+        }
+        my $cf_full_name = "Object-RT::Transaction--CustomField-$cf_id";
+        $txn_custom_fields{$cf_full_name} = $txn_cf_by_name->{$cf_name};
+    }
+
+    ( $ret, $msg) = $args{TransObj}->UpdateCustomFields( %txn_custom_fields);
+    if( ! $ret ) {
+        RT->Logger->error( "cannot update transaction custom fields: $msg");
+        return ( 0, $msg || "Transaction Custom Fields update failed");
+    }
+
+    return 1;
+}
+
 sub create_path {
     my $self = shift;
     my $id = $self->created_transaction->Id;

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


More information about the Bps-public-commit mailing list