Refactor checkout health resource
This commit is contained in:
@@ -0,0 +1,121 @@
|
||||
<?php
|
||||
|
||||
namespace App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths;
|
||||
|
||||
use App\Filament\Clusters\DailyOps\DailyOpsCluster;
|
||||
use App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths\Pages\ListTenantCheckoutHealths;
|
||||
use App\Filament\Clusters\DailyOps\Resources\TenantCheckoutHealths\Tables\TenantCheckoutHealthTable;
|
||||
use App\Models\CheckoutSession;
|
||||
use App\Models\Tenant;
|
||||
use BackedEnum;
|
||||
use Filament\Resources\Resource;
|
||||
use Filament\Tables\Table;
|
||||
use Illuminate\Database\Eloquent\Builder;
|
||||
use UnitEnum;
|
||||
|
||||
class TenantCheckoutHealthResource extends Resource
|
||||
{
|
||||
public const TRANSACTION_WINDOW_DAYS = 30;
|
||||
|
||||
public const DEFAULT_PROVIDER = CheckoutSession::PROVIDER_PAYPAL;
|
||||
|
||||
protected static ?string $model = Tenant::class;
|
||||
|
||||
protected static string|BackedEnum|null $navigationIcon = 'heroicon-o-credit-card';
|
||||
|
||||
protected static ?string $cluster = DailyOpsCluster::class;
|
||||
|
||||
protected static ?string $slug = 'checkout-health';
|
||||
|
||||
protected static ?int $navigationSort = 20;
|
||||
|
||||
public static function table(Table $table): Table
|
||||
{
|
||||
return TenantCheckoutHealthTable::configure($table);
|
||||
}
|
||||
|
||||
public static function canCreate(): bool
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
public static function getNavigationLabel(): string
|
||||
{
|
||||
return __('admin.checkout_health.navigation.label');
|
||||
}
|
||||
|
||||
public static function getNavigationGroup(): UnitEnum|string|null
|
||||
{
|
||||
return __('admin.nav.billing');
|
||||
}
|
||||
|
||||
public static function getEloquentQuery(): Builder
|
||||
{
|
||||
$provider = static::provider();
|
||||
$windowStart = now()->subDays(self::TRANSACTION_WINDOW_DAYS);
|
||||
|
||||
return parent::getEloquentQuery()
|
||||
->with(['activeResellerPackage.package'])
|
||||
->withExists('activeResellerPackage as has_active_reseller_package')
|
||||
->withCount([
|
||||
'purchases as checkout_transaction_count' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', false),
|
||||
'purchases as checkout_transaction_count_window' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', false)
|
||||
->where('purchased_at', '>=', $windowStart),
|
||||
'purchases as checkout_refund_count_window' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', true)
|
||||
->where('purchased_at', '>=', $windowStart),
|
||||
'checkoutSessions as checkout_requires_action_count' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('status', CheckoutSession::STATUS_REQUIRES_CUSTOMER_ACTION),
|
||||
'checkoutSessions as checkout_processing_count' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('status', CheckoutSession::STATUS_PROCESSING),
|
||||
'checkoutSessions as checkout_expired_count' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->whereNotIn('status', [
|
||||
CheckoutSession::STATUS_COMPLETED,
|
||||
CheckoutSession::STATUS_CANCELLED,
|
||||
])
|
||||
->whereNotNull('expires_at')
|
||||
->where('expires_at', '<', now()),
|
||||
])
|
||||
->withSum([
|
||||
'purchases as checkout_transaction_total' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', false),
|
||||
], 'price')
|
||||
->withSum([
|
||||
'purchases as checkout_transaction_total_window' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', false)
|
||||
->where('purchased_at', '>=', $windowStart),
|
||||
], 'price')
|
||||
->withSum([
|
||||
'purchases as checkout_refund_total_window' => fn (Builder $query) => $query
|
||||
->where('provider', $provider)
|
||||
->where('refunded', true)
|
||||
->where('purchased_at', '>=', $windowStart),
|
||||
], 'price')
|
||||
->withMax([
|
||||
'purchases as last_checkout_transaction_at' => fn (Builder $query) => $query
|
||||
->where('provider', $provider),
|
||||
], 'purchased_at');
|
||||
}
|
||||
|
||||
public static function getPages(): array
|
||||
{
|
||||
return [
|
||||
'index' => ListTenantCheckoutHealths::route('/'),
|
||||
];
|
||||
}
|
||||
|
||||
public static function provider(): string
|
||||
{
|
||||
return (string) config('checkout.default_provider', self::DEFAULT_PROVIDER);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user