Commit 5a36078a authored by Mark Harding's avatar Mark Harding

(fix): debit on duplicate boosts should not happen

parent e8162f59
No related merge requests found
Pipeline #66600982 (#760) failed with stages
in 6 minutes and 5 seconds
......@@ -275,6 +275,7 @@ class boost implements Interfaces\Api
$state = 'pending';
}
/** @var Network\Manager $manager */
$manager = Di::_()->get('Boost\Network\Manager');
$boost = (new Network\Boost())
......@@ -289,6 +290,13 @@ class boost implements Interfaces\Api
->setType(lcfirst($pages[0]))
->setPriority(false);
if ($manager->checkExisting($boost)) {
return Factory::response([
'status' => 'error',
'message' => "There's already an ongoing boost for this entity"
]);
}
// Pre-set GUID
if ($bidType == 'tokens' && isset($_POST['guid'])) {
......
......@@ -121,18 +121,6 @@ class Manager
*/
public function add($boost)
{
$existingBoost = $this->getList([
'useElastic' => true,
'state' => 'review',
'type' => $boost->getType(),
'entity_guid' => $boost->getEntityGuid(),
'limit' => 1
]);
if ($existingBoost->count() > 0) {
throw new EntityAlreadyBoostedException();
}
if (!$boost->getGuid()) {
$boost->setGuid($this->guidBuilder->build());
}
......@@ -151,4 +139,22 @@ class Manager
{
$this->elasticRepository->update($boost, $fields);
}
/**
* Checks if a boost already exists for a given entity
* @param $boost
* @return bool
*/
public function checkExisting($boost)
{
$existingBoost = $this->getList([
'useElastic' => true,
'state' => 'review',
'type' => $boost->getType(),
'entity_guid' => $boost->getEntityGuid(),
'limit' => 1
]);
return $existingBoost->count() > 0;
}
}
......@@ -3,7 +3,6 @@
namespace Spec\Minds\Core\Boost\Network;
use Minds\Common\Repository\Response;
use Minds\Core\Boost\Exceptions\EntityAlreadyBoostedException;
use Minds\Core\Boost\Network\Boost;
use Minds\Core\Boost\Network\ElasticRepository;
use Minds\Core\Boost\Network\Manager;
......@@ -232,18 +231,6 @@ class ManagerSpec extends ObjectBehavior
function it_should_add_a_boost(Boost $boost)
{
$boost->getType()
->shouldBeCalled()
->willReturn('network');
$boost->getEntityGuid()
->shouldBeCalled()
->willReturn('2');
$this->elasticRepository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response());
$this->guidBuilder->build()
->shouldBeCalled()
->willReturn(1);
......@@ -264,27 +251,6 @@ class ManagerSpec extends ObjectBehavior
->shouldReturn(true);
}
function it_should_not_add_a_boost_if_the_entity_has_already_been_boosted(Boost $boost, Boost $oldBoost)
{
$boost->getType()
->shouldBeCalled()
->willReturn('network');
$boost->getEntityGuid()
->shouldBeCalled()
->willReturn('2');
$this->elasticRepository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$oldBoost]));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$oldBoost]));
$this->shouldThrow(new EntityAlreadyBoostedException())->during('add', [$boost]);
}
function it_should_update_a_boost(Boost $boost)
{
$this->repository->update($boost, ['@timestamp'])
......@@ -302,4 +268,32 @@ class ManagerSpec extends ObjectBehavior
$this->resync($boost, ['@timestamp']);
}
function it_should_check_if_the_entity_was_already_boosted(Boost $boost)
{
$this->elasticRepository->getList([
'useElastic' => true,
'state' => 'review',
'type' => 'newsfeed',
'entity_guid' => '123',
'limit' => 1,
'hydrate' => true,
])
->shouldBeCalled()
->willReturn(new Response([$boost], ''));
$this->repository->getList(Argument::any())
->shouldBeCalled()
->willReturn(new Response([$boost]));
$boost->getType()
->shouldBeCalled()
->willReturn('newsfeed');
$boost->getEntityGuid()
->shouldBeCalled()
->willReturn('123');
$this->checkExisting($boost)->shouldReturn(true);
}
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment