Fixes for task edge cases

This commit is contained in:
David Bomba 2022-03-30 13:54:40 +11:00
parent 2d0c6fd0af
commit 3a22fd0384
4 changed files with 82 additions and 4 deletions

View File

@ -124,7 +124,7 @@ class Handler extends ExceptionHandler
}
});
if ($this->validException($exception) && auth()->guard('contact')->user()->company->account->report_errors) {
if ($this->validException($exception)) {
app('sentry')->captureException($exception);
}
}

View File

@ -280,6 +280,7 @@ class TaskController extends BaseController
$old_task = json_decode(json_encode($task));
$task = $this->task_repo->save($request->all(), $task);
$task = $this->task_repo->triggeredActions($request, $task);
if($task->status_order != $old_task->status_order)
$this->task_repo->sortStatuses($old_task, $task);
@ -377,6 +378,7 @@ class TaskController extends BaseController
public function store(StoreTaskRequest $request)
{
$task = $this->task_repo->save($request->all(), TaskFactory::create(auth()->user()->company()->id, auth()->user()->id));
$task = $this->task_repo->triggeredActions($request, $task);
event(new TaskWasCreated($task, $task->company, Ninja::eventVars(auth()->user() ? auth()->user()->id : null)));

View File

@ -203,7 +203,7 @@ class TaskRepository extends BaseRepository
$last = end($log);
if($last[1] !== 0){
if(is_array($last) && $last[1] !== 0){
$new = [time(), 0];
$log = array_merge($log, [$new]);
@ -212,6 +212,7 @@ class TaskRepository extends BaseRepository
}
return $task;
}
public function stop(Task $task)
@ -220,7 +221,7 @@ class TaskRepository extends BaseRepository
$last = end($log);
if($last[1] === 0){
if(is_array($last) && $last[1] === 0){
$last[1] = time();
@ -231,5 +232,21 @@ class TaskRepository extends BaseRepository
$task->save();
}
return $task;
}
public function triggeredActions($request, $task)
{
if ($request->has('start') && $request->input('start') == 'true') {
$task = $this->start($task);
}
if ($request->has('stop') && $request->input('stop') == 'true') {
$task = $this->stop($task);
}
return $task;
}
}

View File

@ -90,7 +90,7 @@ class TaskApiTest extends TestCase
$response->assertStatus(200);
$this->assertEquals('taskynumber', $arr['data']['number']);
$this->assertLessThan(5, strlen($arr['data']['time_log']));
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
@ -113,6 +113,23 @@ class TaskApiTest extends TestCase
$this->assertNotEmpty($arr['data']['number']);
}
public function testTaskPostNoDefinedTaskNumber()
{
$data = [
'description' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks', $data);
$arr = $response->json();
$response->assertStatus(200);
$this->assertNotEmpty($arr['data']['number']);
}
public function testTaskPostWithActionStart()
{
$data = [
@ -224,4 +241,46 @@ class TaskApiTest extends TestCase
$this->assertTrue($arr['data'][0]['is_deleted']);
}
public function testTaskPostWithStartAction()
{
$data = [
'description' => $this->faker->firstName,
'number' => 'taskynumber2'
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks?start=true', $data);
$arr = $response->json();
$response->assertStatus(200);
$this->assertEquals('taskynumber2', $arr['data']['number']);
$this->assertGreaterThan(5, strlen($arr['data']['time_log']));
}
public function testTaskPostWithStopAction()
{
$data = [
'description' => $this->faker->firstName,
];
$response = $this->withHeaders([
'X-API-SECRET' => config('ninja.api_secret'),
'X-API-TOKEN' => $this->token,
])->post('/api/v1/tasks?stop=true', $data);
$arr = $response->json();
$response->assertStatus(200);
$this->assertLessThan(5, strlen($arr['data']['time_log']));
}
}