github.com/sharovik/devbot@v1.0.1-0.20240308094637-4a0387c40516/internal/service/history/events_history_service.go (about)

     1  package history
     2  
     3  import (
     4  	"strings"
     5  
     6  	"github.com/sharovik/devbot/internal/service/message/conversation"
     7  	_time "github.com/sharovik/devbot/internal/service/time"
     8  
     9  	"github.com/sharovik/devbot/internal/container"
    10  	"github.com/sharovik/devbot/internal/dto"
    11  	"github.com/sharovik/devbot/internal/dto/databasedto"
    12  	"github.com/sharovik/devbot/internal/log"
    13  	"github.com/sharovik/orm/clients"
    14  	cdto "github.com/sharovik/orm/dto"
    15  )
    16  
    17  const (
    18  	variablesSeparator = ";"
    19  	ignoredRepeatEvent = "repeatevent"
    20  )
    21  
    22  // RememberEventExecution method for store the history of the event execution
    23  func RememberEventExecution(msg dto.BaseChatMessage) {
    24  	if msg.DictionaryMessage.ReactionType == ignoredRepeatEvent {
    25  		return
    26  	}
    27  
    28  	command := msg.OriginalMessage.Text
    29  	if conversation.GetConversation(msg.Channel).Question != "" {
    30  		command = conversation.GetConversation(msg.Channel).Question
    31  	}
    32  
    33  	var variables []string
    34  	for _, variable := range conversation.GetConversation(msg.Channel).Scenario.RequiredVariables {
    35  		variables = append(variables, variable.Value)
    36  	}
    37  
    38  	item := databasedto.EventTriggerHistoryModel
    39  	item.RemoveModelField("id")
    40  	item.AddModelField(cdto.ModelField{
    41  		Name:  "event_id",
    42  		Value: msg.DictionaryMessage.EventID,
    43  	})
    44  	item.AddModelField(cdto.ModelField{
    45  		Name:  "scenario_id",
    46  		Value: msg.DictionaryMessage.ScenarioID,
    47  	})
    48  	item.AddModelField(cdto.ModelField{
    49  		Name:  "user",
    50  		Value: msg.OriginalMessage.User,
    51  	})
    52  	item.AddModelField(cdto.ModelField{
    53  		Name:  "channel",
    54  		Value: msg.Channel,
    55  	})
    56  	item.AddModelField(cdto.ModelField{
    57  		Name:  "command",
    58  		Value: command,
    59  	})
    60  	item.AddModelField(cdto.ModelField{
    61  		Name:  "variables",
    62  		Value: strings.Join(variables, variablesSeparator),
    63  	})
    64  	item.AddModelField(cdto.ModelField{
    65  		Name:  "last_question_id",
    66  		Value: conversation.GetConversation(msg.Channel).LastQuestion.DictionaryMessage.QuestionID,
    67  	})
    68  	item.AddModelField(cdto.ModelField{
    69  		Name:  "created",
    70  		Value: _time.Service.Now().Unix(),
    71  	})
    72  
    73  	c := container.C.Dictionary.GetDBClient()
    74  
    75  	if _, err := c.Execute(new(clients.Query).Insert(item)); err != nil {
    76  		log.Logger().AddError(err).Msg("Failed to insert a log entry into events history table")
    77  	}
    78  }