Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Voucher aggregates collation #37

Merged
merged 1 commit into from
Sep 9, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 39 additions & 0 deletions app/Events/Vouchers/VoucherWasUpdated.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
<?php

namespace App\Events\Vouchers;

use App\Models\Voucher;
use Illuminate\Broadcasting\Channel;
use Illuminate\Broadcasting\InteractsWithSockets;
use Illuminate\Broadcasting\PresenceChannel;
use Illuminate\Broadcasting\PrivateChannel;
use Illuminate\Contracts\Broadcasting\ShouldBroadcast;
use Illuminate\Foundation\Events\Dispatchable;
use Illuminate\Queue\SerializesModels;

class VoucherWasUpdated
{
use Dispatchable;
use InteractsWithSockets;
use SerializesModels;

/**
* Create a new event instance.
*/
public function __construct(public Voucher $voucher)
{
//
}

/**
* Get the channels the event should broadcast on.
*
* @return array<int, Channel>
*/
public function broadcastOn(): array
{
return [
new PrivateChannel('channel-name'),
];
}
}
33 changes: 33 additions & 0 deletions app/Jobs/Vouchers/CollateVoucherAggregatesJob.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
<?php

namespace App\Jobs\Vouchers;

use App\Models\Voucher;
use App\Services\VoucherService;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Foundation\Bus\Dispatchable;
use Illuminate\Foundation\Queue\Queueable;
use Illuminate\Queue\InteractsWithQueue;
use Illuminate\Queue\SerializesModels;

class CollateVoucherAggregatesJob implements ShouldQueue
{
use Queueable;

/**
* Create a new job instance.
*/
public function __construct(public Voucher $voucher)
{
//
}

/**
* Execute the job.
* @throws \Exception
*/
public function handle(): void
{
VoucherService::collateVoucherAggregates($this->voucher);
}
}
27 changes: 27 additions & 0 deletions app/Listeners/Vouchers/HandleVoucherWasUpdatedEvent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
<?php

namespace App\Listeners\Vouchers;

use App\Events\Vouchers\VoucherWasUpdated;
use App\Jobs\Vouchers\CollateVoucherAggregatesJob;
use Illuminate\Contracts\Queue\ShouldQueue;
use Illuminate\Queue\InteractsWithQueue;

class HandleVoucherWasUpdatedEvent implements ShouldQueue
{
/**
* Create the event listener.
*/
public function __construct()
{
//
}

/**
* Handle the event.
*/
public function handle(VoucherWasUpdated $event): void
{
dispatch(new CollateVoucherAggregatesJob($event->voucher));
}
}
28 changes: 4 additions & 24 deletions app/Models/Voucher.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

namespace App\Models;

use App\Events\Vouchers\VoucherWasUpdated;
use Attribute;
use Illuminate\Database\Eloquent\Concerns\HasUuids;
use Illuminate\Database\Eloquent\Factories\HasFactory;
Expand All @@ -19,14 +20,13 @@ class Voucher extends Model
protected $keyType = 'string';
public $incrementing = false;

// protected $appends = [
// 'voucher_code',
// ];

protected $casts = [
'expires_at' => 'datetime',
];
protected $dispatchesEvents = [

protected $dispatchesEvents = [
'updated' => VoucherWasUpdated::class
];

/**
Expand All @@ -53,26 +53,6 @@ public function voucherSet(): BelongsTo
return $this->belongsTo(VoucherSet::class, 'voucher_set_id', 'id');
}

// /**
// * @return HasMany
// */
// public function voucherRedemptions(): HasMany
// {
// return $this->hasMany(VoucherRedemption::class, 'voucher_id', 'id')->orderBy('created_at', 'desc');
// }

// /**
// * Get the voucher's voucher_code
// *
// * @return Attribute
// */
// public function voucherCode(): Attribute
// {
// return Attribute::make(
// get: fn ($value, $attributes) => substr($attributes['id'], 0, 5),
// );
// }

public function voucherRedemptions(): HasMany
{
return $this->hasMany(VoucherRedemption::class, 'voucher_id', 'id');
Expand Down
Loading