[Bps-public-commit] rt-extension-rest2 branch, transaction-custom-fields, created. 1.07-3-gd2deaea
Michel Rodriguez
michel at bestpractical.com
Fri Aug 2 08:22:26 EDT 2019
The branch, transaction-custom-fields has been created
at d2deaea895e28d0e5f11d84d68e7142752cc4009 (commit)
- Log -----------------------------------------------------------------
commit 5a50b0a64fbc7eb5cc56f7a343db6174a3ecfcb6
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/lib/RT/Extension/REST2/Resource/Message.pm b/lib/RT/Extension/REST2/Resource/Message.pm
index 57c8ef1..d118334 100644
--- a/lib/RT/Extension/REST2/Resource/Message.pm
+++ b/lib/RT/Extension/REST2/Resource/Message.pm
@@ -93,6 +93,18 @@ 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");
+ }
+ }
+
$self->created_transaction($TransObj);
$self->response->body(JSON::to_json([$msg], { pretty => 1 }));
commit 11576d064d4fe660ed23d86d19331a52e3860808
Author: michel <michel at bestpractical.com>
Date: Wed Jul 31 14:11:48 2019 +0200
added doc for custom fields on transaction
diff --git a/lib/RT/Extension/REST2.pm b/lib/RT/Extension/REST2.pm
index c4b61c6..4d729ca 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",
+ "CustomFields": {"Object-RT::Transaction--CustomField-2": "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'
commit d2deaea895e28d0e5f11d84d68e7142752cc4009
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..fd485af 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