Files
cybertron/service/HoustonService.go
Varnit Goyal 5f55df4750 TP-55555/houston-integration (#21)
Co-authored-by: github-cicd <github.cicd@navi.com>
2024-09-20 15:28:27 +05:30

160 lines
4.6 KiB
Go

package service
import (
"cybertron/constants"
"cybertron/models/db"
"cybertron/pkg/houstonClient"
"cybertron/pkg/kafka/producer"
"cybertron/pkg/log"
"fmt"
"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)
if err := c.BindJSON(&request); err != nil {
fmt.Println("Error binding JSON:", err)
createErrorResponse(c, http.StatusBadRequest, "Invalid request payload")
return
}
request.CreatedBy = userEmail
request.ErrorID = ""
fmt.Println("Received request payload:", request)
if missingFields := validateCreateHoustonRequest(request); len(missingFields) > 0 {
fmt.Println("Missing required fields:", 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 {
fmt.Println("Error creating incident:", err)
createErrorResponse(c, http.StatusInternalServerError, "Failed to create incident")
return
}
// Handle the response
if response.StatusCode != http.StatusOK {
fmt.Println("Failed to create incident, status code:", response.StatusCode)
createErrorResponse(c, response.StatusCode, "Failed to create incident")
return
}
result := houstonService.dbClient.Create(&db.Houston{
ErrorId: request.ErrorID,
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 {
fmt.Println("Error getting products:", 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 {
fmt.Println("Error getting responder team:", 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,
})
}