48 lines
1.6 KiB
PHP
48 lines
1.6 KiB
PHP
<?php
|
|
|
|
namespace App\Models;
|
|
|
|
use Illuminate\Database\Eloquent\Attributes\Fillable;
|
|
use Illuminate\Database\Eloquent\Model;
|
|
use Illuminate\Database\Eloquent\Relations\BelongsTo;
|
|
use Illuminate\Http\Request;
|
|
|
|
#[Fillable(['webhook_endpoint_id', 'event', 'payload', 'response_status', 'response_body', 'attempts', 'delivered_at', 'created_at'])]
|
|
class WebhookDelivery extends Model
|
|
{
|
|
const UPDATED_AT = null;
|
|
|
|
protected $casts = [
|
|
'payload' => 'array',
|
|
'delivered_at' => 'datetime',
|
|
];
|
|
|
|
public function endpoint(): BelongsTo
|
|
{
|
|
return $this->belongsTo(WebhookEndpoint::class, 'webhook_endpoint_id');
|
|
}
|
|
|
|
/**
|
|
* Historial paginado de intentos de entrega de un webhook — compartido
|
|
* por OAuth\WebhookController (sesión) y Api\WebhookController (token),
|
|
* mismo patrón que AuditLog::paginatedFor.
|
|
*/
|
|
public static function paginatedFor(WebhookEndpoint $endpoint, Request $request)
|
|
{
|
|
$perPage = min(max((int) $request->integer('per_page', 15), 5), 100);
|
|
|
|
return self::where('webhook_endpoint_id', $endpoint->id)
|
|
->latest('created_at')
|
|
->paginate($perPage)
|
|
->through(fn (self $delivery) => [
|
|
'id' => $delivery->id,
|
|
'event' => $delivery->event,
|
|
'response_status' => $delivery->response_status,
|
|
'response_body' => $delivery->response_body,
|
|
'attempts' => $delivery->attempts,
|
|
'delivered_at' => $delivery->delivered_at,
|
|
'created_at' => $delivery->created_at,
|
|
]);
|
|
}
|
|
}
|