Make sure newly created users receive 3 widgets by default
ADD
app/Actions/CreateDefaultWidgetsAction.php
+<?php
+
+namespace App\Actions;
+
+use App\Exceptions\UserNotFoundException;
+use App\Models\User;
+use App\Models\Widget;
+
+class CreateDefaultWidgetsAction
+{
+ public function execute(int $userId): void
+ {
+ $user = User::find($userId);
+
+ if (!$user) {
+ throw new UserNotFoundException();
+ }
+
+ Widget::create([
+ 'user_id' => $user->id,
+ 'type' => 'balance',
+ 'sorting_index' => 0,
+ 'properties' => []
+ ]);
+
+ Widget::create([
+ 'user_id' => $user->id,
+ 'type' => 'spent',
+ 'sorting_index' => 1,
+ 'properties' => ['period' => 'today']
+ ]);
+
+ Widget::create([
+ 'user_id' => $user->id,
+ 'type' => 'spent',
+ 'sorting_index' => 2,
+ 'properties' => ['period' => 'this_month']
+ ]);
+ }
+}

MODIFY
app/Actions/CreateUserAction.php
app/Actions/CreateUserAction.php
@@ -10,11 +10,15 @@ class CreateUserAction
{
public function execute(string $name, string $email, string $password): User
{
- return User::create([
+ $user = User::create([
'name' => $name,
'email' => $email,
'password' => Hash::make($password),
'verification_token' => Str::random(100)
]);
+
+ (new CreateDefaultWidgetsAction())->execute($user->id);
+
+ return $user;
}
}

MODIFY
app/Models/Widget.php
app/Models/Widget.php
@@ -12,6 +12,13 @@ class Widget extends Model
{
use SoftDeletes;
+ protected $fillable = [
+ 'user_id',
+ 'type',
+ 'sorting_index',
+ 'properties'
+ ];
+
public function user()
{
return $this->belongsTo(User::class);

Make sure newly created users receive 3 widgets by default