feat: email gửi tay/nhắc gia hạn, gói hosting, sửa dữ liệu sản phẩm
- Gửi email theo template cho 1 khách, tab lịch sử email, nhắc gia hạn dịch vụ hàng loạt - Mail lưu from/bcc/sent_by; mẫu MARKETING và mẫu cũ trong TemplateSeeder - Gói hosting: giới hạn, trạng thái, lấy plan Plesk qua CLI gateway - Backfill payment_method và ngày đơn hàng còn thiếu - Bổ sung field CPU/RAM/Disk/Băng thông cho loại sản phẩm vps và cloud Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
co-authored by
Claude Opus 5.5
parent
8651dde6b1
commit
30898ea46f
@@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services;
|
||||
|
||||
use App\Mail\ServiceRenewalReminderMail;
|
||||
use App\Models\Mail as MailLog;
|
||||
use App\Models\Service;
|
||||
use App\Models\User;
|
||||
use App\Services\Email\EmailTemplateService;
|
||||
use App\Support\AutoEmailGate;
|
||||
use Illuminate\Database\Eloquent\Collection;
|
||||
use Illuminate\Database\Eloquent\ModelNotFoundException;
|
||||
use Illuminate\Support\Facades\Mail;
|
||||
|
||||
class ServiceRenewalReminderService
|
||||
{
|
||||
private const SCENARIO = 'service_renewal_reminder';
|
||||
|
||||
public const MAX_RESULTS = 1000;
|
||||
|
||||
public function __construct(private readonly EmailTemplateService $templates) {}
|
||||
|
||||
/**
|
||||
* @return Collection<int, Service>
|
||||
*/
|
||||
public function expiring(int $days): Collection
|
||||
{
|
||||
return Service::with(['customer:id,uuid,fullname,email', 'product:id,name'])
|
||||
->where('status', 'active')
|
||||
->whereHas('customer')
|
||||
->whereBetween('expires_at', [now()->startOfDay(), now()->addDays($days)->endOfDay()])
|
||||
->orderBy('expires_at')
|
||||
->limit(self::MAX_RESULTS)
|
||||
->get();
|
||||
}
|
||||
|
||||
public function daysLeft(Service $service): int
|
||||
{
|
||||
return (int) now()->startOfDay()->diffInDays($service->expires_at->copy()->startOfDay(), false);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<int, int> $serviceIds
|
||||
*/
|
||||
public function send(array $serviceIds, User $admin): int
|
||||
{
|
||||
abort_if(!AutoEmailGate::mailEnabled(), 422, 'Hệ thống đang tắt gửi email (Mẫu email → Cài đặt → Bật gửi email).');
|
||||
|
||||
try {
|
||||
$template = $this->templates->loadTemplate(self::SCENARIO);
|
||||
} catch (ModelNotFoundException) {
|
||||
abort(422, 'Chưa có template nhắc gia hạn (service_renewal_reminder) đang bật trong Mẫu email.');
|
||||
}
|
||||
|
||||
AutoEmailGate::markManual();
|
||||
|
||||
$services = Service::with(['customer', 'product', 'orderItem'])
|
||||
->whereIn('id', $serviceIds)
|
||||
->whereNotNull('expires_at')
|
||||
->get()
|
||||
->filter(fn (Service $service) => filled($service->customer?->email));
|
||||
|
||||
foreach ($services as $service) {
|
||||
$mail = new ServiceRenewalReminderMail($service->customer, $service);
|
||||
|
||||
MailLog::create([
|
||||
'template_id' => $template->id,
|
||||
'subject' => $mail->envelope()->subject,
|
||||
'from_address' => $template->mailConfig()['from_email'] ?? config('mail.from.address'),
|
||||
'to' => $service->customer->email,
|
||||
'customer_id' => $service->customer_id,
|
||||
'sent_by' => $admin->id,
|
||||
'message' => $mail->render(),
|
||||
]);
|
||||
|
||||
Mail::to($service->customer->email)->queue($mail);
|
||||
}
|
||||
|
||||
return $services->count();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user