diff --git a/app/Models/EinundzwanzigPleb.php b/app/Models/EinundzwanzigPleb.php index dff4120..d475a6b 100644 --- a/app/Models/EinundzwanzigPleb.php +++ b/app/Models/EinundzwanzigPleb.php @@ -22,4 +22,9 @@ class EinundzwanzigPleb extends Model return $this->hasOne(Profile::class, 'pubkey', 'pubkey'); } + public function paymentEvents() + { + return $this->hasMany(PaymentEvent::class); + } + } diff --git a/app/Models/PaymentEvent.php b/app/Models/PaymentEvent.php new file mode 100644 index 0000000..700fd71 --- /dev/null +++ b/app/Models/PaymentEvent.php @@ -0,0 +1,10 @@ +id(); + $table->unsignedBigInteger('einundzwanzig_pleb_id'); + $table->foreign('einundzwanzig_pleb_id')->references('id')->on('einundzwanzig_plebs')->cascadeOnDelete(); + $table->unsignedInteger('year'); + $table->unsignedInteger('amount'); + $table->string('event_id', 255 * 2)->nullable(); + $table->timestamps(); + }); + + Schema::table('einundzwanzig_plebs', function (Blueprint $table) { + $table->dropColumn('payment_event'); + }); + } + + /** + * Reverse the migrations. + */ + public function down(): void + { + Schema::dropIfExists('payment_events'); + } +}; diff --git a/resources/views/pages/association/profile.blade.php b/resources/views/pages/association/profile.blade.php index 3ee830a..9aa248e 100644 --- a/resources/views/pages/association/profile.blade.php +++ b/resources/views/pages/association/profile.blade.php @@ -52,11 +52,16 @@ updated([ on([ 'nostrLoggedIn' => function ($pubkey) { $this->currentPubkey = $pubkey; - $this->currentPleb = \App\Models\EinundzwanzigPleb::query()->where('pubkey', $pubkey)->first(); + $this->currentPleb = \App\Models\EinundzwanzigPleb::query() + ->with([ + 'paymentEvents' => fn($query) + => $query->where('year', date('Y')), + ]) + ->where('pubkey', $pubkey)->first(); if ($this->currentPleb->association_status === \App\Enums\AssociationStatus::ACTIVE) { $this->amountToPay = 21; } - if (!$this->currentPleb->payment_event) { + if ($this->currentPleb->paymentEvents->count() < 1) { $this->createPaymentEvent(); } $this->loadEvents(); @@ -90,16 +95,17 @@ $searchPaymentEvent = function () { $response = $request->send(); if (count($response[config('services.relay')]) > 0) { - $this->payments = collect($response[config('services.relay')])->map(fn($event) - => [ - 'id' => $event->event->id, - 'kind' => $event->event->kind, - 'content' => $event->event->content, - 'pubkey' => $event->event->pubkey, - 'tags' => $event->event->tags, - 'created_at' => $event->event->created_at, - ]) - ->filter(fn($payment) => collect($payment['tags'])->firstWhere('0', 'p')[1] === $this->currentPubkey) + $this->payments = collect($response[config('services.relay')]) + ->map(fn($event) + => [ + 'id' => $event->event->id, + 'kind' => $event->event->kind, + 'content' => $event->event->content, + 'pubkey' => $event->event->pubkey, + 'tags' => $event->event->tags, + 'created_at' => $event->event->created_at, + ]) + ->filter(fn($payment) => collect($payment['tags'])->firstWhere('0', 'e')[1] === $this->currentPleb->paymentEvents->first()->event_id) ->values() ->toArray(); @@ -173,8 +179,10 @@ $createPaymentEvent = function () { $relay->setMessage($eventMessage); $result = $relay->send(); - $this->currentPleb->update([ - 'payment_event' => $result->eventId, + $this->currentPleb->paymentEvents()->create([ + 'year' => date('Y'), + 'event_id' => $result->eventId, + 'amount' => $this->amountToPay, ]); }; @@ -233,7 +241,7 @@ $loadEvents = function () {
@if($currentPubkey)Nostr Event für die Zahlung des Mitgliedsbeitrags: {{ $currentPleb->payment_event }}