mirror of
https://github.com/HolgerHatGarKeineNode/einundzwanzig-nostr.git
synced 2026-02-03 14:43:19 +00:00
🔒 Handle UniqueConstraintViolationException in createPaymentEvent to prevent duplicate payment events creation
This commit is contained in:
@@ -8,6 +8,7 @@ use App\Support\NostrAuth;
|
|||||||
use App\Traits\NostrFetcherTrait;
|
use App\Traits\NostrFetcherTrait;
|
||||||
use Carbon\Carbon;
|
use Carbon\Carbon;
|
||||||
use Flux\Flux;
|
use Flux\Flux;
|
||||||
|
use Illuminate\Database\UniqueConstraintViolationException;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Illuminate\Support\Str;
|
use Illuminate\Support\Str;
|
||||||
use Livewire\Component;
|
use Livewire\Component;
|
||||||
@@ -469,12 +470,25 @@ new class extends Component {
|
|||||||
|
|
||||||
public function createPaymentEvent(): PaymentEvent
|
public function createPaymentEvent(): PaymentEvent
|
||||||
{
|
{
|
||||||
|
$existing = $this->currentPleb
|
||||||
|
->paymentEvents()
|
||||||
|
->where('year', date('Y'))
|
||||||
|
->first();
|
||||||
|
|
||||||
|
if ($existing) {
|
||||||
|
return $existing;
|
||||||
|
}
|
||||||
|
|
||||||
if (app()->environment('testing')) {
|
if (app()->environment('testing')) {
|
||||||
return $this->currentPleb->paymentEvents()->create([
|
try {
|
||||||
'year' => date('Y'),
|
return $this->currentPleb->paymentEvents()->create([
|
||||||
'event_id' => 'test_event_'.Str::uuid(),
|
'year' => date('Y'),
|
||||||
'amount' => $this->amountToPay,
|
'event_id' => 'test_event_'.Str::uuid(),
|
||||||
]);
|
'amount' => $this->amountToPay,
|
||||||
|
]);
|
||||||
|
} catch (UniqueConstraintViolationException) {
|
||||||
|
return $this->currentPleb->paymentEvents()->where('year', date('Y'))->firstOrFail();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
$note = new NostrEvent;
|
$note = new NostrEvent;
|
||||||
@@ -498,11 +512,15 @@ new class extends Component {
|
|||||||
$relay->setMessage($eventMessage);
|
$relay->setMessage($eventMessage);
|
||||||
$result = $relay->send();
|
$result = $relay->send();
|
||||||
|
|
||||||
return $this->currentPleb->paymentEvents()->create([
|
try {
|
||||||
'year' => date('Y'),
|
return $this->currentPleb->paymentEvents()->create([
|
||||||
'event_id' => $result->eventId,
|
'year' => date('Y'),
|
||||||
'amount' => $this->amountToPay,
|
'event_id' => $result->eventId,
|
||||||
]);
|
'amount' => $this->amountToPay,
|
||||||
|
]);
|
||||||
|
} catch (UniqueConstraintViolationException) {
|
||||||
|
return $this->currentPleb->paymentEvents()->where('year', date('Y'))->firstOrFail();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function loadEvents(): void
|
public function loadEvents(): void
|
||||||
|
|||||||
Reference in New Issue
Block a user