feat: add multiple payment events to user profile

- Added logic to handle multiple payment events in a user's profile
- Created a new PaymentEvent model and associated it with the EinundzwanzigPleb model
- Added a new migration for creating the payment_events table in the database
- Updated the profile.blade.php view to display all payment events for a user
This commit is contained in:
fsociety
2024-09-30 15:14:50 +02:00
parent 1d0c816192
commit 0bdd890dd3
4 changed files with 102 additions and 44 deletions

View File

@@ -22,4 +22,9 @@ class EinundzwanzigPleb extends Model
return $this->hasOne(Profile::class, 'pubkey', 'pubkey');
}
public function paymentEvents()
{
return $this->hasMany(PaymentEvent::class);
}
}

View File

@@ -0,0 +1,10 @@
<?php
namespace App\Models;
use Illuminate\Database\Eloquent\Model;
class PaymentEvent extends Model
{
protected $guarded = [];
}

View File

@@ -0,0 +1,35 @@
<?php
use Illuminate\Database\Migrations\Migration;
use Illuminate\Database\Schema\Blueprint;
use Illuminate\Support\Facades\Schema;
return new class extends Migration {
/**
* Run the migrations.
*/
public function up(): void
{
Schema::create('payment_events', function (Blueprint $table) {
$table->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');
}
};

View File

@@ -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,7 +95,8 @@ $searchPaymentEvent = function () {
$response = $request->send();
if (count($response[config('services.relay')]) > 0) {
$this->payments = collect($response[config('services.relay')])->map(fn($event)
$this->payments = collect($response[config('services.relay')])
->map(fn($event)
=> [
'id' => $event->event->id,
'kind' => $event->event->kind,
@@ -99,7 +105,7 @@ $searchPaymentEvent = function () {
'tags' => $event->event->tags,
'created_at' => $event->event->created_at,
])
->filter(fn($payment) => collect($payment['tags'])->firstWhere('0', 'p')[1] === $this->currentPubkey)
->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,
]);
};