Update task status_order on update()

This commit is contained in:
David Bomba 2021-02-24 13:12:23 +11:00
parent 90b33ef063
commit f7e4bca5a5
6 changed files with 146 additions and 7 deletions

View File

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

View File

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

View File

@ -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);
}
}

View File

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

View File

@ -0,0 +1,94 @@
<?php
/**
* Invoice Ninja (https://invoiceninja.com).
*
* @link https://github.com/invoiceninja/invoiceninja source repository
*
* @copyright Copyright (c) 2021. Invoice Ninja LLC (https://invoiceninja.com)
*
* @license https://opensource.org/licenses/AAL
*/
namespace Tests\Feature;
use App\Models\Project;
use App\Models\Task;
use App\Models\TaskStatus;
use App\Utils\Traits\MakesHash;
use Illuminate\Foundation\Testing\DatabaseTransactions;
use Illuminate\Routing\Middleware\ThrottleRequests;
use Tests\MockAccountData;
use Tests\TestCase;
/**
* @test
*/
class TaskStatusSortOnUpdateTest extends TestCase
{
use DatabaseTransactions;
use MockAccountData;
use MakesHash;
public function setUp() :void
{
parent::setUp();
$this->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);
}
}

View File

@ -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,