12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879 |
- <?php
- namespace App\Http\Requests\API\Project;
- use App\Models\Asset;
- use App\Models\Enums\AssetStatus;
- use App\Models\Plan;
- use App\Models\User;
- use Illuminate\Foundation\Http\FormRequest;
- use Illuminate\Support\Facades\Auth;
- use Illuminate\Validation\Rule;
- use Illuminate\Validation\Rules\Enum;
- class CreateOrUpdateRequest extends FormRequest
- {
- /**
- * Determine if the user is authorized to make this request.
- */
- public function authorize(): bool
- {
- return true;
- }
- /**
- * Get the validation rules that apply to the request.
- *
- * @return array<string, \Illuminate\Contracts\Validation\ValidationRule|array<mixed>|string>
- */
- public function rules(): array
- {
- return [
- 'name' => 'required|max:150',
- 'code' => [
- 'required',
- 'max:50',
- ],
- 'const' => 'numeric',
- 'begin' => 'required|date',
- 'end' => 'required|date',
- 'type' => 'required|max:20',
- 'acl' => 'required|in:private,custom',
- 'available_days' => 'numeric',
- 'whitelist' => [
- 'array',
- function ($attribute, $value, $fail) {
- $userCount = User::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
- if ($userCount != count($value)) {
- $fail('The selected user is invalid.');
- }
- }
- ],
- 'plans' => [
- 'array',
- function ($attribute, $value, $fail) {
- $count = Plan::where("company_id", Auth::user()->company_id)->whereIn('id', $value)->count();
- if ($count != count($value)) {
- $fail('The selected plan is invalid.');
- }
- }
- ],
- 'assets' => [
- 'array',
- function ($attribute, $value, $fail) {
- $count = Asset::query()
- ->leftJoin("project_asset", "assets.id", "=", "project_asset.asset_id")
- ->where("company_id", Auth::user()->company_id)
- ->whereNull("project_asset.id")
- ->whereIn('assets.id', $value)->count();
- if ($count != count($value)) {
- $fail('The selected asset is invalid.');
- }
- }
- ],
- 'latitude' => 'numeric',
- 'longitude' => 'numeric',
- ];
- }
- }
|