121 lines
4.2 KiB
Go
121 lines
4.2 KiB
Go
package action
|
|
|
|
import (
|
|
"fmt"
|
|
"houston/internal/processor/action/view"
|
|
"houston/pkg/postgres/service/incident"
|
|
"strconv"
|
|
"time"
|
|
|
|
"github.com/slack-go/slack"
|
|
"github.com/slack-go/slack/socketmode"
|
|
"go.uber.org/zap"
|
|
)
|
|
|
|
type UpdateIncidentAction struct {
|
|
client *socketmode.Client
|
|
logger *zap.Logger
|
|
incidentService *incident.Service
|
|
}
|
|
|
|
func NewIncidentUpdateAction(client *socketmode.Client, logger *zap.Logger, incidentService *incident.Service) *UpdateIncidentAction {
|
|
return &UpdateIncidentAction{
|
|
client: client,
|
|
logger: logger,
|
|
incidentService: incidentService,
|
|
}
|
|
}
|
|
|
|
func (isp *UpdateIncidentAction) IncidentUpdateStatusRequestProcess(callback slack.InteractionCallback, request *socketmode.Request) {
|
|
incidentStatuses, err := isp.incidentService.FetchAllIncidentStatuses()
|
|
if err != nil || incidentStatuses == nil {
|
|
isp.logger.Error("failed to get the all active incident statuses")
|
|
return
|
|
}
|
|
|
|
modalRequest := view.BuildIncidentUpdateStatusModal(*incidentStatuses, callback.Channel)
|
|
|
|
_, err = isp.client.OpenView(callback.TriggerID, modalRequest)
|
|
if err != nil {
|
|
isp.logger.Error("houston slackbot openview command failed.",
|
|
zap.String("trigger_id", callback.TriggerID), zap.String("channel_id", callback.Channel.ID), zap.Error(err))
|
|
return
|
|
}
|
|
var payload interface{}
|
|
isp.client.Ack(*request, payload)
|
|
}
|
|
|
|
func (isp *UpdateIncidentAction) IncidentUpdateStatus(callback slack.InteractionCallback, request *socketmode.Request, channel slack.Channel, user slack.User) {
|
|
incidentEntity, err := isp.incidentService.FindIncidentByChannelId(callback.View.PrivateMetadata)
|
|
if err != nil {
|
|
isp.logger.Error("FindIncidentBySlackChannelId error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
} else if incidentEntity == nil {
|
|
isp.logger.Error("IncidentEntity Object Not Found",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
|
|
}
|
|
|
|
incidentStatusId := buildUpdateIncidentStatusRequest(isp.logger, callback.View.State.Values)
|
|
result, err := isp.incidentService.FindIncidentStatusById(incidentStatusId)
|
|
if err != nil {
|
|
isp.logger.Error("FindIncidentStatusById error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
} else if result == nil {
|
|
isp.logger.Error("IncidentStatusEntity Object not found",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
return
|
|
}
|
|
|
|
incidentEntity.Status = result.ID
|
|
incidentEntity.UpdatedBy = user.ID
|
|
if result.IsTerminalStatus {
|
|
now := time.Now()
|
|
incidentEntity.EndTime = &now
|
|
}
|
|
err = isp.incidentService.UpdateIncident(incidentEntity)
|
|
if err != nil {
|
|
isp.logger.Error("UpdateIncident error",
|
|
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
|
|
zap.String("user_id", user.ID), zap.Error(err))
|
|
}
|
|
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> > set status to %s", user.ID, result.Name), false)
|
|
_, _, errMessage := isp.client.PostMessage(callback.View.PrivateMetadata, msgOption)
|
|
if errMessage != nil {
|
|
isp.logger.Error("post response failed for IncidentUpdateStatus", zap.Error(errMessage))
|
|
return
|
|
}
|
|
|
|
if result.IsTerminalStatus {
|
|
isp.client.ArchiveConversation(callback.View.PrivateMetadata)
|
|
}
|
|
|
|
var payload interface{}
|
|
isp.client.Ack(*request, payload)
|
|
}
|
|
|
|
func buildUpdateIncidentStatusRequest(logger *zap.Logger, blockActions map[string]map[string]slack.BlockAction) uint {
|
|
var requestMap = make(map[string]string, 0)
|
|
for _, actions := range blockActions {
|
|
for actionID, a := range actions {
|
|
if a.Type == "static_select" {
|
|
requestMap[actionID] = a.SelectedOption.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
selectedValue := requestMap["incident_status_modal_request"]
|
|
selectedValueInInt, err := strconv.Atoi(selectedValue)
|
|
if err != nil {
|
|
logger.Error("String conversion to int faileed in buildUpdateIncidentTypeRequest for "+selectedValue, zap.Error(err))
|
|
}
|
|
return uint(selectedValueInInt)
|
|
}
|