[Bps-public-commit] rt-extension-rest2 01/07: Support additional fields parameters for Roles and CustomFields in Collection

sunnavy sunnavy at bestpractical.com
Wed Jul 14 10:05:48 EDT 2021


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

sunnavy pushed a commit to branch saved-searches
in repository rt-extension-rest2.

commit 7f6868c16a443f6cc2d74de9dadc0eff0c5cf936
Author: sunnavy <sunnavy at bestpractical.com>
AuthorDate: Thu May 27 05:42:56 2021 +0800

    Support additional fields parameters for Roles and CustomFields in Collection
    
    E.g. fields=Requestor&fields[Requestor]=EmailAddress
---
 lib/RT/Extension/REST2/Resource.pm         | 41 ++++++++++++++++++++----------
 lib/RT/Extension/REST2/Resource/Assets.pm  |  2 +-
 lib/RT/Extension/REST2/Resource/Tickets.pm |  2 +-
 3 files changed, 30 insertions(+), 15 deletions(-)

diff --git a/lib/RT/Extension/REST2/Resource.pm b/lib/RT/Extension/REST2/Resource.pm
index 594f5c9..c58fe9b 100644
--- a/lib/RT/Extension/REST2/Resource.pm
+++ b/lib/RT/Extension/REST2/Resource.pm
@@ -36,7 +36,7 @@ sub expand_field {
             while (my $cf = $cfs->Next) {
                 if (! defined $values{$cf->Id}) {
                     $values{$cf->Id} = {
-                        %{ expand_uid($cf->UID) },
+                        %{ $self->_expand_object($cf, $field, $param_prefix) },
                         name   => $cf->Name,
                         values => [],
                     };
@@ -71,13 +71,14 @@ sub expand_field {
 
                 my $group = $item->RoleGroup($role);
                 if ( !$group->Id ) {
-                    $data{$role} = expand_uid( RT->Nobody->UserObj->UID ) if $item->_ROLES->{$role}{Single};
+                    $data{$role} = $self->_expand_object( RT->Nobody->UserObj, $field, $param_prefix )
+                        if $item->_ROLES->{$role}{Single};
                     next;
                 }
 
                 my $gms = $group->MembersObj;
                 while ( my $gm = $gms->Next ) {
-                    push @{ $data{$role} }, expand_uid( $gm->MemberObj->Object->UID );
+                    push @{ $data{$role} }, $self->_expand_object( $gm->MemberObj->Object, $field, $param_prefix );
                 }
 
                 # Avoid the extra array ref for single member roles
@@ -91,13 +92,14 @@ sub expand_field {
 
             my $group = $item->RoleGroup($field);
             if ( !$group->Id ) {
-                $result = expand_uid( RT->Nobody->UserObj->UID ) if $item->_ROLES->{$field}{Single};
+                $result = $self->_expand_object( RT->Nobody->UserObj, $field, $param_prefix )
+                    if $item->_ROLES->{$field}{Single};
                 next;
             }
 
             my $gms = $group->MembersObj;
             while ( my $gm = $gms->Next ) {
-                push @$result, expand_uid( $gm->MemberObj->Object->UID );
+                push @$result, $self->_expand_object( $gm->MemberObj->Object, $field, $param_prefix );
             }
 
             # Avoid the extra array ref for single member roles
@@ -112,14 +114,8 @@ sub expand_field {
         } elsif ($item->can($field . 'Obj')) {
             my $method = $field . 'Obj';
             my $obj = $item->$method;
-            if ( $obj->can('UID') and $result = expand_uid( $obj->UID ) ) {
-                my $param_field = $param_prefix . '[' . $field . ']';
-                my @subfields = split( /,/, $self->request->param($param_field) || '' );
-
-                for my $subfield (@subfields) {
-                    my $subfield_result = $self->expand_field( $obj, $subfield, $param_field );
-                    $result->{$subfield} = $subfield_result if defined $subfield_result;
-                }
+            if ( $obj->can('UID') ) {
+                $result = $self->_expand_object( $obj, $field, $param_prefix );
             }
         }
 
@@ -129,6 +125,25 @@ sub expand_field {
     return $result // '';
 }
 
+sub _expand_object {
+    my $self         = shift;
+    my $object       = shift;
+    my $field        = shift;
+    my $param_prefix = shift || 'fields';
+
+    return unless $object->can('UID');
+
+    my $result      = expand_uid( $object->UID ) or return;
+    my $param_field = $param_prefix . '[' . $field . ']';
+    my @subfields   = split( /,/, $self->request->param($param_field) || '' );
+
+    for my $subfield (@subfields) {
+        my $subfield_result = $self->expand_field( $object, $subfield, $param_field );
+        $result->{$subfield} = $subfield_result if defined $subfield_result;
+    }
+    return $result;
+}
+
 __PACKAGE__->meta->make_immutable;
 
 1;
diff --git a/lib/RT/Extension/REST2/Resource/Assets.pm b/lib/RT/Extension/REST2/Resource/Assets.pm
index 5144833..ae218d9 100644
--- a/lib/RT/Extension/REST2/Resource/Assets.pm
+++ b/lib/RT/Extension/REST2/Resource/Assets.pm
@@ -28,7 +28,7 @@ sub expand_field {
         if ( my $group = $item->RoleGroup($role) ) {
             my $gms = $group->MembersObj;
             while ( my $gm = $gms->Next ) {
-                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+                push @$members, $self->_expand_object( $gm->MemberObj->Object, $field, $param_prefix );
             }
             $members = shift @$members if $group->SingleMemberRoleGroup;
         }
diff --git a/lib/RT/Extension/REST2/Resource/Tickets.pm b/lib/RT/Extension/REST2/Resource/Tickets.pm
index 8abbc7d..f060d49 100644
--- a/lib/RT/Extension/REST2/Resource/Tickets.pm
+++ b/lib/RT/Extension/REST2/Resource/Tickets.pm
@@ -77,7 +77,7 @@ sub expand_field {
         if ( my $group = $item->RoleGroup($role) ) {
             my $gms = $group->MembersObj;
             while ( my $gm = $gms->Next ) {
-                push @$members, expand_uid( $gm->MemberObj->Object->UID );
+                push @$members, $self->_expand_object( $gm->MemberObj->Object, $field, $param_prefix );
             }
         }
         return $members;

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


More information about the Bps-public-commit mailing list