Fixes for time log validation

This commit is contained in:
David Bomba 2023-10-25 20:41:03 +11:00
parent 6313761003
commit bc0690fafc
3 changed files with 61 additions and 23 deletions

View File

@ -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.');

View File

@ -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.');

View File

@ -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()