Skip to content

Commit

Permalink
Add action to users table.
Browse files Browse the repository at this point in the history
  • Loading branch information
timvandijck committed Mar 20, 2024
1 parent 51d2de4 commit cbb762f
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/Filament/Resources/Customers/UserResource.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace App\Filament\Resources\Customers;

use App\Filament\Resources\Customers\UserResource\Actions\TransferPurchaseAssignmentAction;
use App\Filament\Resources\Customers\UserResource\Actions\TransferPurchaseToUserAction;
use App\Filament\Tables\Columns\BooleanColumn;
use App\Models\User;
use Filament\Forms;
Expand Down Expand Up @@ -93,6 +94,7 @@ public static function table(Table $table): Table
ActionGroup::make([
Tables\Actions\EditAction::make(),
TransferPurchaseAssignmentAction::make(),
TransferPurchaseToUserAction::make(),
]),
])
->bulkActions([
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
<?php

namespace App\Filament\Resources\Customers\UserResource\Actions;

use App\Domain\Shop\Models\Purchase;
use App\Models\User;
use Filament\Forms\Components\TextInput;
use Filament\Notifications\Notification;
use Filament\Tables\Actions\Action;
use Laravel\Paddle\Receipt;

class TransferPurchaseToUserAction
{
public static function make()
{
return Action::make('transfer_purchases_to_user')
->icon('heroicon-o-user-plus')
->form([
TextInput::make('email')
->label('Email')
->required()
->email(),
])
->action(function (array $data, User $record): void {
$otherUser = User::where('email', $data['email'])->first();
$user = $record;

if (! $otherUser) {
Notification::make()
->title("No user found with email {$data['email']}")
->danger()
->send();
}

$user->purchases->each(function (Purchase $purchase) use ($otherUser) {
$purchase->update(['user_id' => $otherUser->id]);
});

$user->receipts->each(function (Receipt $receipt) use ($otherUser) {
$receipt->update(['billable_id' => $otherUser->id]);
});

Notification::make()
->title("All purchases transfered to {$otherUser->name} ({$otherUser->email})!")
->success()
->send();
});
}
}

0 comments on commit cbb762f

Please sign in to comment.