diff --git a/app/Console/Commands/CreateSingleAccount.php b/app/Console/Commands/CreateSingleAccount.php index ffd9e33e0b99..082f30842f34 100644 --- a/app/Console/Commands/CreateSingleAccount.php +++ b/app/Console/Commands/CreateSingleAccount.php @@ -17,6 +17,7 @@ use App\Events\Invoice\InvoiceWasCreated; use App\Factory\InvoiceFactory; use App\Factory\InvoiceItemFactory; use App\Helpers\Invoice\InvoiceSum; +use App\Jobs\Company\CreateCompanyTaskStatuses; use App\Models\Account; use App\Models\Client; use App\Models\ClientContact; @@ -148,6 +149,8 @@ class CreateSingleAccount extends Command $this->createClient($company, $user); } + CreateCompanyTaskStatuses::dispatchNow($company, $user); + for ($x = 0; $x < $this->count; $x++) { $client = $company->clients->random(); diff --git a/app/Http/Controllers/TaskController.php b/app/Http/Controllers/TaskController.php index d5a3bee52c5d..076b3c346929 100644 --- a/app/Http/Controllers/TaskController.php +++ b/app/Http/Controllers/TaskController.php @@ -274,8 +274,13 @@ class TaskController extends BaseController return $request->disallowUpdate(); } + $old_task = json_decode(json_encode($task)); + $task = $this->task_repo->save($request->all(), $task); + if($task->status_order != $old_task->status_order) + $this->task_repo->sortStatuses($old_task, $task); + event(new TaskWasUpdated($task, $task->company, Ninja::eventVars())); return $this->itemResponse($task->fresh()); diff --git a/app/Models/Project.php b/app/Models/Project.php index 3e69a6a60252..42b84cbbaa29 100644 --- a/app/Models/Project.php +++ b/app/Models/Project.php @@ -71,11 +71,10 @@ class Project extends BaseModel { return $this->belongsTo(User::class)->withTrashed(); } - // /** - // * @return \Illuminate\Database\Eloquent\Relations\HasMany - // */ - // public function tasks() - // { - // return $this->hasMany('App\Models\Task'); - // } + + public function tasks() + { + return $this->hasMany(Task::class); + } + } diff --git a/app/Repositories/TaskRepository.php b/app/Repositories/TaskRepository.php index bd5ae90de0e8..778b695763ae 100644 --- a/app/Repositories/TaskRepository.php +++ b/app/Repositories/TaskRepository.php @@ -33,6 +33,7 @@ class TaskRepository extends BaseRepository */ public function save(array $data, Task $task) : ?Task { + $task->fill($data); $task->save(); @@ -99,5 +100,36 @@ class TaskRepository extends BaseRepository $task, TaskFactory::create(auth()->user()->company()->id, auth()->user()->id) ); + + } + + /** + * Sorts the task status order IF the old status has changed between requests + * + * @param stdCLass $old_task The old task object + * @param Task $new_task The new Task model + * @return void + */ + public function sortStatuses($old_task, $new_task) + { + + if(!$new_task->project()->exists()) + return; + + $index = $new_task->status_order; + + $tasks = $new_task->project->tasks->reject(function ($task)use($new_task){ + return $task->id == $new_task->id; + }); + + $sorted_tasks = $tasks->filter(function($task, $key)use($index){ + return $key < $index; + })->push($new_task)->merge($tasks->filter(function($task, $key)use($index){ + return $key >= $index; + }))->each(function ($item,$key){ + $item->status_order = $key; + $item->save(); + }); + } } diff --git a/tests/Feature/TaskStatusSortOnUpdateTest.php b/tests/Feature/TaskStatusSortOnUpdateTest.php new file mode 100644 index 000000000000..b5c2732965a8 --- /dev/null +++ b/tests/Feature/TaskStatusSortOnUpdateTest.php @@ -0,0 +1,94 @@ +makeTestData(); + + $this->withoutMiddleware( + ThrottleRequests::class + ); + } + + public function testTasksSort() + { + + $project = Project::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'name' => 'Test Project', + ]); + + for($x=0; $x<10; $x++) + { + $task = Task::factory()->create([ + 'user_id' => $this->user->id, + 'company_id' => $this->company->id, + 'project_id' => $project->id + ]); + + $task->status_id = TaskStatus::where('company_id', $this->company->id)->first()->id; + $task->save(); + } + + + $this->assertTrue($task->project()->exists()); + $this->assertEquals($task->project->tasks->count(), 10); + + $task->status_order = 1; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/tasks/'.$this->encodePrimaryKey($task->id), $task->toArray()); + + $response->assertStatus(200); + + $this->assertEquals($task->fresh()->status_order, 1); + + + $task->status_order = 10; + + $response = $this->withHeaders([ + 'X-API-SECRET' => config('ninja.api_secret'), + 'X-API-TOKEN' => $this->token, + ])->put('/api/v1/tasks/'.$this->encodePrimaryKey($task->id), $task->toArray()); + + $response->assertStatus(200); + + nlog($task->fresh()->project->tasks->toArray()); + + $this->assertEquals($task->fresh()->status_order, 9); + + } + +} diff --git a/tests/MockAccountData.php b/tests/MockAccountData.php index 1d8952dac4ad..016fd2960794 100644 --- a/tests/MockAccountData.php +++ b/tests/MockAccountData.php @@ -20,6 +20,7 @@ use App\Factory\InvoiceInvitationFactory; use App\Factory\InvoiceItemFactory; use App\Factory\InvoiceToRecurringInvoiceFactory; use App\Helpers\Invoice\InvoiceSum; +use App\Jobs\Company\CreateCompanyTaskStatuses; use App\Models\Account; use App\Models\Client; use App\Models\ClientContact; @@ -201,6 +202,8 @@ trait MockAccountData $user_id = $user->id; $this->user = $user; + CreateCompanyTaskStatuses::dispatchNow($this->company, $this->user); + $this->cu = CompanyUserFactory::create($user->id, $this->company->id, $this->account->id); $this->cu->is_owner = true; $this->cu->is_admin = true; @@ -286,6 +289,9 @@ trait MockAccountData 'company_id' => $this->company->id, ]); + $this->task->status_id = TaskStatus::where('company_id', $this->company->id)->first()->id; + $this->task->save(); + $this->expense_category = ExpenseCategory::factory()->create([ 'user_id' => $user_id, 'company_id' => $this->company->id,