diff --git a/app/Events/SummitAction.php b/app/Events/SummitAction.php new file mode 100644 index 00000000..87c41606 --- /dev/null +++ b/app/Events/SummitAction.php @@ -0,0 +1,45 @@ +summit_id = $summit_id; + } + + /** + * @return int + */ + public function getSummitId() + { + return $this->summit_id; + } +} \ No newline at end of file diff --git a/app/Events/SummitDeleted.php b/app/Events/SummitDeleted.php new file mode 100644 index 00000000..0485e56a --- /dev/null +++ b/app/Events/SummitDeleted.php @@ -0,0 +1,22 @@ +getById($event->getSummitId()); + + $owner_id = $resource_server_context->getCurrentUserExternalId(); + if (is_null($owner_id)) $owner_id = 0; + + + $entity_event = new SummitEntityEvent; + $entity_event->setEntityClassName('Summit'); + $entity_event->setEntityId($event->getSummitId()); + $entity_event->setType($type); + + if ($owner_id > 0) { + $member = $member_repository->getById($owner_id); + $entity_event->setOwner($member); + } + if(!is_null($summit)) + $entity_event->setSummit($summit); + + $entity_event->setMetadata(''); + + return $entity_event; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Repositories/ISummitRepository.php b/app/Models/Foundation/Summit/Repositories/ISummitRepository.php index da206efe..fd9a185d 100644 --- a/app/Models/Foundation/Summit/Repositories/ISummitRepository.php +++ b/app/Models/Foundation/Summit/Repositories/ISummitRepository.php @@ -23,6 +23,11 @@ interface ISummitRepository extends IBaseRepository */ public function getCurrent(); + /** + * @return Summit + */ + public function getActive(); + /** * @return Summit[] */ diff --git a/app/Providers/EventServiceProvider.php b/app/Providers/EventServiceProvider.php index d84625b4..551fc49f 100644 --- a/app/Providers/EventServiceProvider.php +++ b/app/Providers/EventServiceProvider.php @@ -32,6 +32,7 @@ use App\Factories\EntityEvents\PresentationMaterialUpdatedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerCreatedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerDeletedEntityEventFactory; use App\Factories\EntityEvents\PresentationSpeakerUpdatedEntityEventFactory; +use App\Factories\EntityEvents\SummitActionEntityEventFactory; use App\Factories\EntityEvents\SummitEventCreatedEntityEventFactory; use App\Factories\EntityEvents\SummitEventDeletedEntityEventFactory; use App\Factories\EntityEvents\SummitEventTypeActionEntityEventFactory; @@ -304,5 +305,17 @@ final class EventServiceProvider extends ServiceProvider { EntityEventPersister::persist(TrackGroupActionActionEntityEventFactory::build($event, 'DELETE')); }); + + // summits + + Event::listen(\App\Events\SummitUpdated::class, function($event) + { + EntityEventPersister::persist(SummitActionEntityEventFactory::build($event, 'UPDATE')); + }); + + Event::listen(\App\Events\SummitDeleted::class, function($event) + { + EntityEventPersister::persist(SummitActionEntityEventFactory::build($event, 'DELETE')); + }); } } diff --git a/app/Repositories/Summit/DoctrineSummitRepository.php b/app/Repositories/Summit/DoctrineSummitRepository.php index 9d24a396..33128899 100644 --- a/app/Repositories/Summit/DoctrineSummitRepository.php +++ b/app/Repositories/Summit/DoctrineSummitRepository.php @@ -89,4 +89,20 @@ final class DoctrineSummitRepository ->getQuery() ->getOneOrNullResult(); } + + /** + * @return Summit + */ + public function getActive() + { + $res = $this->getEntityManager()->createQueryBuilder() + ->select("s") + ->from(\models\summit\Summit::class, "s") + ->where('s.active = 1') + ->orderBy('s.begin_date', 'DESC') + ->getQuery() + ->getResult(); + if (count($res) == 0) return null; + return $res[0]; + } } \ No newline at end of file diff --git a/app/Services/Model/SummitService.php b/app/Services/Model/SummitService.php index 8501575c..77c01121 100644 --- a/app/Services/Model/SummitService.php +++ b/app/Services/Model/SummitService.php @@ -15,6 +15,8 @@ use App\Events\MyFavoritesAdd; use App\Events\MyFavoritesRemove; use App\Events\MyScheduleAdd; use App\Events\MyScheduleRemove; +use App\Events\SummitDeleted; +use App\Events\SummitUpdated; use App\Http\Utils\FileUploader; use App\Models\Foundation\Summit\Factories\SummitFactory; use App\Models\Utils\IntervalParser; @@ -1525,6 +1527,54 @@ final class SummitService extends AbstractService implements ISummitService { return $this->tx_service->transaction(function () use ($summit_id, $data) { + if(isset($data['name'])) { + + $former_summit = $this->summit_repository->getByName(trim($data['name'])); + if (!is_null($former_summit) && $former_summit->getId() != $summit_id) { + throw new ValidationException + ( + trans + ( + 'validation_errors.SummitService.updateSummit.NameAlreadyExists', + ['name' => $data['name']] + ) + ); + } + } + + if(isset($data['active'])) { + $active = boolval($data['active']); + $active_summit = $this->summit_repository->getActive(); + if ($active && !is_null($active_summit) && $active_summit->getId() != $summit_id) { + throw new ValidationException + ( + trans + ( + 'validation_errors.SummitService.updateSummit.SummitAlreadyActive', + ['active_summit_id' => $active_summit->getId()] + ) + ); + } + } + + $summit = $this->summit_repository->getById($summit_id); + + if(is_null($summit)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.SummitService.updateSummit.SummitNotFound', + ['summit_id' => $summit_id] + ) + ); + } + + $summit = SummitFactory::populate($summit, $data); + + Event::fire(new SummitUpdated($summit_id)); + + return $summit; }); } @@ -1538,6 +1588,23 @@ final class SummitService extends AbstractService implements ISummitService { return $this->tx_service->transaction(function () use ($summit_id) { + $summit = $this->summit_repository->getById($summit_id); + + if(is_null($summit)){ + throw new EntityNotFoundException + ( + trans + ( + 'not_found_errors.SummitService.deleteSummit.SummitNotFound', + ['summit_id' => $summit_id] + ) + ); + } + + $this->summit_repository->delete($summit); + + Event::fire(new SummitDeleted($summit_id)); + }); } } \ No newline at end of file diff --git a/resources/lang/en/not_found_errors.php b/resources/lang/en/not_found_errors.php index f9033dc8..53b12f47 100644 --- a/resources/lang/en/not_found_errors.php +++ b/resources/lang/en/not_found_errors.php @@ -68,4 +68,6 @@ return [ 'PresentationCategoryGroupService.associateAllowedGroup2TrackGroup.GroupNotFound' => 'group :group_id does not exists.', 'PresentationCategoryGroupService.disassociateAllowedGroup2TrackGroup.TrackGroupNotFound' => 'track group :track_group_id does not exists on summit :summit_id', 'PresentationCategoryGroupService.disassociateAllowedGroup2TrackGroup.GroupNotFound' => 'group :group_id does not exists.', + 'SummitService.updateSummit.SummitNotFound' => 'summit :summit_id not found', + 'SummitService.deleteSummit.SummitNotFound' => 'summit :summit_id not found', ]; \ No newline at end of file diff --git a/resources/lang/en/validation_errors.php b/resources/lang/en/validation_errors.php index 048fa5b1..534a6c84 100644 --- a/resources/lang/en/validation_errors.php +++ b/resources/lang/en/validation_errors.php @@ -67,5 +67,6 @@ return [ 'PresentationCategoryGroupService.addTrackGroup.NameAlreadyExists' => 'name :name already exists for summit :summit_id', // SummitService 'SummitService.AddSummit.NameAlreadyExists' => 'name :name its already being assigned to another summit', - + 'SummitService.updateSummit.NameAlreadyExists'=> 'name :name its already being assigned to another summit', + 'SummitService.updateSummit.SummitAlreadyActive' => 'summit :active_summit_id is already activated please deactivate it to set current summit as active' ]; \ No newline at end of file diff --git a/tests/OAuth2SummitApiTest.php b/tests/OAuth2SummitApiTest.php index fed325a2..b9345add 100644 --- a/tests/OAuth2SummitApiTest.php +++ b/tests/OAuth2SummitApiTest.php @@ -184,6 +184,68 @@ final class OAuth2SummitApiTest extends ProtectedApiTest return $summit; } + public function testUpdateSummitAlreadyActiveError(){ + $summit = $this->testAddSummit(); + $params = [ + 'id' => $summit->id + ]; + $data = [ + 'active' => 1 + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitApiController@updateSummit", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(412); + } + + public function testUpdateSummitTitle(){ + $summit = $this->testAddSummit(); + $params = [ + 'id' => $summit->id + ]; + $data = [ + 'name' => $summit->name.' update!' + ]; + + $headers = [ + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ]; + + $response = $this->action( + "PUT", + "OAuth2SummitApiController@updateSummit", + $params, + [], + [], + [], + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $summit = json_decode($content); + $this->assertTrue(!is_null($summit)); + + return $summit; + } + public function testGetSummitMin($summit_id = 23) {