Get existing supports for same project and origin

This commit is contained in:
2025-09-04 15:41:18 +02:00
parent e65f0746dd
commit 27a986f21d
2 changed files with 16 additions and 3 deletions
+2 -2
View File
@@ -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]
@@ -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);