Commit c56d9cbe authored by Guy Thouret's avatar Guy Thouret

(chore) Support score and score_decimal fields in Rewards\Contributions\Sums...

(chore) Support score and score_decimal fields in Rewards\Contributions\Sums during migration of data - Fixes #937
1 merge request!285Reward Factors for User Activity
Pipeline #89239502 passed with stages
in 12 minutes and 18 seconds
......@@ -74,27 +74,31 @@ class Sums
public function getScore()
{
$query = new Custom();
$score = (float)0;
if ($this->user) {
$query->query("SELECT SUM(score_decimal) as score from contributions WHERE user_guid = ?
foreach (['score', 'score_decimal'] as $scoreField) {
if ($this->user) {
$query->query("SELECT SUM(${scoreField}) as score from contributions WHERE user_guid = ?
AND timestamp = ?",
[
new Varint((int) $this->user->guid),
new Timestamp($this->timestamp / 1000)
]);
} else {
$query->query("SELECT SUM(score_decimal) as score from contributions_by_timestamp WHERE timestamp = ?",
[
new Timestamp($this->timestamp / 1000)
]);
}
[
new Varint((int)$this->user->guid),
new Timestamp($this->timestamp / 1000)
]);
} else {
$query->query("SELECT SUM(${scoreField}) as score from contributions_by_timestamp WHERE timestamp = ?",
[
new Timestamp($this->timestamp / 1000)
]);
}
try {
$rows = $this->db->request($query);
} catch (\Exception $e) {
error_log($e->getMessage());
try {
$rows = $this->db->request($query);
$score += (float)$rows[0]['score'];
} catch (\Exception $e) {
error_log($e->getMessage());
}
}
return (float) $rows[0]['score'];
return $score;
}
}
......@@ -2,27 +2,64 @@
namespace Spec\Minds\Core\Rewards\Contributions;
use Minds\Core\Data\Cassandra\Prepared\Custom;
use Minds\Entities\User;
use PhpSpec\ObjectBehavior;
use Prophecy\Argument;
use Minds\Core\Data\Cassandra\Client;
class SumsSpec extends ObjectBehavior
{
/** @var Client */
protected $db;
public function it_is_initializable()
{
$this->shouldHaveType('Minds\Core\Rewards\Contributions\Sums');
}
public function it_sould_get_a_balance(Client $db)
public function let(Client $db)
{
$this->beConstructedWith($db);
$this->db = $db;
}
public function it_sould_get_a_balance()
{
$this->db->request(Argument::any())->willReturn([
['amount' => 12]
]);
$this->getAmount()->shouldReturn('12');
}
$db->request(Argument::any())
->willReturn([
[ 'amount' => 12 ]
]);
public function it_should_get_the_score_for_all_users()
{
$scoreData = [
['score' => 146]
];
$scoreDecimalData = [
['score' => 23.65]
];
$this->setTimestamp(1571220000000);
$this->db->request(Argument::type(Custom::class))->willReturn($scoreData, $scoreDecimalData);
$this->getScore()->shouldReturn(169.65);
}
public function it_should_get_the_score_for_a_user(User $user)
{
$scoreData = [
['score' => 146]
];
$scoreDecimalData = [
['score' => 23.65]
];
$this->getAmount()
->shouldReturn('12');
$this->setTimestamp(1571220000000);
$this->setUser($user);
$user->get('guid')->shouldBeCalled()->willReturn(1001);
$this->db->request(Argument::type(Custom::class))->willReturn($scoreData, $scoreDecimalData);
$this->getScore()->shouldReturn(169.65);
}
}
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