fix(contact): update orderRef to use OrderReference value object
Refactor orderRef handling in contact forms to ensure proper validation and type safety.
This commit is contained in:
parent
10b97e3a00
commit
d1d0585b34
7 changed files with 40 additions and 35 deletions
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
|||
namespace App\Cqs\Actions\Contact;
|
||||
|
||||
use App\Contracts\ActionInput;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Webmozart\Assert\Assert;
|
||||
|
||||
|
|
@ -16,7 +17,7 @@ final readonly class SubmitContactFormInput implements ActionInput
|
|||
public string $email,
|
||||
public string $subject,
|
||||
public string $message,
|
||||
public ?string $orderRef = null,
|
||||
public ?OrderReference $orderRef = null,
|
||||
public ?UploadedFile $file = null,
|
||||
) {
|
||||
Assert::stringNotEmpty(trim($this->lastName));
|
||||
|
|
@ -24,9 +25,5 @@ final readonly class SubmitContactFormInput implements ActionInput
|
|||
Assert::stringNotEmpty(trim($this->email));
|
||||
Assert::stringNotEmpty(trim($this->subject));
|
||||
Assert::stringNotEmpty(trim($this->message));
|
||||
|
||||
if ($this->orderRef !== null) {
|
||||
Assert::stringNotEmpty(trim($this->orderRef));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -6,6 +6,7 @@ namespace App\Http\Requests\Fo;
|
|||
|
||||
use App\Cqs\Actions\Contact\SubmitContactFormInput;
|
||||
use App\Rules\FileExtensionMatchesMimeType;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
|
@ -34,7 +35,21 @@ class ContactFormRequest extends FormRequest
|
|||
->validateMxRecord(),
|
||||
],
|
||||
'subject' => ['required', 'string', 'min:2'],
|
||||
'order_ref' => ['nullable', 'string', 'regex:/^[0-9]+$/'],
|
||||
'order_ref' => [
|
||||
'nullable',
|
||||
'string',
|
||||
static function (string $attribute, mixed $value, \Closure $fail): void {
|
||||
if (! is_string($value) || $value === '') {
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
OrderReference::fromString($value);
|
||||
} catch (\DomainException) {
|
||||
$fail(trans('validation.regex', ['attribute' => $attribute]));
|
||||
}
|
||||
},
|
||||
],
|
||||
'message' => ['required', 'string', 'min:20', 'max:1900'],
|
||||
'file' => [
|
||||
'nullable',
|
||||
|
|
@ -71,7 +86,7 @@ class ContactFormRequest extends FormRequest
|
|||
email: $this->string('email')->value(),
|
||||
subject: $this->string('subject')->value(),
|
||||
message: $this->string('message')->value(),
|
||||
orderRef: empty($orderRef) ? null : $this->string('order_ref')->value(),
|
||||
orderRef: empty($orderRef) ? null : OrderReference::fromString($this->string('order_ref')->value()),
|
||||
file: $uploadedFile,
|
||||
);
|
||||
}
|
||||
|
|
|
|||
|
|
@ -12,7 +12,7 @@ final readonly class ContactFormData
|
|||
public string $email,
|
||||
public string $subject,
|
||||
public string $message,
|
||||
public ?string $orderRef = null,
|
||||
public ?OrderReference $orderRef = null,
|
||||
public ?AttachedFile $file = null,
|
||||
) {
|
||||
if (trim($lastName) === '') {
|
||||
|
|
@ -30,8 +30,5 @@ final readonly class ContactFormData
|
|||
if (trim($message) === '') {
|
||||
throw new \DomainException('message cannot be empty');
|
||||
}
|
||||
if ($orderRef !== null && trim($orderRef) === '') {
|
||||
throw new \DomainException('orderRef cannot be empty if provided');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -10,11 +10,13 @@ use App\Lib\PrivateS3Disk;
|
|||
use App\Models\Page;
|
||||
use App\Models\Shop;
|
||||
use Database\Seeders\ContentSeeder;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Cache;
|
||||
use Illuminate\Support\Facades\Config;
|
||||
use Illuminate\Support\Facades\Queue;
|
||||
use Illuminate\Support\Facades\View;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Uwil\Storage\Contracts\StorageService;
|
||||
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
||||
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
||||
|
|
@ -78,7 +80,7 @@ describe('ContactController', function (): void {
|
|||
'last_name' => 'Dupont',
|
||||
'email' => 'jean.dupont@gmail.com',
|
||||
'subject' => 'Question',
|
||||
'order_ref' => '123456',
|
||||
'order_ref' => (string) new OrderReference(Uuid::uuid7()),
|
||||
'message' => 'Bonjour, ceci est un message de test qui contient au moins vingt caractères.',
|
||||
'ok_cgv' => '1',
|
||||
];
|
||||
|
|
|
|||
|
|
@ -4,10 +4,12 @@ declare(strict_types=1);
|
|||
|
||||
use App\Cqs\Actions\Contact\SubmitContactFormInput;
|
||||
use App\Http\Requests\Fo\ContactFormRequest;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Illuminate\Filesystem\FilesystemAdapter;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Illuminate\Support\Facades\Storage;
|
||||
use Illuminate\Support\Facades\Validator;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
describe('ContactFormRequest', function () {
|
||||
it('validates correct rules', function () {
|
||||
|
|
@ -21,7 +23,7 @@ describe('ContactFormRequest', function () {
|
|||
'first_name' => 'John',
|
||||
'email' => 'john.doe@gmail.com',
|
||||
'subject' => 'Help with my order',
|
||||
'order_ref' => '123456',
|
||||
'order_ref' => (string) new OrderReference(Uuid::uuid7()),
|
||||
'message' => 'This is a message that is longer than 20 characters.',
|
||||
'ok_cgv' => true,
|
||||
]);
|
||||
|
|
@ -106,14 +108,15 @@ describe('ContactFormRequest', function () {
|
|||
|
||||
it('converts to action input without file', function () {
|
||||
// GIVEN
|
||||
$request = new ContactFormRequest();
|
||||
$orderRef = new OrderReference(Uuid::uuid7());
|
||||
$request = new ContactFormRequest();
|
||||
$request->initialize();
|
||||
$request->merge([
|
||||
'last_name' => 'Doe',
|
||||
'first_name' => 'John',
|
||||
'email' => 'john.doe@gmail.com',
|
||||
'subject' => 'Help',
|
||||
'order_ref' => '123',
|
||||
'order_ref' => (string) $orderRef,
|
||||
'message' => 'This is a message that is longer than 20 characters.',
|
||||
]);
|
||||
|
||||
|
|
@ -126,7 +129,7 @@ describe('ContactFormRequest', function () {
|
|||
->and($input->firstName)->toBe('John')
|
||||
->and($input->email)->toBe('john.doe@gmail.com')
|
||||
->and($input->subject)->toBe('Help')
|
||||
->and($input->orderRef)->toBe('123')
|
||||
->and($input->orderRef?->value->toString())->toBe($orderRef->value->toString())
|
||||
->and($input->message)->toBe('This is a message that is longer than 20 characters.')
|
||||
->and($input->file)->toBeNull();
|
||||
});
|
||||
|
|
|
|||
|
|
@ -8,7 +8,9 @@ use App\Cqs\Actions\Contact\SubmitContactFormInput;
|
|||
use App\Lib\PrivateS3Disk;
|
||||
use Domain\Models\ValueObjects\AttachedFile;
|
||||
use Domain\Models\ValueObjects\ContactFormData;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Illuminate\Http\UploadedFile;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
use Uwil\Storage\Contracts\StorageService;
|
||||
use Uwil\Storage\Lib\ValueObjects\DiskName;
|
||||
use Uwil\Storage\Lib\ValueObjects\StorageFilePath;
|
||||
|
|
@ -18,14 +20,15 @@ describe('SubmitContactForm action', function (): void {
|
|||
// GIVEN
|
||||
$storage = \Mockery::mock(StorageService::class);
|
||||
$storage->shouldNotReceive('storeUploadedOntoDisk');
|
||||
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
||||
$input = new SubmitContactFormInput(
|
||||
$action = new SubmitContactForm($storage, new PrivateS3Disk());
|
||||
$orderRef = new OrderReference(Uuid::uuid7());
|
||||
$input = new SubmitContactFormInput(
|
||||
lastName: 'Doe',
|
||||
firstName: 'John',
|
||||
email: 'john.doe@example.com',
|
||||
subject: 'Question',
|
||||
message: 'Bonjour, ceci est un message de test valide.',
|
||||
orderRef: '12345',
|
||||
orderRef: $orderRef,
|
||||
);
|
||||
|
||||
// WHEN
|
||||
|
|
@ -37,7 +40,7 @@ describe('SubmitContactForm action', function (): void {
|
|||
->and($result->firstName)->toBe('John')
|
||||
->and($result->email)->toBe('john.doe@example.com')
|
||||
->and($result->subject)->toBe('Question')
|
||||
->and($result->orderRef)->toBe('12345')
|
||||
->and($result->orderRef)->toBe($orderRef)
|
||||
->and($result->file)->toBeNull();
|
||||
});
|
||||
|
||||
|
|
|
|||
|
|
@ -5,6 +5,8 @@ declare(strict_types=1);
|
|||
namespace Tests\Unit\Domain\Models\ValueObjects;
|
||||
|
||||
use Domain\Models\ValueObjects\ContactFormData;
|
||||
use Domain\Models\ValueObjects\OrderReference;
|
||||
use Ramsey\Uuid\Uuid;
|
||||
|
||||
describe('ContactFormData', function () {
|
||||
|
||||
|
|
@ -30,7 +32,7 @@ describe('ContactFormData', function () {
|
|||
$email = 'john@example.com';
|
||||
$subject = 'Inquiry';
|
||||
$message = 'This is a message.';
|
||||
$orderRef = 'REF-123';
|
||||
$orderRef = new OrderReference(Uuid::uuid7());
|
||||
$file = null;
|
||||
|
||||
// WHEN
|
||||
|
|
@ -118,17 +120,3 @@ describe('ContactFormData', function () {
|
|||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
});
|
||||
|
||||
it('throws exception if orderRef is provided but empty', function () {
|
||||
// GIVEN
|
||||
$orderRef = ' ';
|
||||
|
||||
// WHEN
|
||||
$action = fn () => getNewContactFormData([
|
||||
'orderRef' => $orderRef,
|
||||
|
||||
]);
|
||||
|
||||
// THEN
|
||||
expect($action)->toThrow(\DomainException::class);
|
||||
});
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue