Questions and discussion about developing processes and programming in PHP, JavaScript, web services & REST API.
Forum rules: Please search to see if a question has already asked before creating a new topic. Please don't post the same question in multiple forums.
By Bonifac
#795292
Hello,

The Claim case REST endpoint doesn't work.
In my opinion the error is in the line 1005 of the Light.php. (processmaker/workflow/engine/src/ProcessMaker/BusinessModel/Light.php)
Code: Select all
$iDelIndex = $oCase->getCurrentDelegation( $sAppUid, $userUid );
The $userUid variable is the current authenticated user's id. Because this user hasn't been assigned yet to the case, the value of the $iDelIndex is incorrect.

I modified the function call.
Code: Select all
$iDelIndex = $oCase->getCurrentDelegation( $sAppUid );
David
User avatar
By amosbatto
#795310
You are right. Your code fixes the problem, but there is another problem as well. You could also have multiple parallel self service tasks in a case so Cases::getCurrentDelegation() wouldn't find the right delegation index. I think that we should add an optional $iDelIndex as a parameter to the function, so the delegation index can be specified if need be like this:
Code: Select all
    /**
     * Claim a case, i.e. assign a user to a self service task in a case which is currently unassigned.
     *
     * @param $userUid
     * @param $sAppUid
     * @param $iDelIndex
     * @throws \Exception
     */
    public function claimCaseUser($userUid, $sAppUid, $iDelIndex=null)
    {
        $response = array("status" => "fail");
        $oCase = new \Cases();
        if ($iDelIndex === null) {
            $iDelIndex = $oCase->getCurrentDelegation( $sAppUid );
        }

        $oAppDelegation = new \AppDelegation();
        $aDelegation = $oAppDelegation->load( $sAppUid, $iDelIndex );

        //if there are no user in the delegation row, this case is still in selfservice
        if ($aDelegation['USR_UID'] == "") {
            $oCase->setCatchUser( $sAppUid,$iDelIndex, $userUid );
            $response = array("status" => "ok");
        } else {
            //G::SendMessageText( G::LoadTranslation( 'ID_CASE_ALREADY_DERIVATED' ), 'error' );
        }
        return $response;
    }
This means that we would have to change the code for the REST endpoint to also pass the delegation index as an optional POST parameter.
User avatar
By amosbatto
#814963
higgledy,
Some endpoints require a particular permission related to their functionality, but there is no general permission to execute REST endpoints.
For example, the POST /user endpoint to create a user requires the PM_USERS permission.

The user setting up a new REST application needs the PM_REST_API_APPLICATIONS permission in his/her role, but that isn't needed to call a REST endpoint.
By higgledy
#814973
Thanks for the reply. I keep getting a 400 Bad Request when trying to hit claimCase. I cannot figure out why this is happening. I can successfully hit other ProcessMaker REST endpoints, just not this claimCase.

Is there anything I can double check in the extrarest plugin or in the ProcessMaker installation? Any places I can stick in a log statement to catch any exceptions, etc? Anything would help me.

BTW, I am not calling the Light version of claim case, but the version in the ExtraRest plugin Amos provided. I assume the code hits the method, postClaimCase, in Extra.php. Is this correct? How can I add log outputs in this method so that I can figure out what is not working? Or even if I am actually getting to that code or not? I am not PHP knowledgeable --please be explicit. Thanks.

Thanks.
User avatar
By amosbatto
#814977
higgledy,
What version of the plugin as you using?
What are the POST parameters that you are passing to the endpoint?

To debug, edit the code of the postClaimCase() function and start adding lines like:
Code: Select all
    public function postClaimCase($app_uid, $del_index = null, $usr_uid = null)
    {
        try {
            $loggedUserId = $this->getUserId();
            $oCase = new \Cases();
            
            if (empty($del_index)) {
               $del_index = $oCase->getCurrentDelegation($app_uid, '', true);
            }

            $oAppDel = new \AppDelegation();
            $aDelegation = $oAppDel->load($app_uid, $del_index);
            
            return $aDelegation; //for debug

            if ($aDelegation['USR_UID'] != '') {
				throw new \Exception("The task is already assigned to user with ID '{$aDelegation['USR_UID']}'."); 
			}
			
			if (empty($usr_uid) or $loggedUserId == $usr_uid) {
				$userIdToAssign = $loggedUserId;
			} 
			else {
				//check whether the user exists and has the PM_SUPERVISOR permission in role.
				$rbac = \RBAC::getSingleton();
				$rbac->initRBAC();
			
				if ($rbac->verifyUserId($usr_uid) != 1) {
					throw new \Exception("User with ID '$usr_uid' does not exist.");
				}
			
				if ($this->userCanAccess('PM_SUPERVISOR') == 0) {
					throw new \Exception("Logged-in user lacks the PM_SUPERVISOR permission in role.");
				}
				return "Got here"; //for debug
				//check if logged-in user is assigned as a process supervisor to the process
				$oSuper = new \ProcessMaker\BusinessModel\ProcessSupervisor();
				$aSupervisorList = $oSuper->getProcessSupervisors($aDelegation['PRO_UID'], 'ASSIGNED');
				
				if (!isset($aSupervisorList['data']) or !is_array($aSupervisorList['data'])) {
					throw new \Exception("Unable to retrieve list of supervisors for process.");
				}
				$isSuperForProcess = false;
				
				foreach ($aSupervisorList['data'] as $aSupervisorInfo) {
					if ($aSupervisorInfo['usr_uid'] == $loggedUserId) {
						$isSuperForProcess = true;
						break;
					}
				}
				
				if ($isSuperForProcess === false) {
					throw new \Exception("User '$loggedUserId' must be assigned as a Supervisor for process '".
					   $aDelegation['PRO_UID']."'.");
				}	   
				$userIdToAssign = $usr_uid;
			}

            return "User to assign: $userIdToAssign"; //for debug

            $oCase->setCatchUser($app_uid, $del_index, $userIdToAssign);
        } 
        catch (\Exception $e) {
            throw (new RestException(Api::STAT_APP_EXCEPTION, $e->getMessage()));
        }
    }
Add the lines that have //for debug in them.
You should see those values returned when you execute the endpoint. If you don't see them, then you know that that the error occurred at some point beforehand the //for debug line. Delete them one by one until you figure out where the problem is.
By higgledy
#815034
Thanks. I just got back to work from vacation.

I am sending the usr_uid, and app_uid. I ignore del_index field entirely.

How can I tell what version of the plugin I have?

Also, when I modify the php i.e. the postClaimCase(), do I need to restart the ProcessMaker server?
User avatar
By amosbatto
#815043
The version of the plugin in displayed by going to Admin > Plugins > Plugin Manager. The latest version of the extraRest plugin is 1.6.
PluginsManagerSeePluginVersion.png
PluginsManagerSeePluginVersion.png (43.44 KiB) Viewed 7905 times
Here is the latest version:
https://sourceforge.net/p/pmcommunity/c ... /extraRest

After you make changes to the plugin code, then inside ProcessMaker first disable the plugin and then reenable it.
By higgledy
#815054
I am able to hit this line
Code: Select all
return $aDelegation; //for debug
which is the very first return.
When I remove this line, I get caught in the first exception,
Code: Select all
if ($aDelegation['USR_UID'] != '') {
				throw new \Exception("The task is already assigned to user with ID '{$aDelegation['USR_UID']}'."); 
In the database table, APP_CACHE_VIEW, the case I am trying to claim has nothing in the USR_UID column. Is claimCase pulling the current assigned user from another table? Because I am thinking that my database is corrupted or lost it's relational integrity.

If I wanted to delete a case using sql, do you know all the tables I'd need to consider? Thanks.

Also, please reply because I have to work Saturday to get this finished. :(
User avatar
By amosbatto
#815056
I recommend deleting your current version of extraRest and installing version 1.6, then retry using the endpoint.

If you are trying to claim the task in a case, then its APP_CACHE_VIEW.USR_UID (which is copied from APP_DELEGATION.USR_UID) should be empty. If it isn't empty, then either you are using the wrong delegation index number (meaning that there is another unassigned task to claim in the case, so you need to specify the delegation index), or the task you want is already assigned to a user and you need to use the endpoint to reassign the task to another user.
Want to create your own meme coin?

In the world of cryptocurrencies, a unique and exc[…]

The market for cryptocurrencies is demonstrating a[…]

What's SAP FICO?

Embarking on a dissertation can be one of the most[…]

Hello. For rental housing, there are software solu[…]