Fixed bugs on reorder endpoint

Change-Id: I2682421901518c4454a8078e70511dc205cb3770
Signed-off-by: smarcet <smarcet@gmail.com>
This commit is contained in:
smarcet 2021-03-22 14:08:23 -03:00
parent 50ab30d74e
commit 07d8884d2f
5 changed files with 253 additions and 5 deletions

View File

@ -218,7 +218,7 @@ class OAuth2SummitSelectedPresentationListApiController
$validation = Validator::make($payload,[
'hash' => 'sometimes|nullable|string',
'collection' => sprintf('required|string|in:%s,%s', SummitSelectedPresentation::CollectionMaybe, SummitSelectedPresentation::CollectionSelected),
'presentations' => 'required|int_array',
'presentations' => 'nullable|sometimes|int_array',
]);
if ($validation->fails()) {

View File

@ -14,6 +14,7 @@
use Doctrine\ORM\Mapping AS ORM;
use Doctrine\Common\Collections\Criteria;
use Illuminate\Support\Facades\Log;
use models\exceptions\ValidationException;
use models\utils\SilverstripeBaseModel;
use Doctrine\Common\Collections\ArrayCollection;
@ -303,15 +304,18 @@ class SummitSelectedPresentationList extends SilverstripeBaseModel
return $res === false ? null : $res;
}
public function recalculateHash():string{
Log::debug(sprintf("recalculating hash for list %s", $this->id));
if(!$this->isGroup()){
throw new ValidationException("You could only calculate hash on Team list");
}
$criteria = Criteria::create();
$criteria->orderBy(['order'=> 'ASC']);
$hash = '';
foreach ($this->selected_presentations->matching($criteria) as $p){
$hash .= strval($p->getId());
$hash .= strval($p->getPresentationId());
}
$this->hash = md5($hash);

View File

@ -193,7 +193,8 @@ final class SummitSelectedPresentationListService
if (is_null($category) || !$category instanceof PresentationCategory || !$category->isChairVisible()) throw new EntityNotFoundException("Track not found.");
$selection_list = $category->getSelectionListById($list_id);
if (is_null($selection_list)) throw new EntityNotFoundException("List not found.");
if (is_null($selection_list))
throw new EntityNotFoundException("List not found.");
$current_member = $this->resource_server_ctx->getCurrentUser();
@ -217,7 +218,7 @@ final class SummitSelectedPresentationListService
if ($selection_list->isGroup()){
if(empty($payload['hash']))
if(!isset($payload['hash']))
throw new ValidationException(sprintf("hash attributed is mandatory for list %s.", $selection_list->getId()));
if(!$selection_list->compareHash(trim($payload['hash'])))
@ -256,7 +257,13 @@ final class SummitSelectedPresentationListService
// check if the selection already exists on the current list
$selection = $selection_list->getSelectionByPresentation($presentation);
if(is_null($selection) || $selection->getCollection() !== trim($payload['collection'])) {
if(!is_null($selection) && $selection->getCollection() !== trim($payload['collection'])){
// we should remove it from original collection
$selection_list->removeSelection($selection);
$selection = null;
}
if(is_null($selection)) {
// selection does not exists , create it
$selection = SummitSelectedPresentation::create
(

View File

@ -0,0 +1,42 @@
<?php namespace Database\Migrations\Model;
/**
* Copyright 2019 OpenStack Foundation
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
**/
use Doctrine\Migrations\AbstractMigration;
use Doctrine\DBAL\Schema\Schema as Schema;
/**
* Class Version20210322170708
* @package Database\Migrations\Model
*/
class Version20210322170708 extends AbstractMigration
{
/**
* @param Schema $schema
*/
public function up(Schema $schema)
{
$sql = <<<SQL
create index SummitSelectedPresentation_Presentation_List_Unique
on SummitSelectedPresentation (PresentationID, SummitSelectedPresentationListID);
SQL;
$this->addSql($sql);
}
/**
* @param Schema $schema
*/
public function down(Schema $schema)
{
}
}

View File

@ -205,6 +205,79 @@ final class OAuth2SummitSelectedPresentationListApiTest
return $selection_list;
}
/**
* @return mixed
*/
public function testAddGroupSelectionListAndReorder(){
$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@createTeamSelectionList",
$params,
[],
[],
[],
$headers,
""
);
$content = $response->getContent();
$this->assertResponseStatus(201);
$selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list));
$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()
],
'hash' => ''
];
$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);
$this->assertTrue(property_exists($selection_list, 'hash'));
$this->assertTrue(!empty($selection_list->hash));
return $selection_list;
}
/**
* @return mixed
*/
@ -302,4 +375,126 @@ final class OAuth2SummitSelectedPresentationListApiTest
return $selection_list;
}
/**
* @return mixed
*/
public function testAddIndividualSelectionListAndReorderRemoveAll(){
$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();
$this->assertResponseStatus(201);
$selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list));
$params = [
'id' => self::$summit->getId(),
'track_id' => self::$defaultTrack->getId(),
'collection' => SummitSelectedPresentation::CollectionSelected,
'presentation_id' => self::$presentations[0]->getId(),
'expand' => 'selected_presentations,interested_presentations,'
];
$headers = [
"HTTP_Authorization" => " Bearer " . $this->access_token,
"CONTENT_TYPE" => "application/json"
];
$response = $this->action(
"POST",
"OAuth2SummitSelectedPresentationListApiController@assignPresentationToMyIndividualList",
$params,
[],
[],
[],
$headers,
""
);
$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"
];
// add
$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();
$this->assertResponseStatus(201);
$selection_list = json_decode($content);
$this->assertTrue(!is_null($selection_list));
$this->assertTrue(count($selection_list->selected_presentations) == 2);
$data = [
'collection' => SummitSelectedPresentation::CollectionSelected,
'presentations' => [
]
];
// remove all
$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) == 0);
return $selection_list;
}
}