danielgerlag / workflow-core

Lightweight workflow engine for .NET Standard
MIT License
5.33k stars 1.19k forks source link

Workflow-Core: How to implement decision step and invoke from api #1304

Open dbandyop005 opened 1 week ago

dbandyop005 commented 1 week ago

Scenario: A product is created. A notification is sent to approver for approval. If approved/rejected it updates the product status in db. Trying to achieve this using Workflow-Core in .net application.

//Below is the builder: public void Build(IWorkflowBuilder builder) { builder .StartWith < CreateProductStep > () .WaitFor("ApprovedProduct", (data, context) => data.ProductId) .Then < ApproveRejectStep > (); }

//These are the workflow steps: public class CreateProductStep : StepBody { public override ExecutionResult Run(IStepExecutionContext context) { var data = context.Workflow.Data as MyData;

    Console.WriteLine($"Product created {data.ProductId} {data.IsApproved}");
    return ExecutionResult.Next();
}

}

public class ApproveRejectStep : StepBody { public override ExecutionResult Run(IStepExecutionContext context) { var data = context.Workflow.Data as MyData;

    Console.WriteLine($"Product updated {data.ProductId} {data.IsApproved}");
    return ExecutionResult.Next();
}

} //The Controller:

[HttpPost("addproduct")] public async Task AddProduct([FromBody] MyData product) { var workflowId = await _workflowHost.StartWorkflow("ProductWorkflow1", product); return Ok(new { WorkflowId = workflowId, Message = "Product added and workflow started." }); } [HttpPost("approve-reject-product")] public async Task ApproveRejectProduct([FromBody] MyData product) { await _workflowHost.PublishEvent("ApprovedProduct", product.ProductId, product);

return Ok(new { Message = $"Product {product.IsApproved}" });

}

Expecting the WaitFor to get the context of the first step:CreateProductStep and pass it to the next step:ApproveRejectStep. Which is not happening. What am I missing here. How can I achieve passing the context of the previous flow to the next waitfor step and invoke via api.