commit 4254de3a892c064477a76dbc6df04ae474195956 Author: Andy Krohg Date: Tue Nov 26 19:18:20 2024 -0500 Remove attachments diff --git a/internal/handlers/case.go b/internal/handlers/case.go index 1ae7c73..e7f023e 100644 --- a/internal/handlers/case.go +++ b/internal/handlers/case.go @@ -3,12 +3,10 @@ package handlers import ( "encoding/json" "fmt" - "io" "net/http" "strings" "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/middleware" - "go.uber.org/zap" "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/config" utils "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/util" @@ -38,19 +36,6 @@ func CaseHandler(c *gin.Context) { log := logger.GetLogger() cfg := config.GetConfig() - fileName, fileBytes, err := processFormAttachment(c, log) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - return - } - - mime := http.DetectContentType(fileBytes) - if !validation.IsAllowedMIMEType(mime) { - c.AbortWithStatus(http.StatusUnsupportedMediaType) - log.Errorf("unsupported media type %s", mime) - return - } - email, err := utils.GetEmailIDFromToken(middleware.GetAccessTokenFromHeader(c)) if err != nil { c.JSON(http.StatusBadRequest, gin.H{"error": err.Error()}) @@ -153,53 +138,17 @@ func CaseHandler(c *gin.Context) { return } - attachment, err := servicenow.AddAttachment(token, &models.Attachment{ - ID: serviceNowCase.Result.ID, - FileName: fileName, - Content: fileBytes, - }) - if err != nil { - c.JSON(http.StatusInternalServerError, gin.H{"error": err.Error()}) - log.Errorf("add attachment error occurred: %s", err.Error()) - return - } + log.Infof("case created %+v", serviceNowCase.Result.ID) + response := Response{ - AttachmentID: attachment.Result.ID, - AttachmentFileName: attachment.Result.FileName, - CaseID: serviceNowCase.Result.ID, - CaseNumber: serviceNowCase.Result.Number, + CaseID: serviceNowCase.Result.ID, + CaseNumber: serviceNowCase.Result.Number, } log.Infof("case added successfully %+v", &response) c.JSON(http.StatusOK, response) } -func processFormAttachment(c *gin.Context, log *zap.SugaredLogger) (filename string, content []byte, err error) { - file, err := c.FormFile("file") - if err != nil { - return "", nil, fmt.Errorf("bind json for %s error occurred: %s", "ServiceNowCaseNew", err.Error()) - } - - fileData, err := file.Open() - if err != nil { - return "", nil, fmt.Errorf("failed to open uploaded file: %s", err.Error()) - } - - defer func() { - err := fileData.Close() - if err != nil { - log.Errorf("failed to close uploaded file: %s", err.Error()) - } - }() - - fileBytes, err := io.ReadAll(fileData) - if err != nil { - return "", nil, fmt.Errorf("failed to read uploaded file: %s", err.Error()) - } - - return file.Filename, fileBytes, err -} - func validateContractID(contractID string) error { if contractID != "" { validContractID, err := utils.ValidateContractID(contractID) diff --git a/internal/middleware/auth.go b/internal/middleware/auth.go index b8449b0..2c85c71 100644 --- a/internal/middleware/auth.go +++ b/internal/middleware/auth.go @@ -2,12 +2,13 @@ package middleware import ( "context" + "net/http" + "github.com/gin-gonic/gin" sdk "github.com/openshift-online/ocm-sdk-go" azv1 "github.com/openshift-online/ocm-sdk-go/authorizations/v1" "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/config" "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/logger" - "net/http" ) // AuthMiddleware handles authentication. diff --git a/internal/models/attachment.go b/internal/models/attachment.go deleted file mode 100644 index 21d7fd4..0000000 --- a/internal/models/attachment.go +++ /dev/null @@ -1,20 +0,0 @@ -package models - -// Attachment represents an attachment with its associated data. -type Attachment struct { - ID string `json:"sys_id,omitempty"` - FileName string `json:"file_name,omitempty"` - Content []byte `json:"content,omitempty"` -} - -// AttachmentResult represents the result of an attachment operation. -type AttachmentResult struct { - ID string `json:"sys_id,omitempty"` - Size string `json:"size_bytes,omitempty"` - FileName string `json:"file_name,omitempty"` -} - -// AttachmentResponse represents the response containing an attachment result. -type AttachmentResponse struct { - Result AttachmentResult `json:"result,omitempty"` -} diff --git a/internal/servicenow/attachment.go b/internal/servicenow/attachment.go deleted file mode 100644 index dc69595..0000000 --- a/internal/servicenow/attachment.go +++ /dev/null @@ -1,59 +0,0 @@ -package servicenow - -import ( - "bytes" - "encoding/json" - "fmt" - "io" - "net/http" - "net/url" - - "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/config" - "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/logger" - "gitlab.cee.redhat.com/service/fedramp-customer-interest-service/internal/models" -) - -// AddAttachment adds an attachment to a case in ServiceNow. -func AddAttachment(token *models.AccessToken, attachment *models.Attachment) (*models.AttachmentResponse, error) { - log := logger.GetLogger() - cfg := config.GetConfig() - client := GetClient() - - log.Debugf("attachment is null %v \n ", attachment == nil) - - params := url.Values{} - params.Add("table_name", "case") - params.Add("table_sys_id", attachment.ID) - params.Add("file_name", attachment.FileName) - urlStr := CreateURL(cfg.ServiceNowURL, AttachmentPath, params) - - request, err := http.NewRequest(http.MethodPost, urlStr, bytes.NewReader(attachment.Content)) - if err != nil { - log.Debugf("http request to add attachment to case could not created - %s", err.Error()) - return nil, errCaseCreation - } - - SetAuthHeader(token.Token, request) - response, err := client.Do(request) - - if err != nil { - log.Errorf("could not add attachment to the case - %s", err.Error()) - return nil, errAttachmentAdding - } - defer func() { _ = response.Body.Close() }() - - if response.StatusCode != http.StatusCreated { - errorResponse, _ := io.ReadAll(response.Body) - log.Errorf("no attachment created - %d %s", response.StatusCode, string(errorResponse)) - return nil, fmt.Errorf("no attachment created %d %s", response.StatusCode, string(errorResponse)) - } - - var attachmentResponse models.AttachmentResponse - err = json.NewDecoder(response.Body).Decode(&attachmentResponse) - if err != nil { - log.Errorf("error while parsing response body - %s", err.Error()) - return &attachmentResponse, errParsingResponseBody - } - - return &attachmentResponse, nil -}