fix: cleanup stale teamer pickups

This commit is contained in:
2026-08-31 15:56:12 +02:00
parent e4d64558b9
commit 39e6f35374
3 changed files with 53 additions and 2 deletions
+37
View File
@@ -2,6 +2,7 @@
namespace App\Form;
use App\BusProNet\DataProvider\PickupDataProvider;
use App\Entity\Teamer;
use App\Enum\MealPreference;
use App\Model\UploadSessionDto;
@@ -13,12 +14,18 @@ use Symfony\Component\Form\Extension\Core\Type\CollectionType;
use Symfony\Component\Form\Extension\Core\Type\EnumType;
use Symfony\Component\Form\Extension\Core\Type\TextType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
use Symfony\Component\OptionsResolver\OptionsResolver;
class TeamerProfileType extends AbstractType
{
public function __construct(private readonly PickupDataProvider $pickups)
{
}
public function buildForm(FormBuilderInterface $builder, array $options): void
{
$builder
@@ -101,6 +108,36 @@ class TeamerProfileType extends AbstractType
'choice_label' => 'label',
])
;
// The stored pickup ids are free-form and never reconciled with the
// BusProNet pickup base data, so drop ids that no longer resolve (and
// duplicates) before the collection builds its rows. Skipped while the
// base data is unavailable so a failed fetch cannot wipe real data.
// Priority 1 so this runs before the CollectionType's ResizeFormListener
// turns the array into child rows.
$builder->get('pickups')->addEventListener(FormEvents::PRE_SET_DATA, function (FormEvent $event): void {
$ids = $event->getData();
if (!is_array($ids)) {
return;
}
$known = $this->pickups->getAll();
if ([] === $known) {
return;
}
$normalized = [];
foreach ($ids as $id) {
$id = (int) $id;
if (isset($known[$id]) && !in_array($id, $normalized, true)) {
$normalized[] = $id;
}
}
$event->setData($normalized);
}, 1);
}
public function buildView(FormView $view, FormInterface $form, array $options): void