remiges-tech / crux

Crux is a business rules engine and workflow engine
0 stars 6 forks source link

WSC: WFinstanceNew() #44

Open remiges-sachin opened 8 months ago

remiges-sachin commented 8 months ago

This creates a new instance of a workflow. This is called when one entity (i.e. one object instance) begins its journey through a workflow.

Request

{
    "slice": 7,
    "entityid": "0eb8da50-aece-11ee-b168-3b192f7cd2b6",
    "entity": {
        "class": "salesvoucher",
        "voucherdate": "2024-05-20T00:00:00Z",
        "branch": "belampally",
        "voucheramt": "84213"
    },
    "app": "retailsales",
    "workflow": "customeronboarding",
    "trace": 0,
    "parent": 952
}

where:

Processing

Any authenticated user may make this call, from one of the permitted IP addresses.

The call will verify

If any of these conditions fail, the call will return with a suitable error immediately. Else it will add two attributes to the entity object:

"step": "START",
"stepfailed": false

Then it will check whether all the rulesets and schema belonging to the slice specified in the request are loaded into the in-memory cache. If they are not present, the call will load them.

Then it will call doMatch() with:

When doMatch() returns, the call will check whether doMatch() has returned with a critical error. If yes, then it sends an error response to the client with the appropriate details. If there is no critical error, then it will inspect the tasks and properties of the actionset returned. The properties object of actionset must always contain either an attribute called done or an attribute called nextstep. If neither is present, or if a third attribute is present, then the call must log a critical error and return this error in its response. If all this is ok, then it will insert records in the wfinstance table as per the following scheme:

New records in the wfinstance table will have the following values

It will then look up the stepworkflow table to see if any of the task values obtained from actionset.properties.tasks are specified in stepworkflow. (There could be multiple values in the tasks set.) The stepworkflow table specifies if one of the steps, say S1, returned by workflow X is itself to be executed by traversing another multi-step workflow, e.g. workflow Y. In that case, the name Y is specified in stepworkflow against step S1. If this is the case, then this information needs to be returned to the caller.

Response

If all goes well, and it is a simple case where there is just one next step to be executed, the response will be:

{
    "tasks": [ {"passportchk": 8734} ],
    "loggedat": "2025-09-04T19:05:34Z",
    "subflows": {
        "passportchk": "dopassportworkflow"
    },
    "tracedata": "{...}"
}

where everything is mandatory other than subflows, done and tracedata. The tasks array will have a set of objects, one per next step. In this example, there is only one next step, therefore, there is just one object. The name of the attribute of that object is passportchk, which is the name of the step to execute, and the value is 8734, which is the ID of the row in wfinstances corresponding to this entry. Trace data will only be present, in the form of a large JSON block, if it has been requested.

It is possible that the call returns with the indication that the workflow traversal has been completed. In this case, the response will only contain:

{
    "done": true
}

This parameter informs the caller that this entity has reached the last step in its workflow and there is nothing more to be done. It is rare in a real system for WFinstanceNew() to get a response of "done": true, since this call is just the start of a workflow traversal. In cases when done = true is being returned, tracedata may optionally be returned too, to indicate the matching engine's process to arrive at this conclusion.

If the workflow has thrown up a set of concurrent steps which need to be performed, the response will be of the form:

{
    "tasks": [
        {"passportchk": 8734},
        {"degreechk": 8543},
        {"updategeneralregister": 8611}
    ],
    "nextstep": "step2done",
    "loggedat": "2025-09-04T19:05:34Z",
    "subflows": {
        "passportchk": "dopassportworkflow"
    }
}

where everything will be mandatory and present, other than subflows, done and tracedata. The nextstep member will not be present when the tasks array has just one next step, and will always be present if tasks returns multiple steps. The tasks array has an ID against each next step.

The subflows object here is shown specifying the workflow name to call to execute the step called passportchk. The subflows block will only be present if there is a record for passportchk in the stepworkflow table. This attribute has no connection with whether tasks specifies one or multiple next steps.

It is expected that the application code will take note of this sub-workflow indication and will trigger a separate workflow for this entity for dopassportworkflow by making a separate call to WFinstanceNew() and using the same instance ID which it had used before.

remiges-kanchan commented 7 months ago

validation done for workflow

remiges-kanchan commented 7 months ago