diff --git a/database/factories/DelegationFactory.php b/database/factories/DelegationFactory.php index e5917a414..aa43fa7d6 100644 --- a/database/factories/DelegationFactory.php +++ b/database/factories/DelegationFactory.php @@ -1,6 +1,5 @@ define(\ProcessMaker\Model\Delegation::class, function(Faker $faker) { $app = factory(\ProcessMaker\Model\Application::class)->create(); @@ -14,6 +13,8 @@ $factory->define(\ProcessMaker\Model\Delegation::class, function(Faker $faker) { } else{ $user = $users->random(); } + + // Return with default values return [ 'APP_UID' => $app->APP_UID, 'DEL_INDEX' => 1, @@ -35,4 +36,41 @@ $factory->define(\ProcessMaker\Model\Delegation::class, function(Faker $faker) { 'TAS_ID' => $task->TAS_ID, 'DEL_DATA' => '' ]; -}); \ No newline at end of file +}); + +// Create a open delegation +$factory->state(\ProcessMaker\Model\Delegation::class, 'open', function (Faker $faker) { + // Create dates with sense + $delegateDate = $faker->dateTime(); + $initDate = $faker->dateTimeInInterval($delegateDate, '+30 minutes'); + $riskDate = $faker->dateTimeInInterval($initDate, '+1 day'); + $taskDueDate = $faker->dateTimeInInterval($riskDate, '+1 day'); + + return [ + 'DEL_THREAD_STATUS' => 'OPEN', + 'DEL_DELEGATE_DATE' => $delegateDate, + 'DEL_INIT_DATE' => $initDate, + 'DEL_RISK_DATE' => $riskDate, + 'DEL_TASK_DUE_DATE' => $taskDueDate, + 'DEL_FINISH_DATE' => null + ]; +}); + +// Create a closed delegation +$factory->state(\ProcessMaker\Model\Delegation::class, 'closed', function (Faker $faker) { + // Create dates with sense + $delegateDate = $faker->dateTime(); + $initDate = $faker->dateTimeInInterval($delegateDate, '+30 minutes'); + $riskDate = $faker->dateTimeInInterval($initDate, '+1 day'); + $taskDueDate = $faker->dateTimeInInterval($riskDate, '+1 day'); + $finishDate = $faker->dateTimeInInterval($initDate, '+10 days'); + + return [ + 'DEL_THREAD_STATUS' => 'CLOSED', + 'DEL_DELEGATE_DATE' => $delegateDate, + 'DEL_INIT_DATE' => $initDate, + 'DEL_RISK_DATE' => $riskDate, + 'DEL_TASK_DUE_DATE' => $taskDueDate, + 'DEL_FINISH_DATE' => $finishDate + ]; +}); diff --git a/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php b/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php index 2159454c8..232ab57f2 100644 --- a/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php +++ b/tests/unit/workflow/engine/src/ProcessMaker/Model/DelegationTest.php @@ -1,6 +1,8 @@ assertEquals('', $results['data'][0]['APP_STATUS']); } + + /** + * Check if return participation information + * @test + */ + public function it_should_return_participation_info() + { + // Creating one application with two delegations + factory(User::class, 100)->create(); + $process = factory(Process::class)->create(); + $application = factory(Application::class)->create([ + 'APP_UID' => G::generateUniqueID() + ]); + factory(Delegation::class)->states('closed')->create([ + 'APP_UID' => $application->APP_UID + ]); + factory(Delegation::class)->states('open')->create([ + 'APP_UID' => $application->APP_UID, + 'DEL_INDEX' => 2 + ]); + + // Check the information returned + $results = Delegation::getParticipatedInfo($application->APP_UID); + $this->assertEquals('PARTICIPATED', $results['APP_STATUS']); + $this->assertCount(2, $results['DEL_INDEX']); + $this->assertEquals($process->PRO_UID, $results['PRO_UID']); + } + + /** + * Check if return an empty participation information + * @test + */ + public function it_should_return_empty_participation_info() + { + // Try to get the participation information from a case that not exists + $results = Delegation::getParticipatedInfo(G::generateUniqueID()); + + // Check the information returned + $this->assertEmpty($results); + } } \ No newline at end of file diff --git a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php index d0aa34cb7..47c429ad2 100644 --- a/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php +++ b/workflow/engine/src/ProcessMaker/BusinessModel/Cases.php @@ -39,6 +39,7 @@ use ProcessMaker\BusinessModel\Task as BmTask; use ProcessMaker\BusinessModel\User as BmUser; use ProcessMaker\Core\System; use ProcessMaker\Exception\UploadException; +use ProcessMaker\Model\Delegation; use ProcessMaker\Plugins\PluginRegistry; use ProcessMaker\Services\OAuth2\Server; use ProcessMaker\Util\DateTime as UtilDateTime; @@ -2438,6 +2439,14 @@ class Cases * * @return array Return an array with status info Case, array empty otherwise * @throws Exception + * + * @see workflow/engine/methods/cases/main_init.php + * @see workflow/engine/methods/cases/opencase.php + * @see ProcessMaker\BusinessModel\Cases->setCaseVariables() + * @see ProcessMaker\BusinessModel\Cases\InputDocument->getCasesInputDocuments() + * @see ProcessMaker\BusinessModel\Cases\InputDocument->throwExceptionIfHaventPermissionToDelete() + * @see ProcessMaker\BusinessModel\Cases\OutputDocument->throwExceptionIfCaseNotIsInInbox() + * @see ProcessMaker\BusinessModel\Cases\OutputDocument->throwExceptionIfHaventPermissionToDelete() */ public function getStatusInfo($applicationUid, $delIndex = 0, $userUid = "") { @@ -2598,19 +2607,7 @@ class Cases } //Status is PARTICIPATED - $criteria2 = clone $criteria; - - $criteria2->setDistinct(); - $criteria2->clearSelectColumns(); - $criteria2->addSelectColumn($delimiter . 'PARTICIPATED' . $delimiter . ' AS APP_STATUS'); - $criteria2->addSelectColumn(AppDelegationPeer::DEL_INDEX); - $criteria2->addSelectColumn(ApplicationPeer::APP_UID); - $criteria2->addSelectColumn(ApplicationPeer::PRO_UID); - - $rsCriteria2 = ApplicationPeer::doSelectRS($criteria2); - $rsCriteria2->setFetchmode(ResultSet::FETCHMODE_ASSOC); - - $arrayData = $this->__getStatusInfoDataByRsCriteria($rsCriteria2); + $arrayData = Delegation::getParticipatedInfo($applicationUid); if (!empty($arrayData)) { return $arrayData; diff --git a/workflow/engine/src/ProcessMaker/Model/Delegation.php b/workflow/engine/src/ProcessMaker/Model/Delegation.php index 46ee58f7b..32bb0e0c3 100644 --- a/workflow/engine/src/ProcessMaker/Model/Delegation.php +++ b/workflow/engine/src/ProcessMaker/Model/Delegation.php @@ -45,6 +45,19 @@ class Delegation extends Model return $this->belongsTo(Process::class, 'PRO_ID', 'PRO_ID'); } + /** + * Scope a query to get the delegations from a case by APP_UID + * + * @param \Illuminate\Database\Eloquent\Builder $query + * @param string $appUid + * + * @return \Illuminate\Database\Eloquent\Builder + */ + public function scopeAppUid($query, $appUid) + { + return $query->where('APP_UID', '=', $appUid); + } + /** * Searches for delegations which match certain criteria * @@ -330,4 +343,50 @@ class Delegation extends Model return $response; } + /** + * Get participation information for a case + * + * @param string $appUid + * @return array + * + * @see ProcessMaker\BusinessModel\Cases:getStatusInfo() + */ + public static function getParticipatedInfo($appUid) + { + // Build the query + $query = Delegation::query()->select([ + 'APP_UID', + 'DEL_INDEX', + 'PRO_UID' + ]); + $query->appUid($appUid); + $query->orderBy('DEL_INDEX', 'ASC'); + + // Fetch results + $results = $query->get(); + + // Initialize the array to return + $arrayData = []; + + // If the collection have at least one item, build the main array to return + if ($results->count() > 0) { + // Get the first item + $first = $results->first(); + + // Build the main array to return + $arrayData = [ + 'APP_STATUS' => 'PARTICIPATED', // Value hardcoded because we need to return the same structure previously sent + 'DEL_INDEX' => [], // Initialize this item like an array + 'PRO_UID' => $first->PRO_UID + ]; + + // Populate the DEL_INDEX key with the values of the items collected + $results->each(function ($item) use (&$arrayData) { + $arrayData['DEL_INDEX'][] = $item->DEL_INDEX; + }); + } + + return $arrayData; + } + }