github.com/mdaxf/iac@v0.0.0-20240519030858-58a061660378/controllers/workflow/workflow.go (about)

     1  package workflow
     2  
     3  import (
     4  	"fmt"
     5  	"time"
     6  
     7  	//"log"
     8  	"net/http"
     9  
    10  	"github.com/gin-gonic/gin"
    11  
    12  	"github.com/mdaxf/iac/documents"
    13  	"github.com/mdaxf/iac/logger"
    14  
    15  	"github.com/mdaxf/iac/controllers/common"
    16  	"github.com/mdaxf/iac/workflow"
    17  )
    18  
    19  type WorkFlowController struct {
    20  }
    21  
    22  func (wf *WorkFlowController) GetWorkFlowbyUUID(ctx *gin.Context) {
    23  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
    24  
    25  	startTime := time.Now()
    26  	defer func() {
    27  		elapsed := time.Since(startTime)
    28  		iLog.PerformanceWithDuration("GetTasksbyUser.workflow.ExplodeWorkFlow", elapsed)
    29  	}()
    30  
    31  	defer func() {
    32  		err := recover()
    33  		if err != nil {
    34  			iLog.Error(fmt.Sprintf("Error: %v", err))
    35  			ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
    36  		}
    37  	}()
    38  
    39  	requestobj, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
    40  	if err != nil {
    41  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
    42  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    43  		return
    44  	}
    45  
    46  	iLog.ClientID = clientid
    47  	iLog.User = user
    48  	data := make(map[string]interface{})
    49  	data = requestobj["data"].(map[string]interface{})
    50  	WorkFlowUUID := data["uuid"].(string)
    51  	if WorkFlowUUID == "" {
    52  		iLog.Error(fmt.Sprintf("failed to create the notification: %v", err))
    53  		ctx.JSON(http.StatusBadRequest, gin.H{"error": "WorkFlowUUID is required"})
    54  		return
    55  	}
    56  
    57  	//_, WorkFlow, err := workflow.GetWorkFlowbyUUID(WorkFlowUUID, user, *documents.DocDBCon)
    58  	WorkFlow, err := documents.DocDBCon.GetItembyUUID("WorkFlow", WorkFlowUUID)
    59  	//err = json.Unmarshal(WorkFlowSchema, &WorkFlow)
    60  	if err != nil {
    61  		iLog.Error(fmt.Sprintf("Error in getting workflow schema: %s", err))
    62  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    63  		return
    64  	}
    65  
    66  	ctx.JSON(http.StatusOK, gin.H{"data": WorkFlow})
    67  }
    68  
    69  func (wf *WorkFlowController) ExplodeWorkFlow(ctx *gin.Context) {
    70  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
    71  
    72  	startTime := time.Now()
    73  	defer func() {
    74  		elapsed := time.Since(startTime)
    75  		iLog.PerformanceWithDuration("GetTasksbyUser.workflow.ExplodeWorkFlow", elapsed)
    76  	}()
    77  
    78  	defer func() {
    79  		err := recover()
    80  		if err != nil {
    81  			iLog.Error(fmt.Sprintf("Error: %v", err))
    82  			ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
    83  		}
    84  	}()
    85  
    86  	requestobj, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
    87  	if err != nil {
    88  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
    89  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    90  		return
    91  	}
    92  	iLog.ClientID = clientid
    93  	iLog.User = user
    94  	data := make(map[string]interface{})
    95  	data = requestobj["data"].(map[string]interface{})
    96  	WorkFlowName := data["WorkFlowName"].(string)
    97  	EntityName := data["EntityName"].(string)
    98  	EntityType := data["EntityType"].(string)
    99  	Description := data["Description"].(string)
   100  
   101  	if WorkFlowName == "" || EntityName == "" {
   102  		iLog.Error(fmt.Sprintf("failed to create the notification: %v", err))
   103  		ctx.JSON(http.StatusBadRequest, gin.H{"error": "WorkFlowName, EntityName are required"})
   104  		return
   105  	}
   106  
   107  	wfe := workflow.NewExplosion(WorkFlowName, EntityName, EntityType, user, clientid)
   108  	wfentityid, err := wfe.Explode(Description, data)
   109  
   110  	if err != nil {
   111  
   112  		iLog.Error(fmt.Sprintf("failed to create the notification: %v", err))
   113  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   114  		return
   115  	}
   116  
   117  	ctx.JSON(http.StatusOK, gin.H{"data": wfentityid})
   118  }
   119  
   120  func (wf *WorkFlowController) GetTasksbyUser(ctx *gin.Context) {
   121  
   122  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   123  
   124  	startTime := time.Now()
   125  	defer func() {
   126  		elapsed := time.Since(startTime)
   127  		iLog.PerformanceWithDuration("WorkFlowController.workflow.GetTasksbyUser", elapsed)
   128  	}()
   129  
   130  	/*	defer func() {
   131  			err := recover()
   132  			if err != nil {
   133  				iLog.Error(fmt.Sprintf("Error: %v", err))
   134  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   135  			}
   136  		}()
   137  	*/
   138  	_, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   139  	if err != nil {
   140  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   141  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   142  		return
   143  	}
   144  	iLog.ClientID = clientid
   145  	iLog.User = user
   146  
   147  	result, err := workflow.GetTasksbyUser(user)
   148  
   149  	if err != nil {
   150  
   151  		iLog.Error(fmt.Sprintf("failed to get the tasks for the user %s with error: %v", user, err))
   152  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   153  		return
   154  	}
   155  
   156  	ctx.JSON(http.StatusOK, gin.H{"data": result})
   157  
   158  }
   159  
   160  func (wf *WorkFlowController) GetWorkFlowTasks(ctx *gin.Context) {
   161  
   162  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   163  
   164  	startTime := time.Now()
   165  	defer func() {
   166  		elapsed := time.Since(startTime)
   167  		iLog.PerformanceWithDuration("WorkFlowController.workflow.GetWorkFlowTasks", elapsed)
   168  	}()
   169  
   170  	/*	defer func() {
   171  			err := recover()
   172  			if err != nil {
   173  				iLog.Error(fmt.Sprintf("Error: %v", err))
   174  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   175  			}
   176  		}()
   177  	*/
   178  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   179  	if err != nil {
   180  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   181  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   182  		return
   183  	}
   184  	iLog.ClientID = clientid
   185  	iLog.User = user
   186  	data := make(map[string]interface{})
   187  	data = requestbody["data"].(map[string]interface{})
   188  	workflowentityid := int64(data["workflowentityid"].(float64))
   189  
   190  	result, err := workflow.GetWorkFlowTasks(workflowentityid, user)
   191  
   192  	if err != nil {
   193  
   194  		iLog.Error(fmt.Sprintf("failed to get the tasks for the user %s with error: %v", user, err))
   195  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   196  		return
   197  	}
   198  
   199  	ctx.JSON(http.StatusOK, gin.H{"data": result})
   200  
   201  }
   202  
   203  func (wf *WorkFlowController) StartTask(ctx *gin.Context) {
   204  
   205  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   206  
   207  	startTime := time.Now()
   208  	defer func() {
   209  		elapsed := time.Since(startTime)
   210  		iLog.PerformanceWithDuration("WorkFlowController.workflow.StartTask", elapsed)
   211  	}()
   212  
   213  	/*	defer func() {
   214  			err := recover()
   215  			if err != nil {
   216  				iLog.Error(fmt.Sprintf("Error: %v", err))
   217  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   218  			}
   219  		}()
   220  	*/
   221  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   222  	if err != nil {
   223  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   224  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   225  		return
   226  	}
   227  	iLog.ClientID = clientid
   228  	iLog.User = user
   229  	data := make(map[string]interface{})
   230  	data = requestbody["data"].(map[string]interface{})
   231  	taskid := int64(data["taskid"].(float64))
   232  	wft := workflow.NewWorkFlowTaskType(taskid, user)
   233  	err = wft.StartTask()
   234  
   235  	if err != nil {
   236  
   237  		iLog.Error(fmt.Sprintf("failed to start the tasks for the user %s with error: %v", user, err))
   238  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   239  		return
   240  	}
   241  
   242  	ctx.JSON(http.StatusOK, gin.H{"data": "OK"})
   243  
   244  }
   245  func (wf *WorkFlowController) ExecuteTaskTranCode(ctx *gin.Context) {
   246  
   247  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   248  
   249  	startTime := time.Now()
   250  	defer func() {
   251  		elapsed := time.Since(startTime)
   252  		iLog.PerformanceWithDuration("WorkFlowController.workflow.CompleteTask", elapsed)
   253  	}()
   254  
   255  	/*	defer func() {
   256  			err := recover()
   257  			if err != nil {
   258  				iLog.Error(fmt.Sprintf("Error: %v", err))
   259  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   260  			}
   261  		}()
   262  	*/
   263  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   264  	if err != nil {
   265  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   266  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   267  		return
   268  	}
   269  	iLog.ClientID = clientid
   270  	iLog.User = user
   271  	data := make(map[string]interface{})
   272  	data = requestbody["data"].(map[string]interface{})
   273  	taskid := int64(data["taskid"].(float64))
   274  	trancode := data["trancode"].(string)
   275  	wft := workflow.NewWorkFlowTaskType(taskid, user)
   276  	err = wft.ExecuteTaskTranCode(trancode)
   277  
   278  	if err != nil {
   279  
   280  		iLog.Error(fmt.Sprintf("failed to execute the task's trancode for the user %s with error: %v", user, err))
   281  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   282  		return
   283  	}
   284  
   285  	ctx.JSON(http.StatusOK, gin.H{"data": "OK"})
   286  
   287  }
   288  
   289  func (wf *WorkFlowController) UpdateProcessDataAndComplete(ctx *gin.Context) {
   290  
   291  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   292  
   293  	startTime := time.Now()
   294  	defer func() {
   295  		elapsed := time.Since(startTime)
   296  		iLog.PerformanceWithDuration("WorkFlowController.workflow.UpdatePDAndComplete", elapsed)
   297  	}()
   298  
   299  	/*	defer func() {
   300  			err := recover()
   301  			if err != nil {
   302  				iLog.Error(fmt.Sprintf("Error: %v", err))
   303  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   304  			}
   305  		}()
   306  	*/
   307  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   308  	if err != nil {
   309  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   310  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   311  		return
   312  	}
   313  	iLog.ClientID = clientid
   314  	iLog.User = user
   315  	data := make(map[string]interface{})
   316  	data = requestbody["data"].(map[string]interface{})
   317  	taskid := int64(data["taskid"].(float64))
   318  	trancode := data["trancode"].(string)
   319  	processdata := data["processdata"].(map[string]interface{})
   320  
   321  	if taskid == 0 {
   322  		iLog.Error("Does not get the taskid correctly")
   323  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   324  		return
   325  	}
   326  
   327  	wft := workflow.NewWorkFlowTaskType(taskid, user)
   328  
   329  	err = wft.UpdateProcessData(processdata)
   330  
   331  	if err != nil {
   332  		iLog.Error("There is an error when updating process data!")
   333  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   334  		return
   335  	}
   336  
   337  	if trancode != "" {
   338  		_, err = workflow.ExecuteTaskTranCode(taskid, trancode, processdata, nil, nil, user)
   339  		//err = wft.ExecuteTaskTranCode(trancode)
   340  
   341  		if err != nil {
   342  
   343  			iLog.Error(fmt.Sprintf("failed to execute the task's trancode for the user %s with error: %v", user, err))
   344  			ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   345  			return
   346  		}
   347  	}
   348  	err = wft.CompleteTask()
   349  	if err != nil {
   350  
   351  		iLog.Error(fmt.Sprintf("failed to complete the task for the user %s with error: %v", user, err))
   352  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   353  		return
   354  	}
   355  
   356  	ctx.JSON(http.StatusOK, gin.H{"data": "OK"})
   357  
   358  }
   359  
   360  func (wf *WorkFlowController) ExecuteTaskTranCodeAndComplete(ctx *gin.Context) {
   361  
   362  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   363  
   364  	startTime := time.Now()
   365  	defer func() {
   366  		elapsed := time.Since(startTime)
   367  		iLog.PerformanceWithDuration("WorkFlowController.workflow.CompleteTask", elapsed)
   368  	}()
   369  
   370  	/*	defer func() {
   371  			err := recover()
   372  			if err != nil {
   373  				iLog.Error(fmt.Sprintf("Error: %v", err))
   374  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   375  			}
   376  		}()
   377  	*/
   378  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   379  	if err != nil {
   380  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   381  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   382  		return
   383  	}
   384  	iLog.ClientID = clientid
   385  	iLog.User = user
   386  	data := make(map[string]interface{})
   387  	data = requestbody["data"].(map[string]interface{})
   388  	taskid := int64(data["taskid"].(float64))
   389  	trancode := data["trancode"].(string)
   390  
   391  	if taskid == 0 {
   392  		iLog.Error("Does not get the taskid correctly")
   393  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   394  		return
   395  	}
   396  
   397  	if trancode == "" {
   398  		iLog.Error("Does not get the trancode correctly")
   399  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   400  		return
   401  	}
   402  
   403  	wft := workflow.NewWorkFlowTaskType(taskid, user)
   404  	err = wft.ExecuteTaskTranCode(trancode)
   405  
   406  	if err != nil {
   407  
   408  		iLog.Error(fmt.Sprintf("failed to execute the task's trancode for the user %s with error: %v", user, err))
   409  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   410  		return
   411  	}
   412  
   413  	err = wft.CompleteTask()
   414  	if err != nil {
   415  
   416  		iLog.Error(fmt.Sprintf("failed to complete the task for the user %s with error: %v", user, err))
   417  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   418  		return
   419  	}
   420  
   421  	ctx.JSON(http.StatusOK, gin.H{"data": "OK"})
   422  
   423  }
   424  
   425  func (wf *WorkFlowController) CompleteTask(ctx *gin.Context) {
   426  
   427  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   428  
   429  	startTime := time.Now()
   430  	defer func() {
   431  		elapsed := time.Since(startTime)
   432  		iLog.PerformanceWithDuration("WorkFlowController.workflow.CompleteTask", elapsed)
   433  	}()
   434  
   435  	/*	defer func() {
   436  			err := recover()
   437  			if err != nil {
   438  				iLog.Error(fmt.Sprintf("Error: %v", err))
   439  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   440  			}
   441  		}()
   442  	*/
   443  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   444  	if err != nil {
   445  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   446  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   447  		return
   448  	}
   449  	iLog.ClientID = clientid
   450  	iLog.User = user
   451  	data := make(map[string]interface{})
   452  	data = requestbody["data"].(map[string]interface{})
   453  	taskid := int64(data["taskid"].(float64))
   454  	wft := workflow.NewWorkFlowTaskType(taskid, user)
   455  	err = wft.CompleteTask()
   456  
   457  	if err != nil {
   458  
   459  		iLog.Error(fmt.Sprintf("failed to complete the tasks for the user %s with error: %v", user, err))
   460  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   461  		return
   462  	}
   463  
   464  	ctx.JSON(http.StatusOK, gin.H{"data": "OK"})
   465  
   466  }
   467  
   468  func (wf *WorkFlowController) GetPreTaskData(ctx *gin.Context) {
   469  
   470  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "workflow"}
   471  
   472  	startTime := time.Now()
   473  	defer func() {
   474  		elapsed := time.Since(startTime)
   475  		iLog.PerformanceWithDuration("WorkFlowController.workflow.GetPreTaskData", elapsed)
   476  	}()
   477  
   478  	/*	defer func() {
   479  			err := recover()
   480  			if err != nil {
   481  				iLog.Error(fmt.Sprintf("Error: %v", err))
   482  				ctx.JSON(http.StatusBadRequest, gin.H{"error": err})
   483  			}
   484  		}()
   485  	*/
   486  	requestbody, clientid, user, err := common.GetRequestBodyandUserbyJson(ctx)
   487  	if err != nil {
   488  		iLog.Error(fmt.Sprintf("Get request information Error: %v", err))
   489  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   490  		return
   491  	}
   492  	iLog.ClientID = clientid
   493  	iLog.User = user
   494  	data := make(map[string]interface{})
   495  	data = requestbody["data"].(map[string]interface{})
   496  	taskid := int64(data["taskid"].(float64))
   497  
   498  	pretaskdata, err := workflow.GetTaskPreTaskData(taskid, user)
   499  
   500  	if err != nil {
   501  
   502  		iLog.Error(fmt.Sprintf("failed to get the pretask datafor the task %d with error: %v", taskid, err))
   503  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
   504  		return
   505  	}
   506  	iLog.Debug(fmt.Sprintf("the pretaskdata: %v", pretaskdata))
   507  
   508  	pretaskid := ""
   509  	if pretaskdata["workflow_taskid"] != nil {
   510  		pretaskid = pretaskdata["workflow_taskid"].(string)
   511  	}
   512  
   513  	pretaskdata["workflow_taskid"] = taskid
   514  	pretaskdata["workflow_pretaskid"] = pretaskid
   515  
   516  	ctx.JSON(http.StatusOK, gin.H{"data": pretaskdata})
   517  
   518  }