Skip to content

Commit

Permalink
feat: calculate directly the distance on tasks even if no vehicle
Browse files Browse the repository at this point in the history
  • Loading branch information
Atala committed Dec 24, 2024
1 parent 54631c1 commit 6b21b7a
Show file tree
Hide file tree
Showing 5 changed files with 21 additions and 9 deletions.
22 changes: 16 additions & 6 deletions src/Doctrine/EventSubscriber/TaskListSubscriber.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ public function __construct(
private readonly TranslatorInterface $translator,
private readonly RoutingInterface $routing,
private readonly TaskListRepository $taskListRepository,
private readonly EntityManagerInterface $em,
private readonly LoggerInterface $logger
)
{
Expand All @@ -49,11 +48,12 @@ public function getSubscribedEvents()
);
}

private function calculate(TaskList $taskList)
private function calculate(TaskList $taskList, EntityManagerInterface $em)
{
$coordinates = [];
$tasks = [];
$vehicle = $taskList->getVehicle();
$vehicle = $taskList->getVehicle();
$uow = $em->getUnitOfWork();

if (!is_null($vehicle)) {
$coordinates[] = $taskList->getVehicle()->getWarehouse()->getAddress()->getGeo();
Expand Down Expand Up @@ -86,7 +86,16 @@ private function calculate(TaskList $taskList)
$emissions = intval($vehicle->getCo2emissions() * $leg['distance'] / 1000);
$task->setDistanceFromPrevious(intval($leg['distance'])); // in meter
$task->setCo2Emissions($emissions);
$this->em->getUnitOfWork()->recomputeSingleEntityChangeSet($this->em->getClassMetadata(Task::class), $task);
$uow->recomputeSingleEntityChangeSet($em->getClassMetadata(Task::class), $task);
}
} else {
$route = $this->routing->route(...$coordinates)['routes'][0];
$legs = $route["legs"];
foreach ($legs as $index => $leg) {
$task = $taskList->getTasks()[$index + 1]; // we assume we start at the first task, as there is no warehouse
$task->setDistanceFromPrevious(intval($leg['distance'])); // in meter
$task->setCo2Emissions(0); // reset
$uow->recomputeSingleEntityChangeSet($em->getClassMetadata(Task::class), $task);
}
}
}
Expand All @@ -95,9 +104,10 @@ private function calculate(TaskList $taskList)
public function prePersist(LifecycleEventArgs $args)
{
$entity = $args->getObject();
$em = $args->getObjectManager();

if ($entity instanceof TaskList) {
$this->calculate($entity);
$this->calculate($entity, $em);
}
}

Expand Down Expand Up @@ -166,7 +176,7 @@ public function onFlush(OnFlushEventArgs $args)
foreach ($this->taskLists as $taskList) { // @phpstan-ignore-line

$this->logger->debug('TaskList was modified, recalculating…');
$this->calculate($taskList);
$this->calculate($taskList, $em);

if ($uow->isInIdentityMap($taskList)) {
$uow->recomputeSingleEntityChangeSet($em->getClassMetadata(TaskList::class), $taskList);
Expand Down
1 change: 0 additions & 1 deletion src/Entity/Listener/DeliveryListener.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,6 @@ public function postPersist(Delivery $delivery)
foreach ($delivery->getTasks() as $task) {
$taskList = $this->taskListProvider->getTaskList($task, $courier);
$taskList->appendTask($task);
$this->entityManager->persist($taskList);
}

$this->entityManager->flush();
Expand Down
4 changes: 3 additions & 1 deletion src/Entity/Task.php
Original file line number Diff line number Diff line change
Expand Up @@ -490,12 +490,14 @@ class Task implements TaggableInterface, OrganizationAwareInterface, PackagesAwa
private $incidents;

/**
* CO2 emissions from the previous task/warehouse to accomplish this task, in g
* CO2 emissions from the previous task/warehouse to accomplish this task
* @Groups({"task"})
*/
private $co2Emissions = 0;

/**
* Distance from previous task, in meter
* @Groups({"task"})
*/
private $distanceFromPrevious = 0;

Expand Down
2 changes: 1 addition & 1 deletion src/Entity/TaskList.php
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@
* "method"="GET",
* "access_control"="is_granted('ROLE_DISPATCHER') or is_granted('ROLE_OAUTH2_TASKS')",
* "openapi_context"={
* "summary"="Legacy endpoint, please use '/api/task_lists/v2' instead. Retrieves Tasklists as lists of tasks, not tasks and tours, with expanded tasks."
* "summary"="Legacy endpoint, please use '/api/task_lists/v2' instead. Retrieves Tasklists as lists of tasks, not tasks and tours, with expanded tasks. Used by store integrations that wants to track tasks statuses."
* },
* "normalization_context"={"groups"={"task_list", "task_collection", "task", "delivery", "address"}}
* },
Expand Down
1 change: 1 addition & 0 deletions src/Security/TokenStoreExtractor.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ public function __construct(

public function extractStore()
{

if (null === ($token = $this->tokenStorage->getToken())) {
return;
}
Expand Down

0 comments on commit 6b21b7a

Please sign in to comment.