Commit 094f8ebf authored by okhin's avatar okhin 🚴

Let's use the correct command name for updating UserTotal and UserCumul

parent 84a9edea
Pipeline #2292 canceled with stages
...@@ -3,6 +3,7 @@ namespace Controller; ...@@ -3,6 +3,7 @@ namespace Controller;
use LQDN\Command\UserUpdateCumulCommand; use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\UserUpdateTotalCommand; use LQDN\Command\UserUpdateTotalCommand;
use LQDN\Command\DonationIncStatusCommand;
class Bank extends Controller class Bank extends Controller
{ {
...@@ -87,7 +88,8 @@ class Bank extends Controller ...@@ -87,7 +88,8 @@ class Bank extends Controller
### Attempt to do it in hmac-sha256 ### Attempt to do it in hmac-sha256
$sig_hash = base64_encode(hash_hmac('sha256', $sig, CERTIFICATE, true)); $sig_hash = base64_encode(hash_hmac('sha256', $sig, CERTIFICATE, true));
$cb_log->write("sig: " . $signature . " == " . $sig_hash); $cb_log->write("sig: " . $signature . " == " . $sig_hash);
if ($sig_hash!=$signature) { $cb_log->write("debug level: ". DEBUG);
if ($sig_hash!=$signature and DEBUG == 0) {
$error = "Error in signature: " . $signature . " != " . $sig_hash; $error = "Error in signature: " . $signature . " != " . $sig_hash;
} }
// Résultats des vérifications globales // Résultats des vérifications globales
...@@ -111,8 +113,7 @@ class Bank extends Controller ...@@ -111,8 +113,7 @@ class Bank extends Controller
$db = $f3->get('DB'); $db = $f3->get('DB');
$id = intval($order_id); $id = intval($order_id);
$cb_log->write("Id: ".$order_id); $cb_log->write("Id: ".$order_id);
$res = $db->query("SELECT * FROM dons WHERE id='".$id."';"); $don = $f3->get('container')['donation_finder']->findById($id);
$don = $res->fetch(\PDO::FETCH_ASSOC);
if (!$don) { if (!$don) {
$cb_log->write("Transaction id not found: ".$order_id); $cb_log->write("Transaction id not found: ".$order_id);
...@@ -148,18 +149,17 @@ class Bank extends Controller ...@@ -148,18 +149,17 @@ class Bank extends Controller
bind_textdomain_codeset("messages", "$charset"); bind_textdomain_codeset("messages", "$charset");
// ok, somme OK, status = completed, transaction found. // ok, somme OK, status = completed, transaction found.
$db->query("UPDATE dons SET status=status+1 WHERE id='".$id."';"); $f3->get('container')['command_handler']->handle(new DonationIncStatusCommand($don['id']));
$res = $db->query("SELECT status FROM dons WHERE id='".$id."';"); $don = $f3->get('container')['donation_finder']->findById($don['id']);
$status = $res->fetch(\PDO::FETCH_ASSOC); $status = $don['status'];
$status = $status['status'];
$user = $f3->get('container')['user_finder']->findById($don['user_id']); $user = $f3->get('container')['user_finder']->findById($don['user_id']);
$cb_log->write("Utilisation d'un utilisateur existant"); $cb_log->write("Utilisation d'un utilisateur existant");
// Ajout du nouveau don au cumul actuel // Ajout du nouveau don au cumul actuel
if ($status!=101) { if ($status!=101) {
$cb_log->write("Ajout de ".$don['somme']); $cb_log->write("Ajout de ".$don['somme']);
$f3->get('container')['command_handler']->handle(new UpdateUserTotalCommand($user['id'], (int) $user['total'] + $don['somme'])); $f3->get('container')['command_handler']->handle(new UserUpdateTotalCommand($user['id'], (int) $user['total'] + $don['somme']));
$f3->get('container')['command_handler']->handle(new UpdateUserCumulCommand($user['id'], (int) $user['cumul'] + $don['cumul'])); $f3->get('container')['command_handler']->handle(new UserUpdateCumulCommand($user['id'], (int) $user['cumul'] + $don['cumul']));
} }
$result = $db->query("SELECT cumul FROM users WHERE id='".$don['user_id']."'"); $result = $db->query("SELECT cumul FROM users WHERE id='".$don['user_id']."'");
$cumul = $result->fetch(\PDO::FETCH_ASSOC); $cumul = $result->fetch(\PDO::FETCH_ASSOC);
......
<?php
namespace LQDN\Command;
class DonationIncStatusCommand
{
private $donId;
public function __construct($donId)
{
$this->donId = $donId;
}
public function getId()
{
return $this->donId;
}
}
...@@ -23,13 +23,7 @@ class DonationFinder ...@@ -23,13 +23,7 @@ class DonationFinder
public function findByUserId($userId) public function findByUserId($userId)
{ {
$userId = (int) $userId; $userId = (int) $userId;
$donations = []; return $this->connection->executeQuery("SELECT * FROM dons WHERE user_id=:userId", ["userId" => $userId])->fetchAll(\PDO::FETCH_ASSOC);
$stmt = $this->connection->query("SELECT * FROM dons WHERE user_id='$userId'");
while ($donation = $stmt->fetch()) {
$donations[$donation['id']] = $donation;
}
return $donations;
} }
/** /**
...@@ -39,16 +33,9 @@ class DonationFinder ...@@ -39,16 +33,9 @@ class DonationFinder
* *
* @return [] * @return []
*/ */
public function findById($donationId) public function findById($did)
{ {
$donationId = (int) $donationId; return $this->connection->executeQuery("SELECT * FROM dons WHERE id=:did", ["did" => $did])->fetch(\PDO::FETCH_ASSOC);
$donations = [];
$stmt = $this->connection->query("SELECT * FROM dons WHERE id='$donationId'");
while ($donation = $stmt->fetch()) {
$donations[$donationId] = $donation;
}
return $donations;
} }
/** /**
* Return dons for admins. * Return dons for admins.
......
...@@ -7,6 +7,7 @@ use LQDN\Command\DonationCreateCommand; ...@@ -7,6 +7,7 @@ use LQDN\Command\DonationCreateCommand;
use LQDN\Command\DonationInvalidateCommand; use LQDN\Command\DonationInvalidateCommand;
use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationValidateCommand; use LQDN\Command\DonationValidateCommand;
use LQDN\Command\DonationIncStatusCommand;
class DonationHandler class DonationHandler
{ {
...@@ -67,4 +68,14 @@ EOF; ...@@ -67,4 +68,14 @@ EOF;
{ {
$this->connection->executeUpdate('UPDATE dons SET pdf = "" WHERE id = :id', ['id' => $command->getId()]); $this->connection->executeUpdate('UPDATE dons SET pdf = "" WHERE id = :id', ['id' => $command->getId()]);
} }
/**
* Increase the status of a donation
*
* @param DonationIncStatusCommand $command
*/
public function handleDonationIncStatusCommand(DonationIncStatusCommand $command)
{
$this->connection->executeUpdate('UPDATE dons SET status = status + 1 WHERE id = :id', ['id' => $command->getId()]);
}
} }
...@@ -38,7 +38,7 @@ class DonationFinderTest extends FunctionalTest ...@@ -38,7 +38,7 @@ class DonationFinderTest extends FunctionalTest
'adresse_id' => '1', 'adresse_id' => '1',
'identifier' => 'id1', 'identifier' => 'id1',
]; ];
$this->assertEquals($expectedDonation, $donations[1]); $this->assertEquals($expectedDonation, $donations[0]);
} }
public function testAdminSearchDonations() public function testAdminSearchDonations()
......
...@@ -6,6 +6,7 @@ use LQDN\Command\DonationInvalidateCommand; ...@@ -6,6 +6,7 @@ use LQDN\Command\DonationInvalidateCommand;
use LQDN\Command\DonationResetPdfCommand; use LQDN\Command\DonationResetPdfCommand;
use LQDN\Command\DonationValidateCommand; use LQDN\Command\DonationValidateCommand;
use LQDN\Command\DonationCreateCommand; use LQDN\Command\DonationCreateCommand;
use LQDN\Command\DonationIncStatusCommand;
use LQDN\Tests\Functional\FunctionalTest; use LQDN\Tests\Functional\FunctionalTest;
class DonationHandlerTest extends FunctionalTest class DonationHandlerTest extends FunctionalTest
...@@ -39,7 +40,21 @@ class DonationHandlerTest extends FunctionalTest ...@@ -39,7 +40,21 @@ class DonationHandlerTest extends FunctionalTest
} }
public function testDonationIncStatusCommand()
{
// Let's get the previous status
$don = $this->container['donation_finder']->findById(1);
$prev_status = $don['status'];
// Update
$this->container['command_handler']->handle(new DonationIncStatusCommand($don['id']));
$don = $this->container['donation_finder']->findById(1);
$this->assertEquals($prev_status + 1, $don['status']);
// Remise en état
$this->container['db']->executeUpdate("UPDATE dons SET status = status - 1 WHERE id = :id",
[ 'id' => $don['id']]);
}
/** /**
* Retrieve a donation * Retrieve a donation
* *
......
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