* TP-40559 : Incident logs (#227) * TP-40559 : Adding BeforeUpdate and AfterUpdate hook in Incident Entity * TP-40936 - Added deep compare util function * TP-40559 | Added entity and repo for logs * TP-40559 : Added log entry support for incident level updates * Fix zero diff issue * Added lowercase json parameter names * TP-40559 : Added logs for team level updates * Initialize log service * TP-41640 : Added api to fetch logs for particular incident/team * Before create and after create * Convert 2 logs to one on incident creation * Log id populate: * Add populate for team id * Branch update changes * Typo changes * PR REVIEW CHANGES * Nil fix * Fix order issue * TP-43841 | Updating fetch users from conversation to differentiate memeber and others (#245) * Build fix * Added migration script for logs --------- Co-authored-by: Sriram Bhargav <sriram.bhargav@navi.com>
143 lines
4.8 KiB
Go
143 lines
4.8 KiB
Go
package service
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"github.com/gin-gonic/gin"
|
|
"github.com/slack-go/slack/socketmode"
|
|
"go.uber.org/zap"
|
|
"gorm.io/gorm"
|
|
util "houston/common/util"
|
|
"houston/internal/cron"
|
|
"houston/model/incident"
|
|
"houston/model/log"
|
|
"houston/model/severity"
|
|
"houston/model/team"
|
|
"houston/model/user"
|
|
"houston/pkg/slackbot"
|
|
service "houston/service/response"
|
|
common "houston/service/response/common"
|
|
"net/http"
|
|
)
|
|
|
|
type UserService struct {
|
|
gin *gin.Engine
|
|
logger *zap.Logger
|
|
client *slackbot.Client
|
|
db *gorm.DB
|
|
socketModeClient *socketmode.Client
|
|
authService *AuthService
|
|
userRepository *user.Repository
|
|
incidentRepository *incident.Repository
|
|
teamRepository *team.Repository
|
|
}
|
|
|
|
func NewUserService(gin *gin.Engine, logger *zap.Logger, client *slackbot.Client, db *gorm.DB,
|
|
socketModeClient *socketmode.Client, authService *AuthService) *UserService {
|
|
logRepository := log.NewLogRepository(logger, db)
|
|
teamRepository := team.NewTeamRepository(logger, db, logRepository)
|
|
return &UserService{
|
|
gin: gin,
|
|
logger: logger,
|
|
client: client,
|
|
db: db,
|
|
socketModeClient: socketModeClient,
|
|
authService: authService,
|
|
userRepository: user.NewUserRepository(logger, db),
|
|
incidentRepository: incident.NewIncidentRepository(logger, db, severity.NewSeverityRepository(logger, db), logRepository, teamRepository, socketModeClient),
|
|
teamRepository: teamRepository,
|
|
}
|
|
}
|
|
|
|
func (u *UserService) GetUserInfo(c *gin.Context) {
|
|
userId := c.Param("id")
|
|
|
|
users, err := u.client.GetUsersInfo(userId)
|
|
if err != nil {
|
|
u.logger.Error("error in getting user info", zap.String("userId", userId), zap.Error(err))
|
|
c.JSON(http.StatusInternalServerError, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
usersInfo := *users
|
|
c.JSON(http.StatusOK, common.SuccessResponse(&service.UserResponse{
|
|
Id: usersInfo[0].ID,
|
|
Name: usersInfo[0].Profile.RealName,
|
|
Email: usersInfo[0].Profile.Email,
|
|
Image: usersInfo[0].Profile.Image32,
|
|
}, http.StatusOK))
|
|
}
|
|
|
|
func (u *UserService) GetUsersInConversation(c *gin.Context) {
|
|
channelId := c.Query("channel_id")
|
|
incidentEntity, err := u.incidentRepository.FindIncidentByChannelId(channelId)
|
|
if err != nil {
|
|
u.logger.Error("error in getting incident by channel id", zap.String("channelId", channelId), zap.Error(err))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
if incidentEntity == nil {
|
|
err := errors.New(fmt.Sprintf("incident with channel id %v not found", channelId))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
teamEntity, err := u.teamRepository.FindTeamById(incidentEntity.TeamId)
|
|
if err != nil {
|
|
u.logger.Error(fmt.Sprintf("error getting team info for team with id %v", incidentEntity.TeamId), zap.Error(err))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
users, err := u.client.GetUsersInConversation(channelId)
|
|
if err != nil {
|
|
u.logger.Error(fmt.Sprintf("error in getting users from channel %v", channelId), zap.Error(err))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
|
|
usersInfo, err := u.client.GetUsersInfo(users...)
|
|
if err != nil {
|
|
u.logger.Error("error in getting users info", zap.Error(err))
|
|
c.JSON(http.StatusBadRequest, common.ErrorResponse(err, http.StatusBadRequest, nil))
|
|
return
|
|
}
|
|
usersData := *usersInfo
|
|
teamMembers := util.ConvertSliceToMapOfString(teamEntity.SlackUserIds)
|
|
var participants []service.UserResponse
|
|
var others []service.UserResponse
|
|
for userIndex := range usersData {
|
|
userInfo := service.UserResponse{
|
|
Id: usersData[userIndex].ID,
|
|
Name: usersData[userIndex].Profile.RealName,
|
|
Email: usersData[userIndex].Profile.Email,
|
|
Image: usersData[userIndex].Profile.Image32,
|
|
}
|
|
if teamMembers[usersData[userIndex].ID] != "" {
|
|
participants = append(participants, userInfo)
|
|
} else {
|
|
others = append(others, userInfo)
|
|
}
|
|
}
|
|
c.JSON(http.StatusOK, common.SuccessResponse(service.ChannelMembersResponse{
|
|
Participants: participants,
|
|
Others: others,
|
|
}, http.StatusOK))
|
|
}
|
|
func (u *UserService) UpdateHoustonUsers(c *gin.Context) {
|
|
|
|
userEmail := c.GetHeader("X-User-Email")
|
|
authResult, _ := u.authService.checkIfManagerOrAdmin(c, "", Admin, Manager)
|
|
if !authResult {
|
|
err := errors.New(fmt.Sprintf("%v is not an admin", userEmail))
|
|
c.JSON(http.StatusUnauthorized, common.ErrorResponse(err, http.StatusUnauthorized, nil))
|
|
return
|
|
}
|
|
userRepository := user.NewUserRepository(u.logger, u.db)
|
|
socketModeClient := u.socketModeClient
|
|
logger := u.logger
|
|
|
|
go func() {
|
|
cron.UpsertUsers(socketModeClient, logger, userRepository)
|
|
}()
|
|
|
|
c.JSON(http.StatusOK, common.SuccessResponse("done", http.StatusOK))
|
|
}
|