Skip to content

Commit

Permalink
implement workflow response handling
Browse files Browse the repository at this point in the history
  • Loading branch information
MuhammadUmer44 committed Nov 21, 2024
1 parent 6d75668 commit 9446c07
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 22 deletions.
18 changes: 3 additions & 15 deletions handlers/workflow.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,11 +55,8 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http
}

var response struct {
RequestID string `json:"request_id"`
Status db.WfRequestStatus `json:"status"`
ResponseData db.PropertyMap `json:"response_data"`
Action string `json:"action,omitempty"`
WorkflowID string `json:"workflow_id,omitempty"`
RequestID string `json:"request_id"`
ResponseData db.PropertyMap `json:"response_data"`
}

if err := json.Unmarshal(body, &response); err != nil {
Expand All @@ -72,14 +69,6 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http
return
}

switch response.Status {
case db.StatusNew, db.StatusPending, db.StatusCompleted, db.StatusFailed:

default:
http.Error(w, "Invalid status value", http.StatusBadRequest)
return
}

request, err := wh.db.GetWorkflowRequest(response.RequestID)
if err != nil {
http.Error(w, "Failed to retrieve original request", http.StatusInternalServerError)
Expand All @@ -96,8 +85,7 @@ func (wh *workflowHandler) HandleWorkflowResponse(w http.ResponseWriter, r *http
return
}

status := response.Status

status := db.StatusCompleted
if processingMap != nil && processingMap.RequiresProcessing {
status = db.StatusPending
}
Expand Down
21 changes: 14 additions & 7 deletions handlers/workflow_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -205,8 +205,7 @@ func TestHandleWorkflowResponse(t *testing.T) {
assert.Equal(t, http.StatusOK, w.Code)
})

t.Run("should handle invalid status value", func(t *testing.T) {

t.Run("should maintain NEW status when processing map exists", func(t *testing.T) {
requestData := db.PropertyMap{
"test_key": "test_value",
}
Expand All @@ -222,13 +221,20 @@ func TestHandleWorkflowResponse(t *testing.T) {
err := db.TestDB.CreateWorkflowRequest(workflowRequest)
assert.NoError(t, err)

processingMap := &db.WfProcessingMap{
Type: workflowRequest.Source,
ProcessKey: workflowRequest.Action,
RequiresProcessing: true,
HandlerFunc: "test_handler",
}
err = db.TestDB.CreateProcessingMap(processingMap)
assert.NoError(t, err)

response := struct {
RequestID string `json:"request_id"`
Status string `json:"status"`
ResponseData db.PropertyMap `json:"response_data"`
}{
RequestID: workflowRequest.RequestID,
Status: "INVALID_STATUS",
ResponseData: db.PropertyMap{"result": "success"},
}
payload, _ := json.Marshal(response)
Expand All @@ -238,11 +244,12 @@ func TestHandleWorkflowResponse(t *testing.T) {

wh.HandleWorkflowResponse(w, req)

assert.Equal(t, http.StatusBadRequest, w.Code)
assert.Equal(t, http.StatusOK, w.Code)

// Verify the request status hasn't changed
// Verify the request status is set to PENDING due to processing map
updatedReq, err := db.TestDB.GetWorkflowRequest(workflowRequest.RequestID)
assert.NoError(t, err)
assert.Equal(t, db.StatusNew, updatedReq.Status)
assert.Equal(t, db.StatusPending, updatedReq.Status)
assert.Equal(t, response.ResponseData, updatedReq.ResponseData)
})
}

0 comments on commit 9446c07

Please sign in to comment.