Skip to content

Commit

Permalink
fix: use MAX() instead of group by to avoid line duplications
Browse files Browse the repository at this point in the history
  • Loading branch information
r0xsh committed Feb 12, 2025
1 parent 60a53a1 commit 45a158f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 6 deletions.
8 changes: 4 additions & 4 deletions src/Action/Task/Collection.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,21 +18,21 @@ public function __invoke(Paginator|array $data, EntityManagerInterface $entityMa
(select json_agg(json_build_object(
'id', packages_rows.id, 'task_package_id', packages_rows.task_package_id, 'name', packages_rows.name, 'type', packages_rows.name, 'quantity', packages_rows.quantity, 'volume_per_package', packages_rows.volume_units, 'short_code', packages_rows.short_code))
FROM
(select p.id AS id, tp.id AS task_package_id, p.name AS name, p.average_volume_units AS volume_units, p.short_code as short_code, sum(tp.quantity) AS quantity
(select p.id AS id, MAX(tp.id) AS task_package_id, p.name AS name, p.average_volume_units AS volume_units, p.short_code as short_code, sum(tp.quantity) AS quantity
from task t inner join task_package tp on tp.task_id = t.id
inner join package p on tp.package_id = p.id
where t.delivery_id = t_outer.delivery_id
group by p.id, tp.id, p.name, p.average_volume_units
group by p.id, p.name, p.average_volume_units
) packages_rows)
WHEN t_outer.type = 'DROPOFF' THEN
(select json_agg(json_build_object(
'id', packages_rows.id, 'task_package_id', packages_rows.task_package_id, 'name', packages_rows.name, 'type', packages_rows.name, 'quantity', packages_rows.quantity, 'volume_per_package', packages_rows.volume_units, 'short_code', packages_rows.short_code))
FROM
(select p.id AS id, tp.id AS task_package_id, p.name AS name, p.average_volume_units AS volume_units, p.short_code as short_code, sum(tp.quantity) AS quantity
(select p.id AS id, MAX(tp.id) AS task_package_id, p.name AS name, p.average_volume_units AS volume_units, p.short_code as short_code, sum(tp.quantity) AS quantity
from task t inner join task_package tp on tp.task_id = t.id
inner join package p on tp.package_id = p.id
where t.id = t_outer.id
group by p.id, tp.id, p.name, p.average_volume_units
group by p.id, p.name, p.average_volume_units
) packages_rows)
ELSE
NULL
Expand Down
4 changes: 2 additions & 2 deletions src/Serializer/TaskNormalizer.php
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,11 @@ public function normalize($object, $format = null, array $context = array())
->createQueryBuilder('t');

$query = $qb
->select('p.id', 'tp.id as task_package_id', 'p.name AS name', 'p.name AS type', 'sum(tp.quantity) AS quantity', 'p.averageVolumeUnits AS volume_per_package', 'p.shortCode AS short_code')
->select('p.id', 'MAX(tp.id) as task_package_id', 'p.name AS name', 'p.name AS type', 'sum(tp.quantity) AS quantity', 'p.averageVolumeUnits AS volume_per_package', 'p.shortCode AS short_code')
->join('t.packages', 'tp', 'WITH', 'tp.task = t.id')
->join('tp.package', 'p', 'WITH', 'tp.package = p.id')
->join('t.delivery', 'd', 'WITH', 'd.id = :deliveryId')
->groupBy('p.id', 'tp.id', 'p.name', 'p.averageVolumeUnits', 'p.shortCode')
->groupBy('p.id', 'p.name', 'p.averageVolumeUnits', 'p.shortCode')
->setParameter('deliveryId', $deliveryId)
->getQuery();

Expand Down

0 comments on commit 45a158f

Please sign in to comment.