Add feature tests for domain order management
- Implement AdminDomainOrderTest to cover admin functionalities for domain orders including listing, confirming registration, KYC updates, activation, rejection, and refunds. - Implement ClientDomainOrderTest to cover client functionalities for placing domain orders, including validation for balance, duplicate orders, and domain normalization. - Ensure proper handling of domain pricing and service statuses throughout the tests.
This commit is contained in:
@@ -2,6 +2,7 @@
|
||||
|
||||
namespace App\Services\Api\Client;
|
||||
|
||||
use App\Mail\Customer\VerifyAccount as VerifyAccountMail;
|
||||
use App\Mail\Customer\Welcome as WelcomeMail;
|
||||
use App\Models\RefreshToken;
|
||||
use App\Repositories\Api\Client\ClientRepositoryInterface;
|
||||
@@ -39,12 +40,18 @@ class ClientAuthService
|
||||
|
||||
if ((int)$client->status !== 1) {
|
||||
$statusLabel = match ((int)$client->status) {
|
||||
0 => 'chờ xác thực',
|
||||
0 => 'PENDING_VERIFY',
|
||||
2 => 'tạm ngưng hoạt động',
|
||||
3 => 'bị khoá',
|
||||
default => 'vô hiệu hoá',
|
||||
};
|
||||
|
||||
if ($statusLabel === 'PENDING_VERIFY') {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['PENDING_VERIFY'],
|
||||
]);
|
||||
}
|
||||
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ["Tài khoản của bạn đã {$statusLabel}, vui lòng liên hệ hotline."],
|
||||
]);
|
||||
@@ -60,18 +67,53 @@ class ClientAuthService
|
||||
}
|
||||
|
||||
/**
|
||||
* Đăng ký khách hàng mới và tạo access + refresh token
|
||||
* Đăng ký khách hàng mới — status=0 (chờ xác thực OTP), chưa cấp token.
|
||||
*/
|
||||
public function registerClient(array $data): array
|
||||
{
|
||||
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
|
||||
$client = $this->clientRepository->create([
|
||||
'fullname' => $data['fullname'],
|
||||
'phone' => $data['phone'],
|
||||
'email' => $data['email'],
|
||||
'password' => bcrypt($data['password']),
|
||||
'fullname' => $data['fullname'],
|
||||
'phone' => $data['phone'],
|
||||
'email' => $data['email'],
|
||||
'password' => bcrypt($data['password']),
|
||||
'status' => 0,
|
||||
'email_verify_code' => $code,
|
||||
'email_verify_code_expired_at' => now()->addMinutes(5),
|
||||
]);
|
||||
|
||||
$token = JWTAuth::fromUser($client);
|
||||
$this->sendVerifyEmail($client, $code);
|
||||
|
||||
return ['client' => $client];
|
||||
}
|
||||
|
||||
/**
|
||||
* Xác thực OTP — kích hoạt tài khoản, trả về token.
|
||||
*/
|
||||
public function verifyEmail(string $email, string $code): array
|
||||
{
|
||||
$client = \App\Models\Customer::whereRaw('LOWER(email) = ?', [strtolower(trim($email))])->first();
|
||||
|
||||
if (! $client || $client->email_verify_code !== $code) {
|
||||
throw ValidationException::withMessages([
|
||||
'code' => ['Mã xác thực không chính xác.'],
|
||||
]);
|
||||
}
|
||||
|
||||
if (! $client->email_verify_code_expired_at || now()->gt($client->email_verify_code_expired_at)) {
|
||||
throw ValidationException::withMessages([
|
||||
'code' => ['Mã xác thực đã hết hạn, vui lòng yêu cầu gửi lại.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$client->status = 1;
|
||||
$client->email_verify_code = null;
|
||||
$client->email_verify_code_expired_at = null;
|
||||
$client->email_verified_at = now();
|
||||
$client->save();
|
||||
|
||||
$token = JWTAuth::fromUser($client);
|
||||
$refreshToken = $this->createRefreshToken($client->id);
|
||||
|
||||
$this->sendWelcomeEmail($client);
|
||||
@@ -84,7 +126,50 @@ class ClientAuthService
|
||||
}
|
||||
|
||||
/**
|
||||
* Gửi email chào mừng cho khách hàng mới đăng ký.
|
||||
* Gửi lại OTP xác thực (rate limit bởi caller nếu cần).
|
||||
*/
|
||||
public function resendVerifyCode(string $email): void
|
||||
{
|
||||
$client = \App\Models\Customer::whereRaw('LOWER(email) = ?', [strtolower(trim($email))])->first();
|
||||
|
||||
if (! $client || $client->status !== 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
// Nếu mã cũ còn hạn thì không gửi lại — phải đợi hết hạn
|
||||
if ($client->email_verify_code_expired_at && now()->lt($client->email_verify_code_expired_at)) {
|
||||
throw ValidationException::withMessages([
|
||||
'email' => ['Mã xác thực vẫn còn hiệu lực, vui lòng kiểm tra email của bạn.'],
|
||||
]);
|
||||
}
|
||||
|
||||
$code = str_pad((string) random_int(0, 999999), 6, '0', STR_PAD_LEFT);
|
||||
|
||||
$client->email_verify_code = $code;
|
||||
$client->email_verify_code_expired_at = now()->addMinutes(5);
|
||||
$client->save();
|
||||
|
||||
$this->sendVerifyEmail($client, $code);
|
||||
}
|
||||
|
||||
protected function sendVerifyEmail($client, string $code): void
|
||||
{
|
||||
try {
|
||||
Mail::to($client->email)->send(new VerifyAccountMail([
|
||||
'fullname' => $client->fullname,
|
||||
'email' => $client->email,
|
||||
'verify_code' => $code,
|
||||
]));
|
||||
} catch (\Throwable $e) {
|
||||
Log::warning('Send verify email failed: ' . $e->getMessage(), [
|
||||
'customer_id' => $client->id,
|
||||
'email' => $client->email,
|
||||
]);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Gửi email chào mừng sau khi xác thực tài khoản thành công.
|
||||
*/
|
||||
protected function sendWelcomeEmail($client): void
|
||||
{
|
||||
|
||||
Reference in New Issue
Block a user