feat: Add IT Support catalog and related migrations, seeders, and email templates
- Created migration to add 'group_label' to 'product_type_fields' table. - Implemented ItSupportCatalogSeeder to populate IT Support features and tiers. - Added ProductCategorySeeder for initial product categories. - Developed ProductMockupSeeder for mockup products in various categories. - Introduced ProductTypeFieldSeeder to define fields for different product types. - Created email templates for VPS service information and service renewal reminders.
This commit is contained in:
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
|
||||
namespace App\Services\Customer;
|
||||
|
||||
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 = $this->parseEmailRecipients($data['to'] ?? null) ?: [$customer->email];
|
||||
$cc = $this->parseEmailRecipients($data['cc'] ?? null);
|
||||
$bcc = $this->parseEmailRecipients($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,
|
||||
]);
|
||||
}
|
||||
|
||||
private function parseEmailRecipients(?string $value): array
|
||||
{
|
||||
if (!$value) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return collect(preg_split('/[,;]+/', $value) ?: [])
|
||||
->map(fn($email) => trim((string) $email))
|
||||
->filter(fn($email) => $email !== '' && filter_var($email, FILTER_VALIDATE_EMAIL))
|
||||
->unique()
|
||||
->values()
|
||||
->all();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user