Files
houston-be/service/users_service.go
Vijay Joshi 55da2b4791 INFRA-3570 : Do not show the current severity and status in update incident in slack UI (#439)
* INFRA-3570 : Do not show same severity and status in update incident in slack UI

* INFRA-3570 : Cyclic dependency fix

* INFRA-3570 : Minor changes

* INFRA-3570 : Add UT'S

* INFRA-3570 : Major refactor

* INFRA-3570 : Move all incident status repo functions to new service

* INFRA-3570 : Add UT's
2024-07-18 13:17:28 +05:30

145 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"
"houston/appcontext"
"houston/common/util"
"houston/logger"
"houston/model/incident"
"houston/model/log"
"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
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, client *slackbot.Client, db *gorm.DB,
socketModeClient *socketmode.Client, authService *AuthService) *UserService {
logRepository := log.NewLogRepository(db)
teamRepository := team.NewTeamRepository(db, logRepository)
return &UserService{
gin: gin,
client: client,
db: db,
socketModeClient: socketModeClient,
authService: authService,
userRepository: user.NewUserRepository(db),
incidentRepository: incident.NewIncidentRepository(
db, appcontext.GetSeverityService(), appcontext.GetIncidentStatusService(), 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 {
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 {
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 {
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 {
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 {
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) GetAllHoustonUserBots(c *gin.Context) {
userRepository := user.NewUserRepository(u.db)
botUsers, err := userRepository.GetAllActiveHoustonUserBots()
if err != nil {
logger.Error("error in getting all houston_user bots", zap.Error(err))
c.JSON(http.StatusInternalServerError, common.ErrorResponse(err, http.StatusInternalServerError, nil))
return
}
userResponses := make([]service.UserResponse, 0, len(botUsers))
for _, botUser := range botUsers {
userResponse := service.UserResponse{
Id: botUser.SlackUserId,
Name: botUser.RealName,
}
userResponses = append(userResponses, userResponse)
}
c.JSON(http.StatusOK, common.SuccessResponse(userResponses, http.StatusOK))
}