From d856c7ecc2a814765bb629dd243a1ef507bc8cbe Mon Sep 17 00:00:00 2001 From: Sebastian Marcet Date: Tue, 27 Feb 2018 12:19:59 -0300 Subject: [PATCH] Updated Add/Update event endpoint to accept RSVP template id now payload accepts as a new field 'rsvp_template_id' (integer) Change-Id: Ifdfb4e55e8dd818c25c0732c9cbb14f662679c36 --- .../OAuth2SummitEventsApiController.php | 8 +- .../Summit/SummitEventSerializer.php | 11 +- .../Summit/Events/{ => RSVP}/RSVP.php | 4 - .../Summit/Events/{ => RSVP}/RSVPAnswer.php | 0 .../Summit/Events/RSVP/RSVPTemplate.php | 94 +++++++++++++++ .../Foundation/Summit/Events/SummitEvent.php | 64 +++++++--- .../Repositories/IRSVPTemplateRepository.php | 22 ++++ app/Models/Foundation/Summit/Summit.php | 20 +++- app/Repositories/RepositoriesProvider.php | 9 ++ .../Summit/DoctrineRSVPTemplateRepository.php | 33 ++++++ app/Services/Model/SummitService.php | 17 +++ tests/OAuth2SummitEventsApiTest.php | 110 +++++++++++++++++- 12 files changed, 362 insertions(+), 30 deletions(-) rename app/Models/Foundation/Summit/Events/{ => RSVP}/RSVP.php (99%) rename app/Models/Foundation/Summit/Events/{ => RSVP}/RSVPAnswer.php (100%) create mode 100644 app/Models/Foundation/Summit/Events/RSVP/RSVPTemplate.php create mode 100644 app/Models/Foundation/Summit/Repositories/IRSVPTemplateRepository.php create mode 100644 app/Repositories/Summit/DoctrineRSVPTemplateRepository.php diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php index 640de203..cdfecf85 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitEventsApiController.php @@ -330,8 +330,8 @@ final class OAuth2SummitEventsApiController extends OAuth2ProtectedController if(!Request::isJson()) return $this->error403(); $data = Input::json(); - $rules = array - ( + $rules = [ + 'title' => 'required|string|max:100', 'description' => 'required|string', 'type_id' => 'required|integer', @@ -340,6 +340,7 @@ final class OAuth2SummitEventsApiController extends OAuth2ProtectedController 'end_date' => 'sometimes|required_with:start_date|date_format:U|after:start_date', 'track_id' => 'required|integer', 'rsvp_link' => 'sometimes|url', + 'rsvp_template_id' => 'sometimes|integer', 'head_count' => 'sometimes|integer', 'social_description' => 'sometimes|string|max:100', 'allow_feedback' => 'sometimes|boolean', @@ -353,7 +354,7 @@ final class OAuth2SummitEventsApiController extends OAuth2ProtectedController 'moderator_speaker_id' => 'sometimes|integer', // group event 'groups' => 'sometimes|int_array', - ); + ]; // Creates a Validator instance and validates the data. $validation = Validator::make($data->all(), $rules); @@ -412,6 +413,7 @@ final class OAuth2SummitEventsApiController extends OAuth2ProtectedController 'title' => 'sometimes|string|max:100', 'description' => 'sometimes|string', 'rsvp_link' => 'sometimes|url', + 'rsvp_template_id' => 'sometimes|integer', 'head_count' => 'sometimes|integer', 'social_description' => 'sometimes|string|max:100', 'location_id' => 'sometimes|integer', diff --git a/app/ModelSerializers/Summit/SummitEventSerializer.php b/app/ModelSerializers/Summit/SummitEventSerializer.php index d365b462..bb5c1425 100644 --- a/app/ModelSerializers/Summit/SummitEventSerializer.php +++ b/app/ModelSerializers/Summit/SummitEventSerializer.php @@ -13,7 +13,6 @@ **/ use libs\utils\JsonUtils; use models\summit\SummitEvent; - /** * Class SummitEventSerializer * @package ModelSerializers @@ -36,12 +35,13 @@ class SummitEventSerializer extends SilverStripeSerializer 'Published' => 'is_published:json_boolean', 'HeadCount' => 'head_count:json_int', 'RSVPLink' => 'rsvp_link:json_string', - 'IsExternalRSVP' => 'rsvp_external:json_boolean', + 'RSVPTemplateId' => 'rsvp_template_id:json_int', + 'ExternalRSVP' => 'rsvp_external:json_boolean', 'CategoryId' => 'track_id:json_int', ); - protected static $allowed_fields = array - ( + protected static $allowed_fields = [ + 'id', 'title', 'description', @@ -59,7 +59,8 @@ class SummitEventSerializer extends SilverStripeSerializer 'rsvp_link', 'rsvp_external', 'track_id', - ); + 'rsvp_template_id', + ]; protected static $allowed_relations = array ( diff --git a/app/Models/Foundation/Summit/Events/RSVP.php b/app/Models/Foundation/Summit/Events/RSVP/RSVP.php similarity index 99% rename from app/Models/Foundation/Summit/Events/RSVP.php rename to app/Models/Foundation/Summit/Events/RSVP/RSVP.php index dc6a25e3..909a0e24 100644 --- a/app/Models/Foundation/Summit/Events/RSVP.php +++ b/app/Models/Foundation/Summit/Events/RSVP/RSVP.php @@ -1,5 +1,4 @@ title; + } + + /** + * @param string $title + */ + public function setTitle($title) + { + $this->title = $title; + } + + /** + * @return bool + */ + public function isEnabled() + { + return $this->is_enabled; + } + + /** + * @param bool $is_enabled + */ + public function setIsEnabled($is_enabled) + { + $this->is_enabled = $is_enabled; + } + + /** + * @return Member + */ + public function getCreatedBy() + { + return $this->created_by; + } + + /** + * @param Member $created_by + */ + public function setCreatedBy(Member $created_by) + { + $this->created_by = $created_by; + } +} \ No newline at end of file diff --git a/app/Models/Foundation/Summit/Events/SummitEvent.php b/app/Models/Foundation/Summit/Events/SummitEvent.php index 67772050..3530d918 100644 --- a/app/Models/Foundation/Summit/Events/SummitEvent.php +++ b/app/Models/Foundation/Summit/Events/SummitEvent.php @@ -11,6 +11,7 @@ * See the License for the specific language governing permissions and * limitations under the License. **/ +use App\Models\Foundation\Summit\Events\RSVP\RSVPTemplate; use Doctrine\ORM\Mapping AS ORM; use App\Events\SummitEventCreated; use App\Events\SummitEventDeleted; @@ -106,10 +107,11 @@ class SummitEvent extends SilverstripeBaseModel protected $head_count; /** - * @ORM\Column(name="RSVPTemplateID", type="integer") - * @var int + * @ORM\ManyToOne(targetEntity="App\Models\Foundation\Summit\Events\RSVP\RSVPTemplate", fetch="EXTRA_LAZY") + * @ORM\JoinColumn(name="RSVPTemplateID", referencedColumnName="ID") + * @var RSVPTemplate */ - protected $rsvp_template_id; + protected $rsvp_template; /** * @ORM\OneToMany(targetEntity="models\summit\RSVP", mappedBy="event", cascade={"persist"}) @@ -318,9 +320,9 @@ class SummitEvent extends SilverstripeBaseModel /** * @return string */ - public function getRsvpLink() + public function getRSVPLink() { - if($this->rsvp_template_id > 0){ + if($this->hasRSVPTemplate()){ $summit = $this->getSummit(); $main_page = $summit->getMainPage(); @@ -338,25 +340,54 @@ class SummitEvent extends SilverstripeBaseModel return $this->rsvp_link; } + /** + * @return bool + */ + public function hasRSVPTemplate(){ + return $this->getRSVPTemplateId() > 0; + } + + /** + * @return int + */ + public function getRSVPTemplateId(){ + try{ + return !is_null($this->rsvp_template) ? $this->rsvp_template->getId() : 0; + } + catch (\Exception $ex){ + return 0; + } + } + + /** + * @return RSVPTemplate + */ + public function getRSVPTemplate() + { + return $this->rsvp_template; + } + + /** + * @param RSVPTemplate $rsvp_template + */ + public function setRSVPTemplate(RSVPTemplate $rsvp_template) + { + $this->rsvp_template = $rsvp_template; + $this->rsvp_link = ''; + } + /** * @return bool */ public function hasRSVP(){ - return !empty($this->rsvp_link) || $this->rsvp_template_id > 0; + return !empty($this->rsvp_link) || $this->hasRSVPTemplate(); } /** * @return bool */ public function isExternalRSVP(){ - return !empty($this->rsvp_link) && $this->rsvp_template_id == 0; - } - - /** - * @return bool - */ - public function getIsExternalRSVP(){ - return $this->isExternalRSVP(); + return !empty($this->rsvp_link) && !$this->hasRSVPTemplate(); } public function getSlug(){ @@ -367,9 +398,10 @@ class SummitEvent extends SilverstripeBaseModel /** * @param string $rsvp_link */ - public function setRsvpLink($rsvp_link) + public function setRSVPLink($rsvp_link) { - $this->rsvp_link = $rsvp_link; + $this->rsvp_link = $rsvp_link; + $this->rsvp_template = null; } /** diff --git a/app/Models/Foundation/Summit/Repositories/IRSVPTemplateRepository.php b/app/Models/Foundation/Summit/Repositories/IRSVPTemplateRepository.php new file mode 100644 index 00000000..aa713398 --- /dev/null +++ b/app/Models/Foundation/Summit/Repositories/IRSVPTemplateRepository.php @@ -0,0 +1,22 @@ +excluded_categories_for_rejected_presentations = new ArrayCollection; $this->excluded_categories_for_upload_slide_decks = new ArrayCollection; $this->category_default_tags = new ArrayCollection; + $this->rsvp_templates = new ArrayCollection; } /** @@ -1625,4 +1632,15 @@ SQL; $event_type->setSummit($this); return $this; } + + /** + * @param int $rsvp_template_id + * @return RSVPTemplate|null + */ + public function getRSVPTemplateById($rsvp_template_id){ + $criteria = Criteria::create(); + $criteria->where(Criteria::expr()->eq('id', intval($rsvp_template_id))); + $rsvp_template = $this->rsvp_templates->matching($criteria)->first(); + return $rsvp_template === false ? null : $rsvp_template; + } } \ No newline at end of file diff --git a/app/Repositories/RepositoriesProvider.php b/app/Repositories/RepositoriesProvider.php index 11c6cebd..9fb2c773 100644 --- a/app/Repositories/RepositoriesProvider.php +++ b/app/Repositories/RepositoriesProvider.php @@ -12,8 +12,10 @@ * limitations under the License. **/ use App\Models\Foundation\Summit\Defaults\DefaultSummitEventType; +use App\Models\Foundation\Summit\Events\RSVP\RSVPTemplate; use App\Models\Foundation\Summit\Repositories\IDefaultSummitEventTypeRepository; use App\Models\Foundation\Summit\Repositories\IPresentationSpeakerSummitAssistanceConfirmationRequestRepository; +use App\Models\Foundation\Summit\Repositories\IRSVPTemplateRepository; use App\Models\Foundation\Summit\Repositories\ISummitEventTypeRepository; use App\Models\Foundation\Summit\Repositories\ISummitTrackRepository; use Illuminate\Support\Facades\App; @@ -290,5 +292,12 @@ final class RepositoriesProvider extends ServiceProvider return EntityManager::getRepository(PresentationCategory::class); } ); + + App::singleton( + IRSVPTemplateRepository::class, + function(){ + return EntityManager::getRepository(RSVPTemplate::class); + } + ); } } \ No newline at end of file diff --git a/app/Repositories/Summit/DoctrineRSVPTemplateRepository.php b/app/Repositories/Summit/DoctrineRSVPTemplateRepository.php new file mode 100644 index 00000000..10449ea9 --- /dev/null +++ b/app/Repositories/Summit/DoctrineRSVPTemplateRepository.php @@ -0,0 +1,33 @@ +setAbstract(html_entity_decode(trim($data['description']))); + if(isset($data['rsvp_link']) && isset($data['rsvp_template_id'])){ + throw new ValidationException("rsvp_link and rsvp_template_id are both set, you need to especify only one"); + } + if (isset($data['rsvp_link'])) $event->setRsvpLink(html_entity_decode(trim($data['rsvp_link']))); + if (isset($data['rsvp_template_id'])) { + + $rsvp_template = $summit->getRSVPTemplateById(intval($data['rsvp_template_id'])); + + if(is_null($rsvp_template)) + throw new EntityNotFoundException(sprintf('rsvp template id %s does not belongs to summit id %s', $data['rsvp_template_id'], $summit->getId())); + + if(!$rsvp_template->isEnabled()) + throw new ValidationException(sprintf('rsvp template id %s is not enabled', $data['rsvp_template_id'])); + + $event->setRSVPTemplate($rsvp_template); + } + if (isset($data['head_count'])) $event->setHeadCount(intval($data['head_count'])); diff --git a/tests/OAuth2SummitEventsApiTest.php b/tests/OAuth2SummitEventsApiTest.php index 1551d465..2337b977 100644 --- a/tests/OAuth2SummitEventsApiTest.php +++ b/tests/OAuth2SummitEventsApiTest.php @@ -12,7 +12,7 @@ * limitations under the License. **/ -class OAuth2SummitEventsApiTest extends ProtectedApiTest +final class OAuth2SummitEventsApiTest extends ProtectedApiTest { public function testPostEvent($summit_id = 23, $location_id = 0, $type_id = 0, $track_id = 0, $start_date = 1477645200, $end_date = 1477647600) { @@ -68,6 +68,114 @@ class OAuth2SummitEventsApiTest extends ProtectedApiTest return $event; } + public function testPostEventRSVPTemplateUnExistent($summit_id = 23, $location_id = 0, $type_id = 124, $track_id = 208, $start_date = 1477645200, $end_date = 1477647600) + { + $params = array + ( + 'id' => $summit_id, + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $data = array + ( + 'title' => 'Neutron: tbd', + 'description' => 'TBD', + 'allow_feedback' => true, + 'type_id' => $type_id, + 'tags' => ['Neutron'], + 'track_id' => $track_id, + 'rsvp_template_id' => 1, + ); + + if($start_date > 0){ + $data['start_date'] = $start_date; + } + + if($end_date > 0){ + $data['end_date'] = $end_date; + } + + if($location_id > 0){ + $data['location_id'] = $location_id; + } + + $response = $this->action + ( + "POST", + "OAuth2SummitEventsApiController@addEvent", + $params, + array(), + array(), + array(), + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(412); + } + + public function testPostEventRSVPTemplate($summit_id = 23, $location_id = 0, $type_id = 124, $track_id = 208, $start_date = 0, $end_date = 0) + { + $params = array + ( + 'id' => $summit_id, + ); + + $headers = array + ( + "HTTP_Authorization" => " Bearer " . $this->access_token, + "CONTENT_TYPE" => "application/json" + ); + + $data = array + ( + 'title' => 'Neutron: tbd', + 'description' => 'TBD', + 'allow_feedback' => true, + 'type_id' => $type_id, + 'tags' => ['Neutron'], + 'track_id' => $track_id, + 'rsvp_template_id' => 12, + ); + + if($start_date > 0){ + $data['start_date'] = $start_date; + } + + if($end_date > 0){ + $data['end_date'] = $end_date; + } + + if($location_id > 0){ + $data['location_id'] = $location_id; + } + + $response = $this->action + ( + "POST", + "OAuth2SummitEventsApiController@addEvent", + $params, + array(), + array(), + array(), + $headers, + json_encode($data) + ); + + $content = $response->getContent(); + $this->assertResponseStatus(201); + $event = json_decode($content); + $this->assertTrue($event->id > 0); + $this->assertTrue(!$event->rsvp_external); + return $event; + } + public function testPostPresentationFail412($start_date = 1461510000, $end_date = 1461513600) { $params = array