Files
portal-api/app/Services/Customer/CustomerMailService.php
T
2026-10-05 09:30:24 +07:00

134 lines
4.3 KiB
PHP

<?php
namespace App\Services\Customer;
use App\Support\EmailRecipients;
use App\Mail\Customer\ManualCustomerEmail;
use App\Mail\Customer\PasswordResetByAdmin;
use App\Models\Customer;
use App\Models\Log;
use App\Models\Mail as MailLog;
use App\Models\Template;
use App\Models\User;
use App\Repositories\Customer\CustomerRepositoryInterface;
use App\Services\Customer\Concerns\ResolvesCustomer;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Facades\Hash;
use Illuminate\Support\Facades\Mail;
class CustomerMailService
{
use ResolvesCustomer;
public function __construct(
private readonly CustomerRepositoryInterface $customerRepository,
) {}
/**
* Reset về mật khẩu ngẫu nhiên và gửi mail cho khách hàng.
*/
public function resetPassword(string $uuid): void
{
$customer = $this->resolveCustomer($uuid);
$this->setPasswordAndNotify($customer, str()->random(10));
}
/**
* Đặt mật khẩu do admin nhập và gửi mail cho khách hàng, có ghi log.
*/
public function sendPasswordEmail(string $uuid, string $password, string $clientIp): void
{
$customer = $this->resolveCustomer($uuid);
$this->setPasswordAndNotify($customer, $password);
Log::create([
'agent' => 'admin',
'client_ip' => $clientIp,
'description' => sprintf('Admin gửi lại mật khẩu mới cho customer %s', $customer->email),
'customer_id' => $customer->id,
]);
}
private function setPasswordAndNotify(Customer $customer, string $password): void
{
DB::transaction(function () use ($customer, $password) {
$this->customerRepository->update($customer->id, ['password' => Hash::make($password)]);
$mail = new PasswordResetByAdmin([
'fullname' => $customer->fullname,
'email' => $customer->email,
'password' => $password,
]);
Mail::to($customer->email)->send($mail);
MailLog::create([
'subject' => $mail->envelope()->subject,
'to' => $customer->email,
'customer_id' => $customer->id,
'message' => $mail->render(),
]);
});
}
/**
* Gửi email tự do (hoặc theo template) cho khách hàng, có ghi log.
*/
public function sendCustomEmail(string $uuid, array $data, User $admin, string $clientIp): void
{
$customer = $this->resolveCustomer($uuid);
$template = null;
$subject = trim((string) ($data['subject'] ?? ''));
$message = trim((string) ($data['message'] ?? ''));
if (!empty($data['template_id'])) {
$template = Template::query()->whereKey($data['template_id'])->where('status', 1)->first();
abort_if(!$template, 422, 'Template không tồn tại hoặc đang bị tắt.');
$subject = trim((string) $template->subject);
$message = trim((string) $template->message);
}
abort_if($subject === '' || $message === '', 422, 'Subject và content không được để trống.');
$to = EmailRecipients::parse($data['to'] ?? null) ?: [$customer->email];
$cc = EmailRecipients::parse($data['cc'] ?? null);
$bcc = EmailRecipients::parse($data['bcc'] ?? null);
abort_if($to === [], 422, 'Không có địa chỉ email nhận hợp lệ.');
$mailer = Mail::to($to);
if ($cc !== []) {
$mailer->cc($cc);
}
if ($bcc !== []) {
$mailer->bcc($bcc);
}
$mailer->send(new ManualCustomerEmail($subject, $message));
MailLog::create([
'template_id' => $template->id ?? 0,
'subject' => $subject,
'to' => implode(', ', $to),
'cc' => implode(', ', $cc),
'customer_id' => $customer->id,
'message' => $message,
]);
Log::create([
'agent' => 'admin',
'client_ip' => $clientIp,
'description' => sprintf(
'%s gửi email cho customer %s%s',
$admin->username ?? $admin->email ?? 'Admin',
$customer->email,
$template ? sprintf(' bằng template #%s', $template->id) : '',
),
'customer_id' => $customer->id,
]);
}
}