diff --git a/app/controller/admin.php b/app/controller/admin.php index 7135557a3b2893beac5b131870ddcc07e9380229..26c65146e9033ec061b593dc4a83f5546964fe23 100644 --- a/app/controller/admin.php +++ b/app/controller/admin.php @@ -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); } diff --git a/src/LQDN/Command/AdminUpdateParentCommand.php b/src/LQDN/Command/AdminUpdateParentCommand.php new file mode 100644 index 0000000000000000000000000000000000000000..3d9d90fafe92612da31bd9b2f066b4945e76baee --- /dev/null +++ b/src/LQDN/Command/AdminUpdateParentCommand.php @@ -0,0 +1,7 @@ +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. * diff --git a/src/LQDN/Handler/CounterpartHandler.php b/src/LQDN/Handler/CounterpartHandler.php index 8c228fc0cf78f9d2076660942a6c40ab138c56d4..59d855f4c6e1817b6c04bba4bb9473dc9aa98ed5 100644 --- a/src/LQDN/Handler/CounterpartHandler.php +++ b/src/LQDN/Handler/CounterpartHandler.php @@ -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 c JOIN (SELECT MAX(FIELD(quoi, 'piplome', 'pibag', 'pishirt', 'hoodie')) m FROM contreparties GROUP BY user_id, dayofyear(datec)) AS m WHERE FIELD(c.quoi, 'piplome', 'pibag', 'pishirt', 'hoodie') = m.m AND user_id = :user_id", + [ '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] + ); + } + } + } } diff --git a/tests/functional/Handler/CounterpartHandlerTest.php b/tests/functional/Handler/CounterpartHandlerTest.php index a40c099a9af302bf7231162bc592d065f9d35035..26ceee36fcc3764d6a2b3a40b15f4073c641b4fb 100644 --- a/tests/functional/Handler/CounterpartHandlerTest.php +++ b/tests/functional/Handler/CounterpartHandlerTest.php @@ -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'], 4); + } + public function testCounterpartDelete() { $this->assertTrue($this->counterpartExists(1));