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

     1  package bpmcontroller
     2  
     3  import (
     4  	"encoding/json"
     5  	"fmt"
     6  	"time"
     7  
     8  	//"log"
     9  	"net/http"
    10  
    11  	"github.com/gin-gonic/gin"
    12  
    13  	"github.com/mdaxf/iac/controllers/common"
    14  
    15  	"github.com/mdaxf/iac/engine/trancode"
    16  
    17  	"github.com/mdaxf/iac/logger"
    18  )
    19  
    20  type BPMController struct {
    21  	Name    string                 `json:"name"`
    22  	Version string                 `json:"version"`
    23  	Inputs  map[string]interface{} `json:"inputs"`
    24  	Mode    int                    `json:"mode"`
    25  }
    26  
    27  func (b *BPMController) ExecuteBPM(ctx *gin.Context) {
    28  	iLog := logger.Log{ModuleName: logger.API, User: "System", ControllerName: "ExecuteBPM"}
    29  
    30  	startTime := time.Now()
    31  	defer func() {
    32  		elapsed := time.Since(startTime)
    33  		iLog.PerformanceWithDuration("BPMController.ExecuteBPM", elapsed)
    34  	}()
    35  
    36  	body, clientid, user, err := common.GetRequestBodyandUser(ctx)
    37  	if err != nil {
    38  		iLog.Error(fmt.Sprintf("Error reading body: %v", err))
    39  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    40  		return
    41  	}
    42  	iLog.ClientID = clientid
    43  	iLog.User = user
    44  
    45  	var bpm BPMController
    46  	err = json.Unmarshal(body, &bpm)
    47  	if err != nil {
    48  		iLog.Error(fmt.Sprintf("Error unmarshalling body: %v", err))
    49  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    50  		return
    51  	}
    52  
    53  	iLog.Info(fmt.Sprintf("BPMController.ExecuteBPM: %v", bpm))
    54  
    55  	if bpm.Name == "" {
    56  		iLog.Error("BPM Name is required")
    57  		ctx.JSON(http.StatusBadRequest, gin.H{"error": "BPM Name is required"})
    58  		return
    59  	}
    60  
    61  	systemsessions := make(map[string]interface{})
    62  	systemsessions["UserNo"] = user
    63  	systemsessions["ClientID"] = clientid
    64  
    65  	outputs, err := trancode.Execute(bpm.Name, bpm.Inputs, systemsessions)
    66  
    67  	if err != nil {
    68  		iLog.Error(fmt.Sprintf("Error executing BPM: %v", err))
    69  		ctx.JSON(http.StatusBadRequest, gin.H{"error": err.Error()})
    70  		return
    71  	}
    72  
    73  	iLog.Debug(fmt.Sprintf("BPMController.ExecuteBPM: %v", outputs))
    74  	ctx.JSON(http.StatusOK, gin.H{"data": outputs})
    75  
    76  }