Files

76 lines
2.4 KiB
PHP
Raw Permalink Normal View History

2026-07-22 14:10:05 +03:30
<?php
namespace App\Console\Commands;
use App\Models\User;
use Carbon\Carbon;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Http;
use Morilog\Jalali\Jalalian;
class NotifyUpcomingBirthdays extends Command
{
protected $signature = 'birthdays:notify';
protected $description = 'Send Bale reminder to admins one day before employee birthdays';
public function handle(): int
{
$tomorrow = Jalalian::now()->addDays(1);
$month = $tomorrow->getMonth();
$day = $tomorrow->getDay();
$jalaliDate = $tomorrow->format('j F');
$users = User::query()
->whereNotNull('birthday')
->whereNull('deleted_at')
->get()
->filter(function (User $user) use ($month, $day) {
$birth = Jalalian::fromCarbon(Carbon::parse($user->birthday));
return $birth->getMonth() === $month && $birth->getDay() === $day;
});
if ($users->isEmpty()) {
$this->info('No birthdays tomorrow.');
return self::SUCCESS;
}
foreach ($users as $user) {
$name = $user->name ?: $user->user_name;
$message = "🌸 یک یادآوری لطیف از قلب تیم...\n\n"
. "فردا {$jalaliDate}، روز تولد «{$name}» است.\n\n"
. "فردایی که می‌تواند با یک پیام گرم، یک لبخند، یا یک «تولدت مبارک» ساده، برایش خاطره‌ای قشنگ بسازد.\n"
. "بیایید این روز را از یاد نبریم و بهش حس ارزشمند بودن بدهیم 💛";
$this->sendToAdmins($message);
$this->info("Birthday reminder sent for: {$name}");
}
return self::SUCCESS;
}
private function sendToAdmins(string $message): void
{
$admins = User::query()
->whereNotNull('is_admin')
->whereNotNull('bale')
->select('bale')
->get();
foreach ($admins as $admin) {
try {
Http::withoutVerifying()->post(
'https://tapi.bale.ai/1482838710:sYbLzby0tuLRzHvP4d3LsbcNwg2sjSDLTAU/sendMessage',
[
'chat_id' => $admin->bale,
'text' => $message,
]
);
} catch (\Exception $e) {
}
}
}
}