Commit 43da7c56 authored by okhin's avatar okhin 🚴

Let's update Parents before Total

parent 44849044
Pipeline #2336 passed with stages
in 18 seconds
......@@ -12,6 +12,7 @@ use LQDN\Command\DonationCreateCommand;
use LQDN\Command\UserUpdateByAdminCommand;
use LQDN\Command\UserCreateCommand;
use LQDN\Command\UserUpdateCumulCommand;
use LQDN\Command\AdminUpdateParentCommand;
use LQDN\Command\AdminUpdateTotalUsersCommand;
class Admin extends Controller
......@@ -1519,6 +1520,7 @@ class Admin extends Controller
public function recompute($f3, $args)
{
$f3->get('container')['command_handler']->handle(new AdminUpdateParentCommand());
$f3->get('container')['command_handler']->handle(new AdminUpdateTotalUsersCommand());
$this->show($f3, $args);
}
......
<?php
namespace LQDN\Command;
class AdminUpdateParentCommand
{
// There is no parameters for this command.
}
......@@ -13,6 +13,20 @@ class CounterpartFinder
$this->connection = $connection;
}
/**
* Return a counterpart by its id
*
* @param mixed $id
*
* @return array
*/
public function findById($id)
{
$id = (int) $id;
$stmt = $this->connection->executeQuery('SELECT * FROM contreparties WHERE id = :id', [ 'id' => $id ]);
return $stmt->fetch(\PDO::FETCH_ASSOC);
}
/**
* Return all counterparts of a given user.
*
......
......@@ -6,6 +6,7 @@ use Doctrine\DBAL\Connection;
use LQDN\Command\CounterpartCreateCommand;
use LQDN\Command\CounterpartDeleteCommand;
use LQDN\Command\CounterpartChangeStateCommand;
use LQDN\Command\AdminUpdateParentCommand;
use LQDN\Exception\CounterpartAlreadyExistsException;
class CounterpartHandler
......@@ -113,4 +114,42 @@ EOF;
);
return ($status == 2); // status 2 is delivered counterparts
}
/**
* We try to guess how to group all counterparts, if they're created on the same day, they've been asked
* as a group of counterparts, so we should parent them together.
*
* @param AdminUpdateParentCommand $command
*/
public function handleAdminUpdateParentCommand(AdminUpdateParentCommand $command)
{
// Let's get users first
$user_ids = $this->connection->executeQuery('SELECT id FROM users')->fetchAll(\PDO::FETCH_COLUMN);
foreach ($user_ids as $user_id) {
$user_id =(int) $user_id;
// Those are the parents ID, grouped by day.
$parents = $this->connection->executeQuery(
"select id from contreparties WHERE user_id = :user_id group by user_id, dayofyear(datec) order by field(quoi, 'piplome', 'pibag', 'pishirt', 'hoodie')",
[ 'user_id' => $user_id]
)->fetchAll(\PDO::FETCH_COLUMN);
foreach ($parents as $parent) {
$parent = (int) $parent;
// Let's get the date of this one
$day = $this->connection->executeQuery(
'SELECT DAYOFYEAR(datec) FROM contreparties WHERE id = :parent',
['parent' => $parent]
)->fetchAll(\PDO::FETCH_COLUMN)[0];
// Let's update the parent
$this->connection->executeUpdate(
'UPDATE contreparties SET parent = :parent WHERE DAYOFYEAR(datec) = :day AND user_id = :user_id',
['parent' => $parent, 'day' => (int) $day, 'user_id' => $user_id]
);
}
}
}
}
......@@ -5,11 +5,19 @@ namespace LQDN\Tests\Functional\Handler;
use LQDN\Command\CounterpartCreateCommand;
use LQDN\Command\CounterpartDeleteCommand;
use LQDN\Command\CounterpartChangeStateCommand;
use LQDN\Command\AdminUpdateParentCommand;
use LQDN\Handler\CounterpartHandler;
use LQDN\Tests\Functional\FunctionalTest;
class CounterpartHandlerTest extends FunctionalTest
{
public function testAdminUpdateParentCommand()
{
$this->container['command_handler']->handle(new AdminUpdateParentCommand());
$target = $this->container['counterpart_finder']->findById(4);
$this->assertSame((int) $target['parent'], 2);
}
public function testCounterpartDelete()
{
$this->assertTrue($this->counterpartExists(1));
......
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