From 27a986f21d04a181f5be76a0cbfda625f53b8352 Mon Sep 17 00:00:00 2001 From: Daniel Subiabre Date: Thu, 4 Sep 2025 15:41:18 +0200 Subject: [PATCH] Get existing supports for same project and origin --- src/Entity/Project/Support.php | 4 ++-- .../GatewayCheckoutSupportsListener.php | 15 ++++++++++++++- 2 files changed, 16 insertions(+), 3 deletions(-) diff --git a/src/Entity/Project/Support.php b/src/Entity/Project/Support.php index 2c1a4bc..d37c05c 100644 --- a/src/Entity/Project/Support.php +++ b/src/Entity/Project/Support.php @@ -14,7 +14,7 @@ use Doctrine\ORM\Mapping as ORM; #[MapProvider(EntityMapProvider::class)] #[ORM\Entity(repositoryClass: SupportRepository::class)] #[ORM\Table(name: 'project_support', uniqueConstraints: [ - new ORM\UniqueConstraint(fields: ['project', 'origin']) + new ORM\UniqueConstraint(fields: ['project', 'origin']), ])] class Support { @@ -37,7 +37,7 @@ class Support #[ORM\JoinTable(name: 'project_support_trxs')] #[ORM\JoinColumn(name: 'support_id', referencedColumnName: 'id')] #[ORM\InverseJoinColumn(name: 'transaction_id', referencedColumnName: 'id')] - #[ORM\ManyToMany(targetEntity: Transaction::class)] + #[ORM\ManyToMany(targetEntity: Transaction::class, cascade: ['persist'])] private Collection $transactions; #[ORM\Column] diff --git a/src/EventListener/GatewayCheckoutSupportsListener.php b/src/EventListener/GatewayCheckoutSupportsListener.php index 2249e88..5d11ddd 100644 --- a/src/EventListener/GatewayCheckoutSupportsListener.php +++ b/src/EventListener/GatewayCheckoutSupportsListener.php @@ -7,6 +7,7 @@ use App\Entity\Gateway\Charge; use App\Entity\Gateway\Checkout; use App\Entity\Project\Project; use App\Entity\Project\Support; +use App\Repository\Project\SupportRepository; use Doctrine\Bundle\DoctrineBundle\Attribute\AsEntityListener; use Doctrine\ORM\EntityManagerInterface; use Doctrine\ORM\Event\PreFlushEventArgs; @@ -19,6 +20,10 @@ use Doctrine\ORM\Events; )] class GatewayCheckoutSupportsListener { + public function __construct( + private SupportRepository $supportRepository, + ) {} + public function preFlush(Checkout $checkout, PreFlushEventArgs $args): void { /** @var EntityManagerInterface */ @@ -50,7 +55,15 @@ class GatewayCheckoutSupportsListener */ private function createSupport(Project $project, Accounting $origin, array $transactions): Support { - $projectSupport = new Support(); + $projectSupport = $this->supportRepository->findOneBy([ + 'project' => $project->getId(), + 'origin' => $origin->getId(), + ]); + + if (!$projectSupport) { + $projectSupport = new Support(); + } + $projectSupport->setProject($project); $projectSupport->setOrigin($origin); $projectSupport->setAnonymous(false);