From a028456a09a855835731fbc8a966e6978b508a66 Mon Sep 17 00:00:00 2001 From: David Bomba Date: Wed, 19 Jul 2023 08:01:47 +1000 Subject: [PATCH] Add filters for task start date --- app/Repositories/TaskRepository.php | 24 ++++++++++++--- ..._214607_add_start_date_column_to_tasks.php | 29 +++++++++++++++++++ tests/Feature/TaskApiTest.php | 16 ++++++++++ 3 files changed, 65 insertions(+), 4 deletions(-) create mode 100644 database/migrations/2023_07_18_214607_add_start_date_column_to_tasks.php diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index 387979342aeb..be1f17e4911d 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -101,9 +101,6 @@ class TaskRepository extends BaseRepository $key_values = array_column($time_log, 0); array_multisort($key_values, SORT_ASC, $time_log); - // array_multisort($time_log); - // ksort($time_log); - if (isset($data['action'])) { if ($data['action'] == 'start') { $task->is_running = true; @@ -121,8 +118,12 @@ class TaskRepository extends BaseRepository $task->is_running = $data['is_running'] ? 1 : 0; } + $task->calculated_start_date = $this->harvestStartDate($time_log); + $task->time_log = json_encode($time_log); + + $task->saveQuietly(); if (array_key_exists('documents', $data)) { @@ -132,6 +133,17 @@ class TaskRepository extends BaseRepository return $task; } + private function harvestStartDate($time_log) + { + + if(isset($time_log[0][0])){ + return \Carbon\Carbon::createFromTimestamp($time_log[0][0]); + } + + return null; + + } + /** * Store tasks in bulk. * @@ -199,8 +211,12 @@ class TaskRepository extends BaseRepository if (strlen($task->time_log) < 5) { $log = []; - $log = array_merge($log, [[time(), 0]]); + $start_time = time(); + + $log = array_merge($log, [[$start_time, 0]]); $task->time_log = json_encode($log); + $task->calculated_start_date = \Carbon\Carbon::createFromTimestamp($start_time); + $task->saveQuietly(); } diff --git a/database/migrations/2023_07_18_214607_add_start_date_column_to_tasks.php b/database/migrations/2023_07_18_214607_add_start_date_column_to_tasks.php new file mode 100644 index 000000000000..ad988bd747ef --- /dev/null +++ b/database/migrations/2023_07_18_214607_add_start_date_column_to_tasks.php @@ -0,0 +1,29 @@ +date('calculated_start_date')->nullable(); + }); + } + + /** + * Reverse the migrations. + * + * @return void + */ + public function down() + { + } +}; diff --git a/tests/Feature/TaskApiTest.php b/tests/Feature/TaskApiTest.php index 236f2fa66291..1651954bb121 100644 --- a/tests/Feature/TaskApiTest.php +++ b/tests/Feature/TaskApiTest.php @@ -31,6 +31,8 @@ class TaskApiTest extends TestCase use DatabaseTransactions; use MockAccountData; + private $faker; + protected function setUp() :void { parent::setUp(); @@ -100,6 +102,20 @@ class TaskApiTest extends TestCase } } + public function testStartDate() + { + $x = []; + + $this->assertFalse(isset($x[0][0])); + + $x[0][0] = 'a'; + + $this->assertTrue(isset($x[0][0])); + + $this->assertNotNull(\Carbon\Carbon::createFromTimestamp($x[0][0])); + + } + public function testMultiSortArray() {