95 lines
3.3 KiB
Go
95 lines
3.3 KiB
Go
package command
|
|
|
|
import (
|
|
"encoding/json"
|
|
"fmt"
|
|
"houston/model/request"
|
|
"houston/pkg/postgres/query"
|
|
houston "houston/pkg/slack/houston/design"
|
|
|
|
"github.com/slack-go/slack"
|
|
"github.com/slack-go/slack/socketmode"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type incidentAssignProcessor struct {
|
|
client *socketmode.Client
|
|
db *gorm.DB
|
|
logger *zap.Logger
|
|
}
|
|
|
|
func NewIncidentAssignProcessor(client *socketmode.Client, db *gorm.DB, logger *zap.Logger) *incidentAssignProcessor {
|
|
return &incidentAssignProcessor{
|
|
client: client,
|
|
db: db,
|
|
logger: logger,
|
|
}
|
|
}
|
|
|
|
func (iap *incidentAssignProcessor) IncidentAssignProcess(callback slack.InteractionCallback, request *socketmode.Request) {
|
|
|
|
modalRequest := houston.GenerateModalForIncidentAssign(callback.Channel)
|
|
_, err := iap.client.OpenView(callback.TriggerID, modalRequest)
|
|
if err != nil {
|
|
iap.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{}
|
|
iap.client.Ack(*request, payload)
|
|
|
|
}
|
|
|
|
func (iap *incidentAssignProcessor) IncidentAssignModalCommandProcessing(callback slack.InteractionCallback, request *socketmode.Request) {
|
|
incidentEntity, err := query.FindIncidentByChannelId(iap.db, callback.View.PrivateMetadata)
|
|
if err != nil {
|
|
iap.logger.Error("FindIncidentByChannelId error",
|
|
zap.String("incident_slack_channel_id", callback.View.PrivateMetadata),
|
|
zap.String("user_id", callback.User.ID), zap.Error(err))
|
|
} else if incidentEntity == nil {
|
|
iap.logger.Error("IncidentEntity not found ",
|
|
zap.String("incident_slack_channel_id", callback.View.PrivateMetadata), zap.String("channel", callback.Channel.Name),
|
|
zap.String("user_id", callback.User.ID), zap.Error(err))
|
|
return
|
|
}
|
|
assignIncidentRoleRequest := buildAssignIncidentRoleRequest(callback.View.State.Values)
|
|
assignIncidentRoleRequest.CreatedById = callback.User.ID
|
|
assignIncidentRoleRequest.IncidentId = int(incidentEntity.ID)
|
|
iap.logger.Info("request", zap.Any("request", assignIncidentRoleRequest))
|
|
err = query.UpsertIncidentRole(iap.db, assignIncidentRoleRequest)
|
|
if err != nil {
|
|
iap.logger.Error("UpsertIncidentRole failed", zap.Error(err))
|
|
return
|
|
}
|
|
msgOption := slack.MsgOptionText(fmt.Sprintf("<@%s> is assigned to %s by <@%s>", assignIncidentRoleRequest.UserId, assignIncidentRoleRequest.Role, assignIncidentRoleRequest.CreatedById), false)
|
|
_, _, errMessage := iap.client.PostMessage(callback.View.PrivateMetadata, msgOption)
|
|
if errMessage != nil {
|
|
iap.logger.Error("post response failed for IncidentAssignModalCommandProcessing", zap.Error(errMessage))
|
|
return
|
|
}
|
|
|
|
var payload interface{}
|
|
iap.client.Ack(*request, payload)
|
|
|
|
}
|
|
|
|
func buildAssignIncidentRoleRequest(blockActions map[string]map[string]slack.BlockAction) *request.AddIncidentRoleRequest {
|
|
var addIncidentRoleRequest request.AddIncidentRoleRequest
|
|
var requestMap = make(map[string]string, 0)
|
|
for _, actions := range blockActions {
|
|
for actionID, action := range actions {
|
|
if action.Type == "users_select" {
|
|
requestMap[actionID] = action.SelectedUser
|
|
}
|
|
if action.Type == "static_select" {
|
|
requestMap[actionID] = action.SelectedOption.Value
|
|
}
|
|
}
|
|
}
|
|
|
|
desRequestMap, _ := json.Marshal(requestMap)
|
|
json.Unmarshal(desRequestMap, &addIncidentRoleRequest)
|
|
return &addIncidentRoleRequest
|
|
}
|