From 8dad96af84f9f1deeab7baad0856ecc0e22266d9 Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 17 Apr 2018 11:57:05 -0300 Subject: [PATCH] Added endpoints to get by id summit push notification GET /api/v1/summits/{id}/notifications/{notification_id} Change-Id: I325bc804788ed1883230f6c75b246b742d387cf2 --- ...OAuth2SummitNotificationsApiController.php | 113 ++++++++++++++++++ app/Http/routes.php | 4 + app/Models/Foundation/Summit/Summit.php | 28 ++++- database/seeds/ApiEndpointsSeeder.php | 20 ++++ ...h2SummitNotificationsApiControllerTest.php | 38 ++++++ 5 files changed, 202 insertions(+), 1 deletion(-) diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitNotificationsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitNotificationsApiController.php index 656d9d73..00f1d907 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitNotificationsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitNotificationsApiController.php @@ -11,6 +11,8 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use App\Http\Utils\BooleanCellFormatter; +use App\Http\Utils\EpochCellFormatter; use App\Http\Utils\PagingConstants; use App\Services\Model\ISummitPushNotificationService; use models\exceptions\EntityNotFoundException; @@ -159,6 +161,117 @@ class OAuth2SummitNotificationsApiController extends OAuth2ProtectedController } } + /** + * @param $summit_id + * @return mixed + */ + public function getAllCSV($summit_id) + { + try + { + $summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + + + // default values + $page = 1; + $per_page = PHP_INT_MAX; + + + $filter = null; + if (Input::has('filter')) { + $filter = FilterParser::parse(Input::get('filter'), [ + 'channel' => ['=='], + 'sent_date' => ['>', '<', '<=', '>=', '=='], + 'created' => ['>', '<', '<=', '>=', '=='], + 'is_sent' => ['=='], + 'approved' => ['=='], + 'event_id' => ['=='], + ]); + } + + if(is_null($filter)) $filter = new Filter(); + + $filter->validate([ + 'channel' => 'sometimes|in:EVERYONE,SPEAKERS,ATTENDEES,MEMBERS,SUMMIT,EVENT,GROUP', + 'sent_date' => 'sometimes|date_format:U', + 'created' => 'sometimes|date_format:U', + 'is_sent' => 'sometimes|boolean', + 'approved' => 'sometimes|boolean', + 'event_id' => 'sometimes|integer', + ]); + + $order = null; + + if (Input::has('order')) + { + $order = OrderParser::parse(Input::get('order'), [ + + 'sent_date', + 'created', + 'id', + ]); + } + + $data = $this->repository->getAllByPageBySummit($summit, new PagingInfo($page, $per_page), $filter, $order); + $filename = "push-notification-" . date('Ymd'); + $list = $data->toArray(); + return $this->export + ( + 'csv', + $filename, + $list['data'], + [ + 'created' => new EpochCellFormatter, + 'last_edited' => new EpochCellFormatter, + 'sent_date' => new EpochCellFormatter, + 'is_sent' => new BooleanCellFormatter, + 'approved' => new BooleanCellFormatter, + ] + ); + } + catch (EntityNotFoundException $ex1) + { + Log::warning($ex1); + return $this->error404(); + } + catch (ValidationException $ex2) + { + Log::warning($ex2); + return $this->error412($ex2->getMessages()); + } + catch (\Exception $ex) + { + Log::error($ex); + return $this->error500($ex); + } + } + + /** + * @param $summit_id + * @param $notification_id + * @return mixed + */ + public function getById($summit_id, $notification_id){ + try { + $summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id); + if (is_null($summit)) return $this->error404(); + $notification = $summit->getNotificationById($notification_id); + if(is_null($notification)) + return $this->error404(); + return $this->ok(SerializerRegistry::getInstance()->getSerializer($notification)->serialize( Request::input('expand', ''))); + } catch (ValidationException $ex1) { + Log::warning($ex1); + return $this->error412(array($ex1->getMessage())); + } catch (EntityNotFoundException $ex2) { + Log::warning($ex2); + return $this->error404(array('message' => $ex2->getMessage())); + } catch (Exception $ex) { + Log::error($ex); + return $this->error500($ex); + } + } + /** * @param $summit_id * @return mixed diff --git a/app/Http/routes.php b/app/Http/routes.php index 0b91ccdb..aef5f692 100644 --- a/app/Http/routes.php +++ b/app/Http/routes.php @@ -239,7 +239,11 @@ Route::group([ // notifications Route::group(['prefix' => 'notifications'], function () { Route::get('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@getAll']); + Route::get('csv', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@getAllCSV']); Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@addPushNotification']); + Route::group(['prefix' => '{notification_id}'], function () { + Route::get('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@getById']); + }); }); // speakers diff --git a/app/Models/Foundation/Summit/Summit.php b/app/Models/Foundation/Summit/Summit.php index ff4c28f4..df0ef018 100644 --- a/app/Models/Foundation/Summit/Summit.php +++ b/app/Models/Foundation/Summit/Summit.php @@ -1796,7 +1796,6 @@ SQL; return $rsvp_template === false ? null : $rsvp_template; } - /** * @param RSVPTemplate $template * @return $this @@ -2013,4 +2012,31 @@ SQL; { $this->secondary_registration_label = $secondary_registration_label; } + + /** + * @param int $notification_id + * @return SummitPushNotification|null + */ + public function getNotificationById($notification_id){ + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq('id', intval($notification_id))); + $notification = $this->notifications->matching($criteria)->first(); + return $notification === false ? null : $notification; + } + + /** + * @return string + */ + public function getCalendarSyncName() + { + return $this->calendar_sync_name; + } + + /** + * @return string + */ + public function getCalendarSyncDesc() + { + return $this->calendar_sync_desc; + } } \ No newline at end of file diff --git a/database/seeds/ApiEndpointsSeeder.php b/database/seeds/ApiEndpointsSeeder.php index 68136dfc..9fc7fdd3 100644 --- a/database/seeds/ApiEndpointsSeeder.php +++ b/database/seeds/ApiEndpointsSeeder.php @@ -1409,6 +1409,26 @@ class ApiEndpointsSeeder extends Seeder sprintf(SummitScopes::ReadAllSummitData, $current_realm), sprintf(SummitScopes::ReadNotifications, $current_realm) ], + ], + [ + 'name' => 'get-notifications-csv', + 'route' => '/api/v1/summits/{id}/notifications/csv', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadAllSummitData, $current_realm), + sprintf(SummitScopes::ReadNotifications, $current_realm) + ], + ], + [ + 'name' => 'get-notification-by-id', + 'route' => '/api/v1/summits/{id}/notifications/{notification_id}', + 'http_method' => 'GET', + 'scopes' => [ + sprintf(SummitScopes::ReadAllSummitData, $current_realm), + sprintf(SummitScopes::ReadNotifications, $current_realm) + ], + ], + [ 'name' => 'add-notifications', 'route' => '/api/v1/summits/{id}/notifications', 'http_method' => 'POST', diff --git a/tests/OAuth2SummitNotificationsApiControllerTest.php b/tests/OAuth2SummitNotificationsApiControllerTest.php index 00749775..c00bf539 100644 --- a/tests/OAuth2SummitNotificationsApiControllerTest.php +++ b/tests/OAuth2SummitNotificationsApiControllerTest.php @@ -60,6 +60,44 @@ final class OAuth2SummitNotificationsApiControllerTest extends ProtectedApiTest return $notifications; } + /** + * @param int $summit_id + * @return mixed + */ + public function testGetPushNotificationById($summit_id = 24){ + $notifications_response = $this->testGetApprovedSummitNotifications($summit_id); + + $params = [ + 'id' => $summit_id, + 'notification_id' => $notifications_response->data[0]->id + ]; + + $headers = [ + + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action + ( + "GET", + "OAuth2SummitNotificationsApiController@getById", + $params, + [], + [], + [], + $headers + ); + + $content = $response->getContent(); + $this->assertResponseStatus(200); + + $notification = json_decode($content); + $this->assertTrue(!is_null($notification)); + + return $notification; + } + /** * @param int $summit_id * @return mixed