From 4e670f87854146390ed12a2151d23730f7c6af94 Mon Sep 17 00:00:00 2001 From: Nathan Coad Date: Tue, 25 Mar 2025 13:24:23 +1100 Subject: [PATCH] getIncident list response --- server/handler/getIncident.go | 72 +++++++++++++++++++++++++++++++++++ server/models/models.go | 21 ++++++++-- 2 files changed, 90 insertions(+), 3 deletions(-) diff --git a/server/handler/getIncident.go b/server/handler/getIncident.go index 035eba7..d9b4184 100644 --- a/server/handler/getIncident.go +++ b/server/handler/getIncident.go @@ -5,7 +5,9 @@ import ( "encoding/json" "errors" "fmt" + "mocksnow/server/models" "net/http" + "strconv" "strings" ) @@ -37,10 +39,80 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) { // Handle the specific 'number' query parameter number := query.Get("number") + active := query.Get("active") if number != "" { h.Logger.Debug("GetIncident called for specific incident number", "number", number) // Query record from database and return that + } else if active == "true" { + // Return list of all incidents + h.Logger.Debug("GetIncident called for list all incidents") + + responseList := make([]models.SingleIncidentResponse, 0) + + incList, err := h.Database.Queries().ListIncidents(ctx) + if err != nil { + if errors.Is(err, sql.ErrNoRows) { + h.Logger.Debug("No incidents found") + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, "{\"result\": [{}]}") + return + } else { + h.Logger.Error("", "error", err) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{ + "status": "ERROR", + "message": fmt.Sprintf("Unable to query database for all incidents: '%s'", err), + }) + return + } + } + + // convert incList into json output + for _, inc := range incList { + // transform inc to SingleIncidentResponse + r := models.SingleIncidentResponse{ + Number: inc.IncidentNumber.String, + SysID: inc.SysID.String, + IncidentState: strconv.FormatInt(inc.State.Int64, 10), + State: strconv.FormatInt(inc.State.Int64, 10), + Impact: strconv.FormatInt(inc.Impact.Int64, 10), + Urgency: strconv.FormatInt(inc.Urgency.Int64, 10), + ShortDescription: inc.ShortDescription.String, + AssignedTo: inc.AssignedTo.String, + Category: inc.Category.String, + Subcategory: inc.SubCategory.String, + // TODO + } + // add to responseList + h.Logger.Debug("Adding incident to active inc response list", "incident", r) + responseList = append(responseList, r) + } + + // marshal struct into json and return + + wrappedList := models.MultipleIncidentResponse{ + Result: responseList, + } + prettyPrint(wrappedList) + + b, err := json.Marshal(wrappedList) + if err != nil { + h.Logger.Error("Unable to convert database records into json", "error", err) + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusInternalServerError) + json.NewEncoder(w).Encode(map[string]string{ + "status": "ERROR", + "message": fmt.Sprintf("Unable to convert database records into json: '%s'", err), + }) + return + } + w.Header().Set("Content-Type", "application/json") + w.WriteHeader(http.StatusOK) + fmt.Fprintf(w, string(b)) + return } else { // Requested a single incident diff --git a/server/models/models.go b/server/models/models.go index a93e8e6..c8cfa0f 100644 --- a/server/models/models.go +++ b/server/models/models.go @@ -18,11 +18,26 @@ type IncidentResultItem struct { // TODO - populate more fields here type SingleIncidentResponse struct { - IncidentNumber string `json:"incident_number,omitempty"` // The incident number in ServiceNow. If blank, creates a new incident, if populated with a valid value it updates that record. - State string `json:"state,omitempty"` // integer value, 1-6 (6 is resolved) + Number string `json:"number"` // corresponds with incident_number + SysID string `json:"sys_id,omitempty"` + IncidentState string `json:"incident_state"` // integer value, 1-6 (6 is resolved) + State string `json:"state,omitempty"` // unsure how this differs from IncidentState + ShortDescription string `json:"short_description"` + AssignedTo interface{} `json:"assigned_to,omitempty"` + Description string `json:"description"` + Urgency string `json:"urgency"` + Impact string `json:"impact"` + Category string `json:"category"` + Subcategory string `json:"subcategory"` + // TODO - unvalidated from here on + // Associated DeviceID (UUID from CPDB) ExternalID string `json:"external_id,omitempty"` // CPDB UUID for the configuration item - SysID string `json:"sys_id,omitempty"` + +} + +type MultipleIncidentResponse struct { + Result []SingleIncidentResponse `json:"result"` } type Incoming struct {