Skip to content

FOUR-18636 #7399

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Sep 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
55 changes: 55 additions & 0 deletions ProcessMaker/Http/Controllers/Api/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,15 @@ class TaskController extends Controller
'Completed' => 'CLOSED',
];

protected $defaultCase = [
'id', // Task #
'element_name', // Task Name
'user_id', // Participant
'process_id', // Process
'due_at', // Due At
'process_request_id', // Request Id #
];

/**
* Display a listing of the resource.
*
Expand Down Expand Up @@ -149,6 +158,52 @@ public function index(Request $request, $getTotal = false, User $user = null)
return new TaskCollection($response);
}

/**
* Get the task list related to the case
* @param Request $request
* @param User $user used by Saved Search package to return accurate counts
* @return array
*/
public function indexCase(Request $request, User $user = null)
{
if (!$user) {
$user = Auth::user();
}

// Review the 'case_number'
$request->validate([
'case_number' => 'required|integer',
]);
$caseNumber = $request->input('case_number');

// Get only the columns defined
$query = ProcessRequestToken::select($this->defaultCase);
// Filter by case_number
$query->whereHas('processRequest', function ($query) use ($caseNumber) {
$query->where('case_number', $caseNumber);
});
// Filter the status
$query->where('status', 'ACTIVE');
// Return the process information
$query->with(['process' => function ($query) {
$query->select('id', 'name');
}]);
// Return the user information
$query->with(['user' => function ($query) {
$query->select('id', 'firstname', 'lastname', 'username', 'avatar');
}]);
// Filter only the task related to the user
$this->applyForCurrentUser($query, $user);
try {
$response = $query->paginate($request->input('per_page', 10));
$response->inOverdue = 0;
} catch (QueryException $e) {
return $this->handleQueryException($e);
}

return new TaskCollection($response);
}

/**
* Display the specified resource.
* @TODO remove this method,view and route this is not a used file
Expand Down
1 change: 1 addition & 0 deletions routes/api.php
Original file line number Diff line number Diff line change
Expand Up @@ -189,6 +189,7 @@

// Tasks
Route::get('tasks', [TaskController::class, 'index'])->name('tasks.index'); // Already filtered in controller
Route::get('tasks-by-case', [TaskController::class, 'indexCase'])->name('tasks.indexCase');
Route::get('tasks/{task}', [TaskController::class, 'show'])->name('tasks.show')->middleware('can:view,task');
Route::get('tasks/{task}/screen_fields', [TaskController::class, 'getScreenFields'])->name('getScreenFields.show')->middleware('can:view,task');
Route::get('tasks/{task}/screens/{screen}', [TaskController::class, 'getScreen'])->name('tasks.get_screen')->middleware('can:viewScreen,task,screen');
Expand Down
66 changes: 66 additions & 0 deletions tests/Feature/Api/TaskControllerTest.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
<?php

namespace Tests\Feature\Api;

use Illuminate\Support\Facades\Auth;
use ProcessMaker\Models\Process;
use ProcessMaker\Models\ProcessRequest;
use ProcessMaker\Models\ProcessRequestToken;
use ProcessMaker\Models\User;
use Tests\Feature\Shared\RequestHelper;
use Tests\TestCase;

class TaskControllerTest extends TestCase
{
use RequestHelper;

const API_TASK_BY_CASE = '/tasks-by-case';

/**
* Test indexCase without case_number.
*
* @return void
*/
public function test_index_case_requires_case_number()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);

// Call the endpoint without the 'case_number' parameter
$response = $this->apiCall('GET', self::API_TASK_BY_CASE);

// Check if the response returns a 400 error due to missing 'case_number'
$response->assertStatus(422)
->assertJson(['message' => 'The Case number field is required.']);
}

/**
* Test indexCase returns tasks related to the case_number.
*
* @return void
*/
public function test_index_case_returns_tasks_for_case_number()
{
// Simulate an authenticated user
$user = User::factory()->create();
Auth::login($user);

// Create a ProcessRequestToken associated with a specific case_number
$processRequest = ProcessRequest::factory()->create([
'case_number' => 81,
]);
$processRequestToken = ProcessRequestToken::factory()->create([
'user_id' => $user->id,
'status' => 'ACTIVE',
'process_request_id' => $processRequest->id, // id del ProcessRequest
]);

// Call the endpoint with the 'case_number' parameter
$response = $this->apiCall('GET', self::API_TASK_BY_CASE . '?case_number=81');

// Check if the response is successful and contains the expected tasks
$response->assertStatus(200);
$this->assertCount(0, $response->json('data'));
}
}
Loading