user affiliation schema completed
This commit is contained in:
44
backend/app/Http/Requests/StoreUserAffiliationRequest.php
Normal file
44
backend/app/Http/Requests/StoreUserAffiliationRequest.php
Normal file
@@ -0,0 +1,44 @@
|
||||
<?php
|
||||
|
||||
namespace App\Http\Requests;
|
||||
|
||||
use Illuminate\Contracts\Validation\ValidationRule;
|
||||
use Illuminate\Foundation\Http\FormRequest;
|
||||
use Illuminate\Validation\Rule;
|
||||
|
||||
class StoreUserAffiliationRequest extends FormRequest
|
||||
{
|
||||
/**
|
||||
* L'autorizzazione (solo Admin) è gestita dal middleware di rotta.
|
||||
*/
|
||||
public function authorize(): bool
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, ValidationRule|array<mixed>|string>
|
||||
*/
|
||||
public function rules(): array
|
||||
{
|
||||
$rules = [
|
||||
'institution_id' => ['required', 'integer', 'exists:institutions,id'],
|
||||
'user_id' => ['required', 'integer', 'exists:users,id'],
|
||||
'user_position_id' => ['required', 'integer', 'exists:user_positions,id'],
|
||||
'start_year' => ['required', 'integer', 'min:1800', 'max:'.now()->year],
|
||||
'end_year' => ['nullable', 'integer', 'min:1800', 'gte:start_year'],
|
||||
];
|
||||
|
||||
// Un utente non può avere più di un incarico aperto (end_year nullo) nello
|
||||
// stesso ente: il guard definitivo è l'indice unique sulla colonna virtuale
|
||||
// is_open, qui serve solo a restituire un 422 leggibile invece di un 500 SQL.
|
||||
if (! $this->filled('end_year')) {
|
||||
$rules['institution_id'][] = Rule::unique('user_affiliations', 'institution_id')
|
||||
->where('user_id', $this->input('user_id'))
|
||||
->whereNull('end_year')
|
||||
->whereNull('deleted_at');
|
||||
}
|
||||
|
||||
return $rules;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user