Files
houston-be/pkg/slack/houston/command/incident_update_severity.go
Shubham Kirve b974cb6bf3 TP-0000 | Initialize houston repo (#1)
* TP-0000 | intialize houston repo

* TP-0000 | intialize houston repo
2023-03-29 00:01:17 +05:30

126 lines
4.7 KiB
Go

package command
import (
"fmt"
"houston/pkg/postgres/query"
"houston/pkg/slack/common"
houston "houston/pkg/slack/houston/design"
"strconv"
"time"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
"gorm.io/gorm"
)
type incidentUpdateSevertityProcessor struct {
client *socketmode.Client
db *gorm.DB
logger *zap.Logger
}
func NewIncidentUpdateSeverityProcessor(client *socketmode.Client, db *gorm.DB, logger *zap.Logger) *incidentUpdateSevertityProcessor {
return &incidentUpdateSevertityProcessor{
client: client,
db: db,
logger: logger,
}
}
func (isp *incidentUpdateSevertityProcessor) IncidentUpdateSeverityRequestProcess(callback slack.InteractionCallback, request *socketmode.Request) {
incidentSeverity, err := query.FindIncidentSeverityEntity(isp.db, isp.logger)
if err != nil {
isp.logger.Error("FindSeverityEntity error",
zap.String("incident_slack_channel_id", callback.Channel.ID), zap.String("channel", callback.Channel.Name),
zap.String("user_id", callback.User.ID), zap.Error(err))
return
}
modalRequest := houston.BuildIncidentUpdateSeverityModal(callback.Channel, incidentSeverity)
_, err = isp.client.OpenView(callback.TriggerID, modalRequest)
if err != nil {
isp.logger.Error("houston slack 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 *incidentUpdateSevertityProcessor) IncidentUpdateSeverity(callback slack.InteractionCallback, request *socketmode.Request, channel slack.Channel, user slack.User) {
incidentEntity, err := query.FindIncidentByChannelId(isp.db, callback.View.PrivateMetadata)
if err != nil {
isp.logger.Error("FindIncidentByChannelId error",
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
zap.String("user_id", user.ID), zap.Error(err))
} else if incidentEntity == nil {
isp.logger.Error("IncidentEntity not found ",
zap.String("incident_slack_channel_id", callback.Channel.ID), zap.String("channel", callback.Channel.Name),
zap.String("user_id", callback.User.ID), zap.Error(err))
return
}
incidentSeverityId := buildUpdateIncidentSeverityRequest(isp.logger, callback.View.State.Values)
result, err := query.FindIncidentSeverityEntityById(isp.db, isp.logger, incidentSeverityId)
if err != nil {
isp.logger.Error("FindIncidentSeverityEntityById 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("SeverityEntity 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.SeverityId = int(result.ID)
incidentEntity.UpdatedBy = user.ID
incidentEntity.SeverityTat = time.Now().AddDate(0, 0, result.Sla)
err = query.UpdateIncident(isp.db, 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))
}
userIdList, err := query.FindDefaultUserIdToBeAddedBySeverity(isp.db, int(result.ID))
if err != nil {
isp.logger.Error("FindDefaultUserIdToBeAddedBySeverity error",
zap.String("incident_slack_channel_id", channel.ID), zap.String("channel", channel.Name),
zap.String("user_id", user.ID), zap.Error(err))
return
}
for _, o := range userIdList {
common.InviteUsersToConversation(isp.client, isp.logger, callback.View.PrivateMetadata, o)
}
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> > set severity 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 IncidentUpdateSeverity", zap.Error(errMessage))
return
}
var payload interface{}
isp.client.Ack(*request, payload)
}
//TODO - ADD USER ACCORDING TO SEVERITY
func buildUpdateIncidentSeverityRequest(logger *zap.Logger, blockActions map[string]map[string]slack.BlockAction) int {
var requestMap = make(map[string]string, 0)
for _, actions := range blockActions {
for actionID, action := range actions {
if action.Type == "static_select" {
requestMap[actionID] = action.SelectedOption.Value
}
}
}
selectedValue := requestMap["incident_severity_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 selectedValueInInt
}