Files
houston-be/pkg/slack/houston/command/incident_update_status.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

112 lines
3.9 KiB
Go

package command
import (
"fmt"
"houston/entity"
"houston/pkg/postgres/query"
houston "houston/pkg/slack/houston/design"
"strconv"
"github.com/slack-go/slack"
"github.com/slack-go/slack/socketmode"
"go.uber.org/zap"
"gorm.io/gorm"
)
type incidentUpdateStatusProcessor struct {
client *socketmode.Client
db *gorm.DB
logger *zap.Logger
}
func NewIncidentUpdateStatusProcessor(client *socketmode.Client, db *gorm.DB, logger *zap.Logger) *incidentUpdateStatusProcessor {
return &incidentUpdateStatusProcessor{
client: client,
db: db,
logger: logger,
}
}
func (isp *incidentUpdateStatusProcessor) IncidentUpdateStatusRequestProcess(callback slack.InteractionCallback, request *socketmode.Request) {
modalRequest := houston.BuildIncidentUpdateStatusModal(isp.db, callback.Channel)
_, 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 *incidentUpdateStatusProcessor) IncidentUpdateStatus(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("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 := query.FindIncidentStatusById(isp.db, 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))
} 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 = entity.IncidentStatus(result.Name)
incidentEntity.UpdatedBy = user.ID
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))
}
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> > set status to %s", user.ID, incidentEntity.Status), 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 incidentEntity.Status == "RESOLVED" {
isp.client.ArchiveConversation(callback.View.PrivateMetadata)
}
var payload interface{}
isp.client.Ack(*request, payload)
}
//TODO - FOR RESOLVED SCENARIO
func buildUpdateIncidentStatusRequest(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_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 selectedValueInInt
}