166 lines
4.9 KiB
Go
166 lines
4.9 KiB
Go
package service
|
|
|
|
import (
|
|
"cybertron/constants"
|
|
"cybertron/models/db"
|
|
"cybertron/pkg/houstonClient"
|
|
"cybertron/pkg/kafka/producer"
|
|
"cybertron/pkg/log"
|
|
"go.uber.org/zap"
|
|
"net/http"
|
|
|
|
"github.com/gin-gonic/gin"
|
|
"gorm.io/gorm"
|
|
)
|
|
|
|
type HoustonService struct {
|
|
logger *log.Logger
|
|
dbClient *gorm.DB
|
|
kafkaProducer producer.KProducer
|
|
houstonClient houstonClient.HoustonClientInterface
|
|
}
|
|
|
|
type CreateHoustonRequest struct {
|
|
Title string `json:"title"`
|
|
SeverityId int `json:"severityId"`
|
|
Description string `json:"description"`
|
|
ReportingTeamId int `json:"reportingTeamId"`
|
|
ResponderTeamId int `json:"responderTeamId"`
|
|
ProductIds []int `json:"productIds"`
|
|
CreatedBy string `json:"createdBy"`
|
|
ErrorID string `json:"errorId"`
|
|
}
|
|
|
|
func NewHoustonService(logger *log.Logger, dbClient *gorm.DB, kafkaProducer producer.KProducer, houstonClient houstonClient.HoustonClientInterface) *HoustonService {
|
|
return &HoustonService{
|
|
logger: logger,
|
|
dbClient: dbClient,
|
|
kafkaProducer: kafkaProducer,
|
|
houstonClient: houstonClient,
|
|
}
|
|
}
|
|
|
|
func (houstonService *HoustonService) CreateHouston(c *gin.Context) {
|
|
var request CreateHoustonRequest
|
|
userEmail := c.GetHeader(constants.EMAIL_HEADER_NAME)
|
|
issueId := c.GetHeader(constants.ISSUE_ID_HEADER_NAME)
|
|
if err := c.BindJSON(&request); err != nil {
|
|
houstonService.logger.Error("Error binding JSON:", zap.Error(err))
|
|
createErrorResponse(c, http.StatusBadRequest, "Invalid request payload")
|
|
return
|
|
}
|
|
|
|
if userEmail == "" || issueId == "" {
|
|
houstonService.logger.Error("User email not found in headers")
|
|
createErrorResponse(c, http.StatusBadRequest, "BAD REQUEST")
|
|
return
|
|
}
|
|
|
|
request.CreatedBy = userEmail
|
|
request.ErrorID = ""
|
|
|
|
if missingFields := validateCreateHoustonRequest(request); len(missingFields) > 0 {
|
|
houstonService.logger.Info("Missing required fields:", zap.Strings("missingFields", missingFields))
|
|
c.JSON(http.StatusBadRequest, gin.H{
|
|
"message": "Missing required fields",
|
|
"fields": missingFields,
|
|
})
|
|
return
|
|
}
|
|
|
|
// Make the POST request using houstonClient
|
|
response, err := houstonService.houstonClient.CreateIncident(houstonClient.CreateHoustonRequest(request))
|
|
if err != nil {
|
|
houstonService.logger.Error("Error creating incident:", zap.Error(err))
|
|
createErrorResponse(c, http.StatusInternalServerError, "Failed to create incident")
|
|
return
|
|
}
|
|
|
|
// Handle the response
|
|
if response.StatusCode != http.StatusOK {
|
|
houstonService.logger.Info("Failed to create incident, status code:", zap.Int("statusCode", response.StatusCode))
|
|
createErrorResponse(c, response.StatusCode, "Failed to create incident")
|
|
return
|
|
}
|
|
|
|
result := houstonService.dbClient.Create(&db.Houston{
|
|
ErrorId: issueId,
|
|
HoustonID: response.Data.ID,
|
|
})
|
|
|
|
if result.Error != nil {
|
|
c.JSON(http.StatusInternalServerError, gin.H{"error": result.Error.Error(), "message": "Failed to create project"})
|
|
return
|
|
}
|
|
|
|
// Send the response body back to the client
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Incident created successfully",
|
|
"data": response.Data,
|
|
})
|
|
}
|
|
|
|
func (houstonService *HoustonService) GetProducts(c *gin.Context) {
|
|
// Get the products using the houstonClient
|
|
products, err := houstonService.houstonClient.GetAllProducts()
|
|
if err != nil {
|
|
houstonService.logger.Error("Error getting products:", zap.Error(err))
|
|
createErrorResponse(c, http.StatusInternalServerError, "Failed to get products")
|
|
return
|
|
}
|
|
|
|
// Send the response back to the client
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Products fetched successfully",
|
|
"data": products.Data,
|
|
})
|
|
}
|
|
|
|
func (houstonService *HoustonService) GetResponderTeam(c *gin.Context) {
|
|
// read query params
|
|
productID := c.QueryArray("productId")
|
|
|
|
// Get the responder team using the houstonClient
|
|
responderTeam, err := houstonService.houstonClient.GetReportingAndResponder(productID)
|
|
if err != nil {
|
|
houstonService.logger.Error("Error getting responder team:", zap.Error(err))
|
|
createErrorResponse(c, http.StatusInternalServerError, "Failed to get responder team")
|
|
return
|
|
}
|
|
|
|
// Send the response back to the client
|
|
c.JSON(http.StatusOK, gin.H{
|
|
"message": "Responder team fetched successfully",
|
|
"data": responderTeam.Data,
|
|
})
|
|
}
|
|
|
|
func validateCreateHoustonRequest(request CreateHoustonRequest) []string {
|
|
missingFields := []string{}
|
|
if request.Title == "" {
|
|
missingFields = append(missingFields, "title")
|
|
}
|
|
if request.SeverityId == 0 {
|
|
missingFields = append(missingFields, "severityId")
|
|
}
|
|
if request.Description == "" {
|
|
missingFields = append(missingFields, "description")
|
|
}
|
|
if request.ReportingTeamId == 0 {
|
|
missingFields = append(missingFields, "reportingTeamId")
|
|
}
|
|
if request.ResponderTeamId == 0 {
|
|
missingFields = append(missingFields, "responderTeamId")
|
|
}
|
|
if len(request.ProductIds) == 0 {
|
|
missingFields = append(missingFields, "productIds")
|
|
}
|
|
return missingFields
|
|
}
|
|
|
|
func createErrorResponse(c *gin.Context, statusCode int, message string) {
|
|
c.JSON(statusCode, gin.H{
|
|
"message": message,
|
|
})
|
|
}
|