diff --git a/app/Http/Requests/Task/StoreTaskRequest.php b/app/Http/Requests/Task/StoreTaskRequest.php index 52d331d3ab92..124c1e7d9c9f 100644 --- a/app/Http/Requests/Task/StoreTaskRequest.php +++ b/app/Http/Requests/Task/StoreTaskRequest.php @@ -54,7 +54,15 @@ class StoreTaskRequest extends Request $rules['project_id'] = 'bail|required|exists:projects,id,company_id,'.$user->company()->id.',is_deleted,0'; } - $rules['timelog'] = ['bail','array',function ($attribute, $values, $fail) { + $rules['time_log'] = ['bail', function ($attribute, $values, $fail) { + + if(is_string($values)) + $values = json_decode($values, 1); + + if(!is_array($values)) { + return $fail('The '.$attribute.' is invalid. Must be an array.'); + } + foreach ($values as $k) { if (!is_int($k[0]) || !is_int($k[1])) { $fail('The '.$attribute.' - '.print_r($k, 1).' is invalid. Unix timestamps only.'); diff --git a/app/Http/Requests/Task/UpdateTaskRequest.php b/app/Http/Requests/Task/UpdateTaskRequest.php index 7107df9fe63f..f9d1d2d96f78 100644 --- a/app/Http/Requests/Task/UpdateTaskRequest.php +++ b/app/Http/Requests/Task/UpdateTaskRequest.php @@ -34,27 +34,43 @@ class UpdateTaskRequest extends Request if ($this->task->invoice_id && $this->task->company->invoice_task_lock) { return false; } + + /** @var \App\Models\User $user */ + $user = auth()->user(); - return auth()->user()->can('edit', $this->task); + return $user->can('edit', $this->task); } public function rules() { + + /** @var \App\Models\User $user */ + $user = auth()->user(); + $rules = []; if (isset($this->number)) { - $rules['number'] = Rule::unique('tasks')->where('company_id', auth()->user()->company()->id)->ignore($this->task->id); + $rules['number'] = Rule::unique('tasks')->where('company_id', $user->company()->id)->ignore($this->task->id); } if (isset($this->client_id)) { - $rules['client_id'] = 'bail|required|exists:clients,id,company_id,'.auth()->user()->company()->id.',is_deleted,0'; + $rules['client_id'] = 'bail|required|exists:clients,id,company_id,'.$user->company()->id.',is_deleted,0'; } if (isset($this->project_id)) { - $rules['project_id'] = 'bail|required|exists:projects,id,company_id,'.auth()->user()->company()->id.',is_deleted,0'; + $rules['project_id'] = 'bail|required|exists:projects,id,company_id,'.$user->company()->id.',is_deleted,0'; } - $rules['timelog'] = ['bail','array',function ($attribute, $values, $fail) { + $rules['time_log'] = ['bail',function ($attribute, $values, $fail) { + + if(is_string($values)) { + $values = json_decode($values, 1); + } + + if(!is_array($values)) { + return $fail('The '.$attribute.' is invalid. Must be an array.'); + } + foreach ($values as $k) { if (!is_int($k[0]) || !is_int($k[1])) { $fail('The '.$attribute.' - '.print_r($k, 1).' is invalid. Unix timestamps only.'); diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 2d7d9e4363fe..e93640852cb9 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -132,6 +132,26 @@ class TaskApiTest extends TestCase $this->assertEquals(41, $arr['data']['rate']); } + public function testTaskTimelogParse() + { + $data = [ + "description" => "xx", + "rate" => "6574", + "time_log" => "[[Oct 31, 2023 12:00 am,Oct 31, 2023 1:00 am]]" + ]; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson("/api/v1/tasks", $data); + + $response->assertStatus(422); + $arr = $response->json(); + + + + } + public function testTaskProjectRateSet() { @@ -579,19 +599,16 @@ class TaskApiTest extends TestCase public function testTimeLogValidation() { $data = [ - 'timelog' => $this->faker->firstName(), + 'time_log' => $this->faker->firstName(), ]; - try { $response = $this->withHeaders([ 'X-API-SECRET' => config('ninja.api_secret'), 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/tasks', $data); + ])->postJson('/api/v1/tasks', $data); + + $response->assertStatus(422); - $arr = $response->json(); - } catch (ValidationException $e) { - $response->assertStatus(302); - } } public function testTimeLogValidation1() @@ -629,19 +646,16 @@ class TaskApiTest extends TestCase public function testTimeLogValidation3() { $data = [ - 'timelog' => [["a","b",'d'],["c","d",'d']], + 'time_log' => [["a","b",'d'],["c","d",'d']], ]; - try { - $response = $this->withHeaders([ - 'X-API-SECRET' => config('ninja.api_secret'), - 'X-API-TOKEN' => $this->token, - ])->post('/api/v1/tasks', $data); + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->postJson('/api/v1/tasks', $data); + + $response->assertStatus(422); - $arr = $response->json(); - } catch (ValidationException $e) { - $response->assertStatus(302); - } } public function testTimeLogValidation4()