added endpoint to approve/unpprove push notification

PUT /api/v1/summits/{id}/notifications/{notification_id}/approve
DELETE /api/v1/summits/{id}/notifications/{notification_id}/approve

Change-Id: I8128585b90bc73314eb4d024a5786f2d8ef637e5
This commit is contained in:
Sebastian Marcet 2018-04-17 12:51:31 -03:00
parent 8dad96af84
commit f36d3a9da8
8 changed files with 188 additions and 3 deletions

View File

@ -272,6 +272,65 @@ class OAuth2SummitNotificationsApiController extends OAuth2ProtectedController
}
}
/**
* @param $summit_id
* @param $notification_id
* @return mixed
*/
public function approveNotification($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();
$current_member = null;
if(!is_null($this->resource_server_context->getCurrentUserExternalId())){
$current_member = $this->member_repository->getById($this->resource_server_context->getCurrentUserExternalId());
}
$notification = $this->push_notification_service->approveNotification($summit, $current_member, $notification_id);
return $this->updated(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
* @param $notification_id
* @return mixed
*/
public function unApproveNotification($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();
$current_member = null;
if(!is_null($this->resource_server_context->getCurrentUserExternalId())){
$current_member = $this->member_repository->getById($this->resource_server_context->getCurrentUserExternalId());
}
$notification = $this->push_notification_service->unApproveNotification($summit, $current_member, $notification_id);
return $this->updated(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

View File

@ -243,6 +243,8 @@ Route::group([
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']);
Route::put('approve', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@approveNotification']);
Route::delete('approve', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitNotificationsApiController@unApproveNotification']);
});
});

View File

@ -212,6 +212,25 @@ class PushNotificationMessage extends SilverstripeBaseModel
$this->owner = $owner;
}
/**
* @param Member|null $approved_by
* @return $this
*/
public function approve(Member $approved_by = null){
$this->approved = true;
$this->approved_by = $approved_by;
return $this;
}
/**
* @return $this
*/
public function unApprove(){
$this->approved = false;
$this->approved_by = null;
return $this;
}
/**
* @return string
*/

View File

@ -148,6 +148,10 @@ final class SummitFactory
$summit->setScheduleDefaultStartDate($start_datetime);
}
if(isset($data['link']) ){
$summit->setLink(trim($data['link']));
}
if(isset($data['secondary_registration_link']) ){
$summit->setSecondaryRegistrationLink(trim($data['secondary_registration_link']));
}

View File

@ -31,4 +31,24 @@ interface ISummitPushNotificationService
* @throws EntityNotFoundException
*/
public function addPushNotification(Summit $summit, Member $current_member, array $data);
/**
* @param Summit $summit
* @param Member|null $current_member
* @param int $notification_id
* @return SummitPushNotification
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function approveNotification(Summit $summit, Member $current_member, $notification_id);
/**
* @param Summit $summit
* @param Member|null $current_member
* @param int $notification_id
* @return SummitPushNotification
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function unApproveNotification(Summit $summit, Member $current_member, $notification_id);
}

View File

@ -153,9 +153,7 @@ final class SummitPushNotificationService
if($notification->getChannel() == SummitPushNotificationChannel::Members){
// auto approve for members
$notification->setApproved(true);
if(!is_null($current_member))
$notification->setApprovedBy($current_member);
$notification->approve($current_member);
}
$summit->addNotification($notification);
@ -164,4 +162,67 @@ final class SummitPushNotificationService
});
}
/**
* @param Summit $summit
* @param Member|null $current_member
* @param int $notification_id
* @return SummitPushNotification
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function approveNotification(Summit $summit, Member $current_member, $notification_id)
{
return $this->tx_service->transaction(function() use($summit, $current_member, $notification_id){
$notification = $summit->getNotificationById($notification_id);
if(is_null($notification)){
throw new EntityNotFoundException
(
trans
(
"not_found_errors.SummitPushNotificationService.approveNotification.NotificationNotFound",
[
'summit_id' => $summit->getId(),
'notification_id' => $notification_id
]
)
);
}
return $notification->approve($current_member);
});
}
/**
* @param Summit $summit
* @param Member|null $current_member
* @param int $notification_id
* @return SummitPushNotification
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function unApproveNotification(Summit $summit, Member $current_member, $notification_id)
{
return $this->tx_service->transaction(function() use($summit, $current_member, $notification_id){
$notification = $summit->getNotificationById($notification_id);
if(is_null($notification)){
throw new EntityNotFoundException
(
trans
(
"not_found_errors.SummitPushNotificationService.approveNotification.unApproveNotification",
[
'summit_id' => $summit->getId(),
'notification_id' => $notification_id
]
)
);
}
return $notification->unApprove();
});
}
}

View File

@ -1437,6 +1437,24 @@ class ApiEndpointsSeeder extends Seeder
sprintf(SummitScopes::WriteNotifications, $current_realm)
],
],
[
'name' => 'approve-notification',
'route' => '/api/v1/summits/{id}/notifications/{notification_id}/approve',
'http_method' => 'PUT',
'scopes' => [
sprintf(SummitScopes::WriteSummitData, $current_realm),
sprintf(SummitScopes::WriteNotifications, $current_realm)
],
],
[
'name' => 'unapprove-notification',
'route' => '/api/v1/summits/{id}/notifications/{notification_id}/approve',
'http_method' => 'DELETE',
'scopes' => [
sprintf(SummitScopes::WriteSummitData, $current_realm),
sprintf(SummitScopes::WriteNotifications, $current_realm)
],
],
// promo codes
[
'name' => 'get-promo-codes',

View File

@ -74,4 +74,6 @@ return [
'SummitPushNotificationService.addPushNotification.EventNotFound' => 'event :event_id does not belongs to summit :summit_id schedule',
'SummitPushNotificationService.addPushNotification.GroupNotFound' => 'group :group_id not found',
'SummitPushNotificationService.addPushNotification.MemberNotFound' => 'member :member_id not found',
'SummitPushNotificationService.approveNotification.approveNotification' => 'notification :notification_id not found on summit :summit_id',
'SummitPushNotificationService.approveNotification.unApproveNotification'=> 'notification :notification_id not found on summit :summit_id',
];