Fixed track chairs bugs

Change-Id: I22f9b1fcad456c01ec3d151b03873a83a7a2afb4
Signed-off-by: smarcet <smarcet@gmail.com>
This commit is contained in:
smarcet 2021-02-22 17:54:03 -03:00
parent 8084e257a3
commit a86a6c576f
10 changed files with 300 additions and 34 deletions

View File

@ -216,7 +216,7 @@ class OAuth2SummitSelectedPresentationListApiController
$payload = $data->all(); $payload = $data->all();
// Creates a Validator instance and validates the data. // Creates a Validator instance and validates the data.
$validation = Validator::make($payload,[ $validation = Validator::make($payload,[
'hash' => 'required|string', 'hash' => 'sometimes|string',
'collection' => sprintf('required|string|in:%s,%s', SummitSelectedPresentation::CollectionMaybe, SummitSelectedPresentation::CollectionSelected), 'collection' => sprintf('required|string|in:%s,%s', SummitSelectedPresentation::CollectionMaybe, SummitSelectedPresentation::CollectionSelected),
'presentations' => 'required|int_array', 'presentations' => 'required|int_array',
]); ]);

View File

@ -12,6 +12,7 @@
* limitations under the License. * limitations under the License.
**/ **/
use Libs\ModelSerializers\AbstractSerializer;
use models\summit\PresentationCategory; use models\summit\PresentationCategory;
/** /**
@ -52,6 +53,7 @@ final class PresentationCategorySerializer extends SilverStripeSerializer
$groups = []; $groups = [];
$allowed_tag = []; $allowed_tag = [];
$extra_questions = []; $extra_questions = [];
$selection_lists = [];
$summit = $category->getSummit(); $summit = $category->getSummit();
foreach ($category->getGroups() as $group) { foreach ($category->getGroups() as $group) {
@ -66,26 +68,32 @@ final class PresentationCategorySerializer extends SilverStripeSerializer
$extra_questions[] = intval($question->getId()); $extra_questions[] = intval($question->getId());
} }
foreach ($category->getSelectionLists() as $list) {
$selection_lists[] = intval($list->getId());
}
$values['track_groups'] = $groups; $values['track_groups'] = $groups;
$values['allowed_tags'] = $allowed_tag; $values['allowed_tags'] = $allowed_tag;
$values['extra_questions'] = $extra_questions; $values['extra_questions'] = $extra_questions;
$values['selection_lists'] = $selection_lists;
if (!empty($expand)) { if (!empty($expand)) {
$exp_expand = explode(',', $expand); $exp_expand = explode(',', $expand);
foreach ($exp_expand as $relation) { foreach ($exp_expand as $relation) {
switch (trim($relation)) { $relation = trim($relation);
switch ($relation) {
case 'track_groups': case 'track_groups':
{ {
$groups = []; $groups = [];
unset($values['track_groups']); unset($values['track_groups']);
foreach ($category->getGroups() as $g) { foreach ($category->getGroups() as $g) {
$groups[] = SerializerRegistry::getInstance()->getSerializer($g)->serialize(null, [], ['none']); $groups[] = SerializerRegistry::getInstance()->getSerializer($g)->serialize(AbstractSerializer::filterExpandByPrefix($expand, $relation));
} }
$values['track_groups'] = $groups; $values['track_groups'] = $groups;
} }
break; break;
}
switch (trim($relation)) {
case 'allowed_tags': case 'allowed_tags':
{ {
$allowed_tags = []; $allowed_tags = [];
@ -101,19 +109,30 @@ final class PresentationCategorySerializer extends SilverStripeSerializer
$values['allowed_tags'] = $allowed_tags; $values['allowed_tags'] = $allowed_tags;
} }
break; break;
}
switch (trim($relation)) {
case 'extra_questions': case 'extra_questions':
{ {
$extra_questions = []; $extra_questions = [];
unset($values['extra_questions']); unset($values['extra_questions']);
foreach ($category->getExtraQuestions() as $question) { foreach ($category->getExtraQuestions() as $question) {
$extra_questions[] = SerializerRegistry::getInstance()->getSerializer($question)->serialize(null, [], ['none']); $extra_questions[] = SerializerRegistry::getInstance()->getSerializer($question)->serialize(AbstractSerializer::filterExpandByPrefix($expand, $relation));
} }
$values['extra_questions'] = $extra_questions; $values['extra_questions'] = $extra_questions;
} }
break; break;
case 'selection_lists':
{
$selection_lists = [];
unset($values['selection_lists']);
foreach ($category->getSelectionLists() as $list) {
$selection_lists[] = SerializerRegistry::getInstance()->getSerializer($list)->serialize(AbstractSerializer::filterExpandByPrefix($expand, $relation));
} }
$values['selection_lists'] = $selection_lists;
}
break;
}
} }
} }

View File

@ -30,15 +30,6 @@ final class SummitSelectedPresentationListSerializer extends SilverStripeSeriali
'OwnerId' => 'owner_id:json_int', 'OwnerId' => 'owner_id:json_int',
]; ];
protected static $allowed_fields = [
'name',
'type',
'popularity_score',
'hash',
'category_id',
'owner_id',
];
protected static $allowed_relations = [ protected static $allowed_relations = [
'selected_presentations', 'selected_presentations',
'interested_presentations', 'interested_presentations',

View File

@ -26,11 +26,6 @@ final class SummitTrackChairSerializer extends SilverStripeSerializer
'MemberId' => 'member_id:json_int', 'MemberId' => 'member_id:json_int',
]; ];
protected static $allowed_fields = [
'summit_id',
'member_id',
];
protected static $allowed_relations = [ protected static $allowed_relations = [
'categories', 'categories',
]; ];

View File

@ -641,6 +641,13 @@ SQL;
return $list === false ? null : $list; return $list === false ? null : $list;
} }
/**
* @return ArrayCollection|SummitSelectedPresentationList[]
*/
public function getSelectionLists(){
return $this->selection_lists;
}
/** /**
* @param int $list_id * @param int $list_id
* @return SummitSelectedPresentationList|null * @return SummitSelectedPresentationList|null

View File

@ -80,6 +80,9 @@ class SummitSelectedPresentationList extends SilverstripeBaseModel
*/ */
private $selected_presentations; private $selected_presentations;
/**
* SummitSelectedPresentationList constructor.
*/
public function __construct() public function __construct()
{ {
parent::__construct(); parent::__construct();

View File

@ -64,6 +64,8 @@ class SummitTrackChair extends SilverstripeBaseModel
* @throws ValidationException * @throws ValidationException
*/ */
public function addCategory(PresentationCategory $track){ public function addCategory(PresentationCategory $track){
if($this->categories->contains($track))
return;
if(!$track->isChairVisible()) if(!$track->isChairVisible())
throw new ValidationException(sprintf("Category %s is not visible by track chairs.", $track->getId())); throw new ValidationException(sprintf("Category %s is not visible by track chairs.", $track->getId()));
@ -73,10 +75,19 @@ class SummitTrackChair extends SilverstripeBaseModel
/** /**
* @param PresentationCategory $track * @param PresentationCategory $track
* @throws ValidationException
*/ */
public function removeCategory(PresentationCategory $track){ public function removeCategory(PresentationCategory $track){
if(!$this->categories->contains($track))
return;
$track->removeFromTrackChairs($this); $track->removeFromTrackChairs($this);
$this->categories->removeElement($track); $this->categories->removeElement($track);
$list = $track->getSelectionListByTypeAndOwner(SummitSelectedPresentationList::Individual, $track, $this->member);
// if we remove the track , then we need to remove the selection lists
if(!is_null($list)){
$track->removeSelectionList($list);
}
} }
/** /**

View File

@ -16,6 +16,7 @@ use App\Models\Exceptions\AuthzException;
use App\Models\Foundation\Main\IGroup; use App\Models\Foundation\Main\IGroup;
use App\Services\Model\AbstractService; use App\Services\Model\AbstractService;
use App\Services\Model\ITrackChairService; use App\Services\Model\ITrackChairService;
use Illuminate\Support\Facades\Log;
use libs\utils\ITransactionService; use libs\utils\ITransactionService;
use models\exceptions\EntityNotFoundException; use models\exceptions\EntityNotFoundException;
use models\exceptions\ValidationException; use models\exceptions\ValidationException;
@ -25,7 +26,9 @@ use models\main\Member;
use models\oauth2\IResourceServerContext; use models\oauth2\IResourceServerContext;
use models\summit\PresentationCategory; use models\summit\PresentationCategory;
use models\summit\Summit; use models\summit\Summit;
use models\summit\SummitSelectedPresentationList;
use models\summit\SummitTrackChair; use models\summit\SummitTrackChair;
use function Psy\debug;
/** /**
* Class TrackChairService * Class TrackChairService
@ -111,12 +114,41 @@ final class TrackChairService
foreach ($payload['categories'] as $track_id) { foreach ($payload['categories'] as $track_id) {
$track = $summit->getPresentationCategory(intval($track_id)); $track = $summit->getPresentationCategory(intval($track_id));
if (is_null($track) || !$track instanceof PresentationCategory) if (is_null($track) || !$track instanceof PresentationCategory || !$track->isChairVisible())
throw new EntityNotFoundException(sprintf("Presentation Category %s not found.", $track_id)); throw new EntityNotFoundException(sprintf("Presentation Category %s not found.", $track_id));
$categories[] = $track; $categories[] = $track;
} }
return $summit->addTrackChair($member, $categories); $track_chair = $summit->addTrackChair($member, $categories);
foreach ($categories as $category) {
$individual_selection_list = $category->getSelectionListByTypeAndOwner(SummitSelectedPresentationList::Individual, $category, $member);
if (is_null($individual_selection_list)) {
$individual_selection_list = new SummitSelectedPresentationList();
Log::debug(sprintf("TrackChaiService::addTrackChair adding individual list for track %s and member %s", $category->getId(), $member->getId()));
$individual_selection_list->setName(sprintf("%s Individual Selection List for %s", $member->getFullName(), $category->getTitle()));
$individual_selection_list->setListType(SummitSelectedPresentationList::Individual);
$individual_selection_list->setListClass(SummitSelectedPresentationList::Session);
$individual_selection_list->setOwner($member);
$category->addSelectionList($individual_selection_list);
}
$team_selection_list = $category->getSelectionListByTypeAndOwner(SummitSelectedPresentationList::Group, $category);
if (is_null($team_selection_list)) {
Log::debug(sprintf("TrackChaiService::addTrackChair adding team list for track %s", $category->getId()));
$team_selection_list = new SummitSelectedPresentationList();
$team_selection_list->setName(sprintf("Team Selections for %s", $category->getTitle()));
$team_selection_list->setListType(SummitSelectedPresentationList::Group);
$team_selection_list->setListClass(SummitSelectedPresentationList::Session);
$category->addSelectionList($team_selection_list);
}
}
return $track_chair;
}); });
} }
@ -144,13 +176,45 @@ final class TrackChairService
if(is_null($track_chair)) if(is_null($track_chair))
throw new EntityNotFoundException(sprintf("Track Chair %s not found.", $track_chair_id)); throw new EntityNotFoundException(sprintf("Track Chair %s not found.", $track_chair_id));
$track_chair->clearCategories(); $categories_2_remove = [];
foreach ($track_chair->getCategories() as $category) {
if (!in_array($category->getId(), $payload['categories']))
$categories_2_remove[] = $category;
}
foreach ($payload['categories'] as $track_id) { foreach ($payload['categories'] as $track_id) {
$track = $summit->getPresentationCategory(intval($track_id)); $category = $summit->getPresentationCategory(intval($track_id));
if (is_null($track) || !$track instanceof PresentationCategory) if (is_null($category) || !$category instanceof PresentationCategory || !$category->isChairVisible())
throw new EntityNotFoundException(sprintf("Presentation Category %s not found.", $track_id)); throw new EntityNotFoundException(sprintf("Presentation Category %s not found.", $track_id));
$track_chair->addCategory($track);
$track_chair->addCategory($category);
$individual_selection_list = $category->getSelectionListByTypeAndOwner(SummitSelectedPresentationList::Individual, $category, $track_chair->getMember());
if (is_null($individual_selection_list)) {
$individual_selection_list = new SummitSelectedPresentationList();
Log::debug(sprintf("TrackChaiService::updateTrackChair adding individual list for track %s and member %s", $category->getId(), $track_chair->getMemberId()));
$individual_selection_list->setName(sprintf("%s Individual Selection List for ", $track_chair->getMember()->getFullName(), $category->getTitle()));
$individual_selection_list->setListType(SummitSelectedPresentationList::Individual);
$individual_selection_list->setListClass(SummitSelectedPresentationList::Session);
$individual_selection_list->setOwner($current_member);
$category->addSelectionList($individual_selection_list);
}
$team_selection_list = $category->getSelectionListByTypeAndOwner(SummitSelectedPresentationList::Group, $category);
if (is_null($team_selection_list)) {
Log::debug(sprintf("TrackChaiService::updateTrackChair adding team list for track %s", $category->getId()));
$team_selection_list = new SummitSelectedPresentationList();
$team_selection_list->setName(sprintf("Team Selections for %s", $category->getTitle()));
$team_selection_list->setListType(SummitSelectedPresentationList::Group);
$team_selection_list->setListClass(SummitSelectedPresentationList::Session);
$category->addSelectionList($team_selection_list);
}
}
foreach ($categories_2_remove as $category){
$track_chair->removeCategory($category);
} }
return $track_chair; return $track_chair;
@ -158,7 +222,9 @@ final class TrackChairService
} }
/** /**
* @inheritDoc * @param Summit $summit
* @param int $track_chair_id
* @throws \Exception
*/ */
public function deleteTrackChair(Summit $summit, int $track_chair_id): void public function deleteTrackChair(Summit $summit, int $track_chair_id): void
{ {
@ -177,6 +243,10 @@ final class TrackChairService
if(is_null($track_chair)) if(is_null($track_chair))
throw new EntityNotFoundException(sprintf("Track Chair %s not found.", $track_chair_id)); throw new EntityNotFoundException(sprintf("Track Chair %s not found.", $track_chair_id));
foreach($track_chair->getCategories() as $category){
$track_chair->removeCategory($category);
}
$summit->removeTrackChair($track_chair); $summit->removeTrackChair($track_chair);
}); });
} }

View File

@ -110,7 +110,7 @@ final class OAuth2SummitSelectedPresentationListApiTest
/** /**
* @return mixed * @return mixed
*/ */
public function testAddIndividualSelectionListAndAddSelectionAndRemoveIt(){ public function testAddIndividualSelectionListAndReorder(){
$params = [ $params = [
'id' => self::$summit->getId(), 'id' => self::$summit->getId(),
@ -162,11 +162,79 @@ final class OAuth2SummitSelectedPresentationListApiTest
"" ""
); );
$content = $response->getContent();
$this->assertResponseStatus(201);
$params = [
'id' => self::$summit->getId(),
'track_id' => self::$defaultTrack->getId(),
'list_id' => $selection_list->id,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$data = [
'collection' => SummitSelectedPresentation::CollectionSelected,
'presentations' => [
self::$presentations[1]->getId(),
self::$presentations[0]->getId(),
self::$presentations[2]->getId()
]
];
$response = $this->action(
"PUT",
"OAuth2SummitSelectedPresentationListApiController@reorderSelectionList",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list));
$this->assertTrue(count($selection_list->selected_presentations) == 3);
return $selection_list;
}
/**
* @return mixed
*/
public function testAddIndividualSelectionListAndReorderRemove(){
$params = [
'id' => self::$summit->getId(),
'track_id' => self::$defaultTrack->getId(),
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2SummitSelectedPresentationListApiController@createIndividualSelectionList",
$params,
[],
[],
[],
$headers,
""
);
$content = $response->getContent(); $content = $response->getContent();
$this->assertResponseStatus(201); $this->assertResponseStatus(201);
$selection_list = json_decode($content); $selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list)); $this->assertTrue(!is_null($selection_list));
$this->assertTrue(count($selection_list->selected_presentations) > 0);
$params = [ $params = [
'id' => self::$summit->getId(), 'id' => self::$summit->getId(),
@ -182,8 +250,8 @@ final class OAuth2SummitSelectedPresentationListApiTest
]; ];
$response = $this->action( $response = $this->action(
"DELETE", "POST",
"OAuth2SummitSelectedPresentationListApiController@removePresentationFromMyIndividualList", "OAuth2SummitSelectedPresentationListApiController@assignPresentationToMyIndividualList",
$params, $params,
[], [],
[], [],
@ -192,11 +260,44 @@ final class OAuth2SummitSelectedPresentationListApiTest
"" ""
); );
$content = $response->getContent();
$this->assertResponseStatus(201);
$params = [
'id' => self::$summit->getId(),
'track_id' => self::$defaultTrack->getId(),
'list_id' => $selection_list->id,
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$data = [
'collection' => SummitSelectedPresentation::CollectionSelected,
'presentations' => [
self::$presentations[1]->getId(),
self::$presentations[2]->getId()
]
];
$response = $this->action(
"PUT",
"OAuth2SummitSelectedPresentationListApiController@reorderSelectionList",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent(); $content = $response->getContent();
$this->assertResponseStatus(201); $this->assertResponseStatus(201);
$selection_list = json_decode($content); $selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list)); $this->assertTrue(!is_null($selection_list));
$this->assertTrue(count($selection_list->selected_presentations) == 0); $this->assertTrue(count($selection_list->selected_presentations) == 2);
return $selection_list; return $selection_list;
} }

View File

@ -76,6 +76,7 @@ class OAuth2SummitTrackChairsApiTest extends \ProtectedApiTest
} }
public function testAddTrackChair(){ public function testAddTrackChair(){
$params = [ $params = [
'id' => self::$summit->getId(), 'id' => self::$summit->getId(),
'expand' => 'member,categories' 'expand' => 'member,categories'
@ -108,4 +109,72 @@ class OAuth2SummitTrackChairsApiTest extends \ProtectedApiTest
$this->assertTrue(!is_null($track_chair)); $this->assertTrue(!is_null($track_chair));
} }
public function testUpdateTrackChair(){
$params = [
'id' => self::$summit->getId(),
'expand' => 'member,categories,categories.selection_lists'
];
$data = [
'member_id' => self::$member2->getId(),
'categories' => [self::$defaultTrack->getId()]
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2SummitTrackChairsApiController@add",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$track_chair = json_decode($content);
$this->assertTrue(!is_null($track_chair));
$params = [
'id' => self::$summit->getId(),
'track_chair_id' => $track_chair->id,
'expand' => 'member,categories,categories.selection_lists'
];
$data = [
'member_id' => self::$member2->getId(),
'categories' => [
self::$secondaryTrack->getId()
]
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"PUT",
"OAuth2SummitTrackChairsApiController@update",
$params,
[],
[],
[],
$headers,
json_encode($data)
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$track_chair = json_decode($content);
$this->assertTrue(!is_null($track_chair));
}
} }