-
app/Enums/UserMailPreference.php
Open in GitHubenum UserMailPreference: string { case Daily = 'daily'; case Weekly = 'weekly'; case Never = 'never'; public static function toArray(): array { return [ self::Daily->value => 'Daily', self::Weekly->value => 'Weekly', self::Never->value => 'Never', ]; } }
-
app/Models/User.php
Open in GitHubuse App\Enums\UserMailPreference; final class User extends Authenticatable implements FilamentUser, MustVerifyEmail, Viewable { // ... protected function casts(): array { return [ 'created_at' => 'datetime', 'updated_at' => 'datetime', 'email_verified_at' => 'datetime', 'two_factor_confirmed_at' => 'datetime', 'is_verified' => 'boolean', 'is_company_verified' => 'boolean', 'password' => 'hashed', 'settings' => 'array', 'prefers_anonymous_questions' => 'boolean', 'avatar_updated_at' => 'datetime', 'mail_preference_time' => UserMailPreference::class, 'views' => 'integer', 'is_uploaded_avatar' => 'boolean', ]; } }
-
app/Http/Requests/UserUpdateRequest.php
Open in GitHubuse App\Enums\UserMailPreference; use App\Models\User; use App\Rules\NoBlankCharacters; use App\Rules\UnauthorizedEmailProviders; use App\Rules\Username; use Illuminate\Contracts\Validation\ValidationRule; use Illuminate\Contracts\Validation\ValidatorAwareRule; use Illuminate\Foundation\Http\FormRequest; use Illuminate\Validation\Rule; use Stringable; final class UserUpdateRequest extends FormRequest { public function rules(): array { $user = type($this->user())->as(User::class); return [ 'name' => ['required', 'string', 'max:255', new NoBlankCharacters], 'username' => [ 'required', 'string', 'min:4', 'max:50', Rule::unique(User::class)->ignore($user->id), new Username($user), ], 'email' => [ 'required', 'string', 'lowercase', 'email', 'max:255', Rule::unique(User::class)->ignore($user->id), new UnauthorizedEmailProviders(), ], 'mail_preference_time' => [Rule::enum(UserMailPreference::class)], 'bio' => ['nullable', 'string', 'max:255'], 'prefers_anonymous_questions' => ['required', 'boolean'], ]; } }
-
app/Console/Commands/SendUnreadNotificationEmailsCommand.php
Open in GitHubuse App\Enums\UserMailPreference; use App\Mail\PendingNotifications; use App\Models\User; use Illuminate\Console\Command; use Illuminate\Database\Eloquent\Builder; use Illuminate\Support\Facades\Mail; final class SendUnreadNotificationEmailsCommand extends Command { protected $signature = 'send:unread-notification-emails {--weekly : Send the unread notification emails to the users who have set their mail preference to weekly.}'; protected $description = 'Send the unread notification emails to the users.'; public function handle(): void { User::query() ->when($this->option('weekly'), function (Builder $query): void { $query->where('mail_preference_time', UserMailPreference::Weekly); }, function (Builder $query): void { $query->where('mail_preference_time', UserMailPreference::Daily); }) ->whereHas('notifications') ->withCount('notifications') ->each(fn (User $user) => Mail::to($user)->queue(new PendingNotifications($user, $user->notifications_count))); } }