diff --git a/src/Commands/NotificationsCommand.php b/src/Commands/NotificationsCommand.php index df5840c..37ca97a 100644 --- a/src/Commands/NotificationsCommand.php +++ b/src/Commands/NotificationsCommand.php @@ -5,8 +5,10 @@ use AcquiaCli\Cli\Config; use AcquiaCloudApi\Connector\Client; use AcquiaCloudApi\Endpoints\Notifications; +use AcquiaCloudApi\Endpoints\Applications; use AcquiaCloudApi\Endpoints\Organizations; use Symfony\Component\Console\Helper\Table; +use AcquiaCloudApi\Exception\ApiErrorException; /** * Class NotificationsCommand @@ -26,16 +28,20 @@ class NotificationsCommand extends AcquiaCommand * A leading "~" in the field indicates the field should be sorted in a descending order. * * @command notification:list + * @option details Whether to show more details in the notication list (slower). * @aliases n:l */ public function notificationList( Config $config, Client $client, + Applications $applicationsAdapter, + Organizations $organizationsAdapter, Notifications $notificationsAdapter, $uuid, $limit = 50, $filter = null, - $sort = '~created_at' + $sort = '~created_at', + $options = ['details'] ) { // Allows for limits and sort criteria. @@ -58,19 +64,66 @@ public function notificationList( $format = $extraConfig['format']; $timezone = new \DateTimeZone($tz); + /** + * If we are using the details option, override the headers as we will + * be using a different number of columns. + * + * We will also be making THREE additional API calls which is why details + * nestled in as an option rather than the default, with a warning that + * it will be slower. + * + * We have to get both admins and members as separate API calls as there + * is no single call to source all users in an organisation. + * + * The array_reduce() allows us to go from an ArrayObject of MemberResponses + * to a simple associative array keyed on user UUID with a value of their email. + * + * @TODO find a way to store the application object further up the chain + * as we can call on that where needed. + */ + if ($options['details']) { + $table->setHeaders(['UUID', 'User', 'Created', 'Name', 'Status']); + + $application = $applicationsAdapter->get($uuid); + $orgUuid = $application->organization->uuid; + + $admins = $organizationsAdapter->getAdmins($orgUuid); + $members = $organizationsAdapter->getMembers($orgUuid); + + $users = $admins->getArrayCopy() + $members->getArrayCopy(); + $uuids = array_reduce($users, function ($result, $member) { + $result[$member->uuid] = $member->mail; + return $result; + }, []); + } + foreach ($notifications as $notification) { $createdDate = new \DateTime($notification->created_at); $createdDate->setTimezone($timezone); + $rows = [ + $notification->uuid, + $createdDate->format($format), + $notification->label, + $notification->status, + ]; + + /** + * Again only fires if we've used the details option. + * There is a chance that an operation will occur without a user attached. + * This could happen if an automated task/Acquia support person does something. + * The code here finds the user by UUID and presents their email. + */ + if ($options['details']) { + $author = $notification->context->author->uuid; + $mail = isset($uuids[$author]) ? $uuids[$author] : 'N/A'; + array_splice($rows, 1, 0, $mail); + } + $table ->addRows( [ - [ - $notification->uuid, - $createdDate->format($format), - $notification->label, - $notification->status, - ], + $rows ] ); } @@ -88,8 +141,14 @@ public function notificationList( * @aliases n:i * @throws \Exception */ - public function notificationInfo(Config $config, Notifications $notificationsAdapter, $uuid, $notificationUuid) - { + public function notificationInfo( + Config $config, + Applications $applicationsAdapter, + Organizations $organizationsAdapter, + Notifications $notificationsAdapter, + $uuid, + $notificationUuid + ) { $extraConfig = $config->get('extraconfig'); $tz = $extraConfig['timezone']; @@ -103,7 +162,21 @@ public function notificationInfo(Config $config, Notifications $notificationsAda $completedDate = new \DateTime($notification->completed_at); $completedDate->setTimezone($timezone); + // @TODO Find a way to store the application object earlier to remove this call. + $application = $applicationsAdapter->get($uuid); + $orgUuid = $application->organization->uuid; + $admins = $organizationsAdapter->getAdmins($orgUuid); + $members = $organizationsAdapter->getMembers($orgUuid); + $users = $admins->getArrayCopy() + $members->getArrayCopy(); + $uuids = array_reduce($users, function ($result, $member) { + $result[$member->uuid] = $member->mail; + return $result; + }, []); + $author = $notification->context->author->uuid; + $mail = isset($uuids[$author]) ? $uuids[$author] : 'N/A'; + $this->say(sprintf('ID: %s', $notification->uuid)); + $this->say(sprintf('User: %s', $mail)); $this->say(sprintf('Event: %s', $notification->event)); $this->say(sprintf('Description: %s', htmlspecialchars_decode($notification->description))); $this->say(sprintf('Status: %s', $notification->status)); diff --git a/tests/AcquiaCliTestCase.php b/tests/AcquiaCliTestCase.php index 6c102d3..d602097 100644 --- a/tests/AcquiaCliTestCase.php +++ b/tests/AcquiaCliTestCase.php @@ -184,6 +184,7 @@ public static function getFixtureMap() 'get' => 'Applications/getAllApplications.json', ], '/applications/a47ac10b-58cc-4372-a567-0e02b2c3d470' => [ + 'get' => 'Applications/getApplication.json', 'put' => 'Applications/renameApplication.json' ], '/applications/a47ac10b-58cc-4372-a567-0e02b2c3d470/environments' => [ diff --git a/tests/Commands/NotificationCommandTest.php b/tests/Commands/NotificationCommandTest.php index 26fe634..3987450 100644 --- a/tests/Commands/NotificationCommandTest.php +++ b/tests/Commands/NotificationCommandTest.php @@ -24,10 +24,19 @@ public function notificationProvider() +--------------------------------------+---------------------+-----------------------------------+-----------+ | 1bd3487e-71d1-4fca-a2d9-5f969b3d35c1 | 2019-07-30 06:47:13 | Application added to recents list | completed | +--------------------------------------+---------------------+-----------------------------------+-----------+ +TABLE; + + $getAllNotifictionsDetails = <<