Added endpoint to update selection plan by summit

PUT /api/v1/summits/{id}/selection-plans/{selection_plan_id}

Payload

'name'                  => 'sometimes|string|max:255',
'is_enabled'            => 'sometimes|boolean',
'submission_begin_date' => 'nullable|date_format:U',
'submission_end_date'   => 'nullable|required_with:submission_begin_date|date_format:U|after_or_equal:submission_begin_date',
'voting_begin_date'     => 'nullable|date_format:U',
'voting_end_date'       => 'nullable|required_with:voting_begin_date|date_format:U|after_or_equal:voting_begin_date',
'selection_begin_date'  => 'nullable|date_format:U',
'selection_end_date'    => 'nullable|required_with:selection_begin_date|date_format:U|after_or_equal:selection_begin_date',

Change-Id: I451cd23f5208f1d08a8812871c3667e6dc951ad5
This commit is contained in:
Sebastian Marcet 2018-06-07 13:05:37 -07:00
parent cb6b3a22c7
commit 3318a2840e
8 changed files with 212 additions and 28 deletions

View File

@ -57,6 +57,10 @@ final class OAuth2SummitSelectionPlansApiController extends OAuth2ProtectedContr
$this->selection_plan_service = $selection_plan_service;
}
/**
* @param $summit_id
* @return mixed
*/
public function addSelectionPlan($summit_id){
try {
@ -97,4 +101,50 @@ final class OAuth2SummitSelectionPlansApiController extends OAuth2ProtectedContr
return $this->error500($ex);
}
}
/**
* @param $summit_id
* @param $selection_plan_id
* @return mixed
*/
public function updateSelectionPlan($summit_id, $selection_plan_id){
try {
if(!Request::isJson()) return $this->error400();
$payload = Input::json()->all();
$summit = SummitFinderStrategyFactory::build($this->summit_repository, $this->resource_server_context)->find($summit_id);
if (is_null($summit)) return $this->error404();
$rules = SummitSelectionPlanValidationRulesFactory::build($payload, true);
// Creates a Validator instance and validates the data.
$validation = Validator::make($payload, $rules);
if ($validation->fails()) {
$messages = $validation->messages()->toArray();
return $this->error412
(
$messages
);
}
$template = $this->selection_plan_service->updateSelectionPlan($summit, $selection_plan_id, $payload);
return $this->updated(SerializerRegistry::getInstance()->getSerializer($template)->serialize());
}
catch (ValidationException $ex1) {
Log::warning($ex1);
return $this->error412([$ex1->getMessage()]);
}
catch(EntityNotFoundException $ex2)
{
Log::warning($ex2);
return $this->error404(['message'=> $ex2->getMessage()]);
}
catch (Exception $ex) {
Log::error($ex);
return $this->error500($ex);
}
}
}

View File

@ -112,6 +112,9 @@ Route::group([
// selection plans
Route::group(['prefix' => 'selection-plans'], function () {
Route::post('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitSelectionPlansApiController@addSelectionPlan']);
Route::group(['prefix' => '{selection_plan_id}'], function () {
Route::put('', [ 'middleware' => 'auth.user:administrators|summit-front-end-administrators', 'uses' => 'OAuth2SummitSelectionPlansApiController@updateSelectionPlan']);
});
});
// RSVP templates

View File

@ -1972,16 +1972,24 @@ SQL;
*/
public function checkSelectionPlanConflicts(SelectionPlan $selection_plan){
foreach ($this->selection_plans as $sp){
$start = $selection_plan->getSelectionBeginDate();
$end = $selection_plan->getSelectionEndDate();
if(!is_null($start) && !is_null($end) && DateUtils::checkTimeFramesOverlap
(
$start,
$end,
$sp->getSelectionBeginDate(),
$sp->getSelectionEndDate()
))
if($sp->getId() == $selection_plan->getId()) continue;
$start1 = $selection_plan->getSelectionBeginDate();
$end1 = $selection_plan->getSelectionEndDate();
$start2 = $sp->getSelectionBeginDate();
$end2 = $sp->getSelectionEndDate();
if(!is_null($start1) && !is_null($end1) &&
!is_null($start2) && !is_null($end2)
&& DateUtils::checkTimeFramesOverlap
(
$start1,
$end1,
$start2,
$end2
)
)
throw new ValidationException(trans(
'validation_errors.Summit.checkSelectionPlanConflicts.conflictOnSelectionWorkflow',
[
@ -1990,15 +1998,22 @@ SQL;
]
));
$start = $selection_plan->getSubmissionBeginDate();
$end = $selection_plan->getSubmissionEndDate();
if(!is_null($start) && !is_null($end) && DateUtils::checkTimeFramesOverlap
(
$start,
$end,
$sp->getSubmissionBeginDate(),
$sp->getSubmissionEndDate()
))
$start1 = $selection_plan->getSubmissionBeginDate();
$end1 = $selection_plan->getSubmissionEndDate();
$start2 = $sp->getSubmissionBeginDate();
$end2 = $sp->getSubmissionEndDate();
if(!is_null($start1) && !is_null($end1) &&
!is_null($start2) && !is_null($end2) &&
DateUtils::checkTimeFramesOverlap
(
$start1,
$end1,
$start2,
$end2
)
)
throw new ValidationException(trans(
'validation_errors.Summit.checkSelectionPlanConflicts.conflictOnSubmissionWorkflow',
[
@ -2007,16 +2022,21 @@ SQL;
]
));
$start = $selection_plan->getVotingBeginDate();
$end = $selection_plan->getVotingEndDate();
$start1 = $selection_plan->getVotingBeginDate();
$end1 = $selection_plan->getVotingEndDate();
$start2 = $sp->getVotingBeginDate();
$end2 = $sp->getVotingEndDate();
if(!is_null($start) && !is_null($end) && DateUtils::checkTimeFramesOverlap
(
$start,
$end,
$sp->getVotingBeginDate(),
$sp->getVotingEndDate()
))
if(!is_null($start1) && !is_null($end1) &&
!is_null($start2) && !is_null($end2) &&
DateUtils::checkTimeFramesOverlap
(
$start1,
$end1,
$start2,
$end2
)
)
throw new ValidationException(trans(
'validation_errors.Summit.checkSelectionPlanConflicts.conflictOnVotingWorkflow',
[
@ -2035,7 +2055,18 @@ SQL;
*/
public function getSelectionPlanByName($name){
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('name', intval($name)));
$criteria->where(Criteria::expr()->eq('name', trim($name)));
$selection_plan = $this->selection_plans->matching($criteria)->first();
return $selection_plan === false ? null : $selection_plan;
}
/**
* @param int $id
* @return null|SelectionPlan
*/
public function getSelectionPlanById($id){
$criteria = Criteria::create();
$criteria->where(Criteria::expr()->eq('id', intval($id)));
$selection_plan = $this->selection_plans->matching($criteria)->first();
return $selection_plan === false ? null : $selection_plan;
}

View File

@ -27,4 +27,13 @@ interface ISummitSelectionPlanService
* @throws ValidationException
*/
public function addSelectionPlan(Summit $summit, array $payload);
/**
* @param Summit $summit
* @param int $selection_plan_id
* @param array $payload
* @return SelectionPlan
* @throws ValidationException
*/
public function updateSelectionPlan(Summit $summit, $selection_plan_id, array $payload);
}

View File

@ -13,6 +13,7 @@
**/
use App\Models\Foundation\Summit\Factories\SummitSelectionPlanFactory;
use App\Models\Foundation\Summit\SelectionPlan;
use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException;
use models\summit\Summit;
/**
@ -55,4 +56,47 @@ final class SummitSelectionPlanService
return $selection_plan;
});
}
/**
* @param Summit $summit
* @param int $selection_plan_id
* @param array $payload
* @return SelectionPlan
* @throws ValidationException
* @throws EntityNotFoundException
*/
public function updateSelectionPlan(Summit $summit, $selection_plan_id, array $payload)
{
return $this->tx_service->transaction(function() use($summit, $selection_plan_id, $payload){
$selection_plan = $summit->getSelectionPlanById($selection_plan_id);
if(is_null($selection_plan))
throw new EntityNotFoundException(trans
('not_found_errors.SummitSelectionPlanService.updateSelectionPlan.SelectionPlanNotFound',
[
'selection_plan_id' => $selection_plan_id,
'summit_id' => $summit->getId()
]
));
if(isset($payload['name'])) {
$former_selection_plan = $summit->getSelectionPlanByName($payload['name']);
if ($former_selection_plan->getId() != $selection_plan_id && !is_null($former_selection_plan)) {
throw new ValidationException(trans(
'validation_errors.SummitSelectionPlanService.updateSelectionPlan.alreadyExistName',
[
'summit_id' => $summit->getId()
]
));
}
}
SummitSelectionPlanFactory::populate($selection_plan, $payload, $summit);
// validate selection plan
$summit->checkSelectionPlanConflicts($selection_plan);
return $selection_plan;
});
}
}

View File

@ -77,4 +77,5 @@ return [
'SummitPushNotificationService.approveNotification.NotificationNotFound' => 'notification :notification_id not found on summit :summit_id',
'SummitPushNotificationService.unApproveNotification.NotificationNotFound'=> 'notification :notification_id not found on summit :summit_id',
'SummitPushNotificationService.deleteNotification.NotificationNotFound'=> 'notification :notification_id not found on summit :summit_id',
'SummitSelectionPlanService.updateSelectionPlan.SelectionPlanNotFound' => 'selection plan :selection_plan_id not found on summit :summit_id',
];

View File

@ -77,4 +77,5 @@ return [
'Summit.checkSelectionPlanConflicts.conflictOnSubmissionWorkflow' => 'there is a conflict on submission dates with selection plan :selection_plan_id on summit :summit_id',
'Summit.checkSelectionPlanConflicts.conflictOnVotingWorkflow' => 'there is a conflict on voting dates with selection plan :selection_plan_id on summit :summit_id',
'SummitSelectionPlanService.addSelectionPlan.alreadyExistName' => 'there is already another selection plan with same name on summit :summit_id',
'SummitSelectionPlanService.updateSelectionPlan.alreadyExistName' => 'there is already another selection plan with same name on summit :summit_id',
];

View File

@ -52,4 +52,49 @@ final class OAuth2SelectionPlansApiTest extends ProtectedApiTest
$this->assertEquals($name, $selection_plan->name);
return $selection_plan;
}
/**
* @param int $summit_id
* @return mixed
*/
public function testUpdateSelectionPlan($summit_id = 24){
$selection_plan = $this->testAddSelectionPlan($summit_id);
$params = [
'id' => $summit_id,
'selection_plan_id' => $selection_plan->id
];
$start = new DateTime('now');
$end = new DateTime('now');
$end->add(new DateInterval('P15D'));
$data = [
'is_enabled' => false,
'submission_begin_date' => $start->getTimestamp(),
'submission_end_date' => $end->getTimestamp(),
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"PUT",
"OAuth2SummitSelectionPlansApiController@updateSelectionPlan",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$selection_plan = json_decode($content);
$this->assertTrue(!is_null($selection_plan));
$this->assertEquals(false, $selection_plan->is_enabled);
return $selection_plan;
}
}