Files
Investidor-V2/app/Http/Requests/StoreInterestRequestRequest.php
T
nala 4f35313d06
tests / ci (push) Canceled after 0s
novas funcionalidades dos formulario
2026-10-02 11:13:37 +02:00

79 lines
3.5 KiB
PHP

<?php
namespace App\Http\Requests;
use App\Services\ExistingBusinessDrafts;
use Illuminate\Foundation\Http\FormRequest;
use Illuminate\Support\Facades\DB;
use Illuminate\Support\Str;
use Illuminate\Validation\Rule;
use Illuminate\Validation\Validator;
class StoreInterestRequestRequest extends FormRequest
{
public function authorize(): bool
{
return true;
}
/** @return array<string, array<int, mixed>> */
public function rules(): array
{
$key = $this->header('Idempotency-Key');
$knownKey = is_string($key) && Str::isUuid($key)
&& DB::table('interest_submission_receipts')->where('key_hash', hash('sha256', strtolower($key)))->exists();
return [
'fullName' => ['required', 'string', 'min:3', 'max:150'],
'email' => ['required', 'email:rfc,dns', 'max:150'],
'phone' => ['nullable', 'string', 'max:30'],
'investorType' => ['required', 'string', Rule::in([
'individual', 'empresa', 'fundo', 'entidade_publica', 'outro',
])],
'country' => ['required', 'string', 'max:100'],
'sectorInterest' => ['required', 'string', 'max:150'],
// A known receipt can replay after withdrawal; the service verifies the payload hash.
'opportunityReference' => ['nullable', 'string', 'max:80', ...($knownKey ? [] : [
Rule::exists('opportunities', 'reference')->where(fn ($query) => $query
->where('status', 'published')->whereNotNull('published_at')
->where('published_at', '<=', now())->whereNull('deleted_at')),
])],
'existingBusinessId' => ['nullable', 'uuid', ...($knownKey ? [] : [
Rule::exists('existing_businesses', 'id')->where(fn ($query) => $query
->where('status', 'published')->whereNotNull('published_at')->where('published_at', '<=', now())->whereNull('deleted_at')),
])],
'modality' => ['nullable', 'required_with:existingBusinessId', 'string', 'max:50', Rule::in(ExistingBusinessDrafts::MODALITIES)],
'investmentRange' => ['nullable', 'string', 'max:80'],
'message' => ['required', 'string', 'min:20', 'max:2000'],
'privacyConsent' => ['required', 'accepted'],
'shareWithProponent' => ['sometimes', 'boolean'],
];
}
/** @return list<callable(Validator): void> */
public function after(): array
{
return [function (Validator $validator): void {
if ($this->filled('existingBusinessId') && $this->filled('opportunityReference')) {
$validator->errors()->add('existingBusinessId', 'Seleccione apenas uma oportunidade.');
}
}];
}
public function messages(): array
{
return [
'opportunityReference.exists' => 'A oportunidade seleccionada já não está disponível.',
'fullName.required' => 'O nome completo é obrigatório.',
'email.required' => 'O email é obrigatório.',
'email.email' => 'Indique um email válido.',
'investorType.required' => 'Seleccione o tipo de investidor.',
'country.required' => 'O país é obrigatório.',
'sectorInterest.required' => 'O sector de interesse é obrigatório.',
'message.required' => 'A mensagem é obrigatória.',
'message.min' => 'A mensagem deve ter pelo menos 20 caracteres.',
'privacyConsent.accepted' => 'É necessário aceitar a política de privacidade.',
];
}
}