diff --git a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php index c708db59..6de34e47 100644 --- a/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php +++ b/app/Http/Controllers/Apis/Protected/Summit/OAuth2SummitTracksApiController.php @@ -384,6 +384,7 @@ final class OAuth2SummitTracksApiController extends OAuth2ProtectedController 'lightning_alternate_count' => 'sometimes|integer', 'voting_visible' => 'sometimes|boolean', 'chair_visible' => 'sometimes|boolean', + 'allowed_tags' => 'sometimes|string_array', ]; // Creates a Validator instance and validates the data. @@ -482,6 +483,7 @@ final class OAuth2SummitTracksApiController extends OAuth2ProtectedController 'lightning_alternate_count' => 'sometimes|integer', 'voting_visible' => 'sometimes|boolean', 'chair_visible' => 'sometimes|boolean', + 'allowed_tags' => 'sometimes|string_array', ]; // Creates a Validator instance and validates the data. diff --git a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php b/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php index aa4ebbe2..0f971c6f 100644 --- a/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php +++ b/app/Models/Foundation/Summit/Events/Presentations/PresentationCategory.php @@ -248,6 +248,18 @@ class PresentationCategory extends SilverstripeBaseModel return $this->allowed_tags; } + public function clearAllowedTags(){ + $this->allowed_tags->clear(); + } + + /** + * @param Tag $tag + */ + public function addAllowedTag(Tag $tag){ + if($this->allowed_tags->contains($tag)) return; + $this->allowed_tags->add($tag); + } + /** * @param int $group_id * @return PresentationCategoryGroup|null diff --git a/app/Models/Foundation/Summit/Events/SummitEvent.php b/app/Models/Foundation/Summit/Events/SummitEvent.php index dd08f998..fe26b49b 100644 --- a/app/Models/Foundation/Summit/Events/SummitEvent.php +++ b/app/Models/Foundation/Summit/Events/SummitEvent.php @@ -665,6 +665,7 @@ class SummitEvent extends SilverstripeBaseModel */ public function addTag(Tag $tag) { + if($this->tags->contains($tag)) return; $this->tags->add($tag); } diff --git a/app/Models/Foundation/Summit/Summit.php b/app/Models/Foundation/Summit/Summit.php index 9ddb5816..43e3163d 100644 --- a/app/Models/Foundation/Summit/Summit.php +++ b/app/Models/Foundation/Summit/Summit.php @@ -16,6 +16,7 @@ use App\Http\Utils\DateUtils; use App\Models\Foundation\Summit\Events\RSVP\RSVPTemplate; use App\Models\Foundation\Summit\SelectionPlan; use App\Models\Foundation\Summit\TrackTagGroup; +use App\Models\Foundation\Summit\TrackTagGroupAllowedTag; use App\Models\Utils\TimeZoneEntity; use DateTime; use Doctrine\Common\Collections\ArrayCollection; @@ -2225,6 +2226,56 @@ SQL; return count($res) > 0 ? $res[0] : null; } + /** + * @param string $tag_value + * @return bool + * @throws \Doctrine\ORM\NoResultException + * @throws \Doctrine\ORM\NonUniqueResultException + */ + public function isTagValueAllowedOnTrackTagGroups($tag_value){ + $query = <<getEM()->createQuery($query); + + $native_query->setParameter("summit", $this); + $native_query->setParameter("tag_value", $tag_value); + + $res = $native_query->getSingleScalarResult(); + return $res > 0; + } + + /** + * @param string $tag_value + * @return null|TrackTagGroupAllowedTag + */ + public function getAllowedTagOnTagTrackGroup($tag_value){ + $query = <<getEM()->createQuery($query); + + $native_query->setParameter("summit", $this); + $native_query->setParameter("tag_value", $tag_value); + + $res = $native_query->getResult(); + return count($res) > 0 ? $res[0] : null; + } + /** * @param int $tag_id * @return TrackTagGroup|null diff --git a/app/Services/Model/SummitTrackService.php b/app/Services/Model/SummitTrackService.php index fa9e53f5..7f4d99d1 100644 --- a/app/Services/Model/SummitTrackService.php +++ b/app/Services/Model/SummitTrackService.php @@ -21,6 +21,7 @@ use Illuminate\Support\Facades\Event; use libs\utils\ITransactionService; use models\exceptions\EntityNotFoundException; use models\exceptions\ValidationException; +use models\main\ITagRepository; use models\summit\PresentationCategory; use models\summit\Summit; /** @@ -36,14 +37,26 @@ final class SummitTrackService */ private $repository; + /** + * @var ITagRepository + */ + private $tag_repository; + /** * SummitTrackService constructor. * @param ISummitTrackRepository $repository + * @param ITagRepository $tag_repository * @param ITransactionService $tx_service */ - public function __construct(ISummitTrackRepository $repository, ITransactionService $tx_service) + public function __construct + ( + ISummitTrackRepository $repository, + ITagRepository $tag_repository, + ITransactionService $tx_service + ) { parent::__construct($tx_service); + $this->tag_repository = $tag_repository; $this->repository = $repository; } @@ -70,6 +83,18 @@ final class SummitTrackService $track = PresentationCategoryFactory::build($summit, $data); + if(isset($data['allowed_tags'])){ + foreach($data['allowed_tags'] as $tag_value) { + $tackTagGroupAllowedTag = $summit->getAllowedTagOnTagTrackGroup($tag_value); + if(is_null($tackTagGroupAllowedTag)){ + throw new ValidationException( + sprintf("allowed_tags : tag value %s is not allowed on current track tag groups for summit %s", $tag_value, $summit->getId()) + ); + } + $track->addAllowedTag($tackTagGroupAllowedTag->getTag()); + } + } + $summit->addPresentationCategory($track); return $track; @@ -114,6 +139,19 @@ final class SummitTrackService $track = PresentationCategoryFactory::populate($track, $data); + if(isset($data['allowed_tags'])){ + $track->clearAllowedTags(); + foreach($data['allowed_tags'] as $tag_value) { + $tackTagGroupAllowedTag = $summit->getAllowedTagOnTagTrackGroup($tag_value); + if(is_null($tackTagGroupAllowedTag)){ + throw new ValidationException( + sprintf("allowed_tags : tag value %s is not allowed on current track tag groups for summit %s", $tag_value, $summit->getId()) + ); + } + $track->addAllowedTag($tackTagGroupAllowedTag->getTag()); + } + } + Event::fire(new TrackUpdated($track->getSummitId(), $track->getId())); return $track; diff --git a/tests/OAuth2TracksApiTest.php b/tests/OAuth2TracksApiTest.php index 7b565bd0..3f70203f 100644 --- a/tests/OAuth2TracksApiTest.php +++ b/tests/OAuth2TracksApiTest.php @@ -195,16 +195,17 @@ final class OAuth2TracksApiTest extends ProtectedApiTest * @param int $summit_id * @return mixed */ - public function testAddTrack($summit_id = 23){ + public function testAddTrack($summit_id = 25){ $params = [ 'id' => $summit_id, ]; $name = str_random(16).'_track'; $data = [ - 'title' => $name, + 'name' => $name, 'description' => 'test desc', 'code' => '', + 'allowed_tags' => ['101','Case Study', 'Demo'], ]; $headers = [