initial version of endpoint
This commit is contained in:
@@ -1,16 +1,20 @@
|
||||
package handler
|
||||
|
||||
import (
|
||||
"database/sql"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"io"
|
||||
"net/http"
|
||||
"math/rand"
|
||||
"mocksnow/db/queries"
|
||||
"mocksnow/server/models"
|
||||
"net/http"
|
||||
|
||||
"github.com/google/uuid"
|
||||
)
|
||||
|
||||
// NewSnow receives data from the DMSP Snow New() function
|
||||
func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
var ()
|
||||
|
||||
reqBody, err := io.ReadAll(r.Body)
|
||||
if err != nil {
|
||||
@@ -23,13 +27,93 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
// Decode the JSON body
|
||||
var inc models.Incident
|
||||
if err := json.Unmarshal(reqBody, &inc); err != nil {
|
||||
var incident models.Incident
|
||||
if err := json.Unmarshal(reqBody, &incident); err != nil {
|
||||
h.Logger.Error("unable to decode json", "error", err)
|
||||
http.Error(w, "Invalid JSON body", http.StatusBadRequest)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("successfully decoded JSON")
|
||||
prettyPrint(inc)
|
||||
prettyPrint(incident)
|
||||
}
|
||||
|
||||
ctx := r.Context()
|
||||
|
||||
// 1. Insert into incoming table
|
||||
params := queries.CreateIncomingParams{
|
||||
IncidentNumber: nullStr(incident.IncidentNumber),
|
||||
Description: nullStr(incident.Description),
|
||||
ShortDescription: nullStr(incident.ShortDescription),
|
||||
Urgency: nullStr(incident.Urgency),
|
||||
Impact: nullStr(incident.Impact),
|
||||
State: nullStr(incident.State),
|
||||
ExternalID: nullStr(incident.ExternalID),
|
||||
WorkNotes: nullStr(incident.WorkNotes),
|
||||
AssignmentGroup: nullStr(incident.AssignmentGroup),
|
||||
AssignedTo: nullStr(incident.AssignedTo),
|
||||
Category: nullStr(incident.Category),
|
||||
Subcategory: nullStr(incident.SubCategory),
|
||||
}
|
||||
|
||||
h.Logger.Debug("database params", "params", params)
|
||||
|
||||
// Insert the new inventory record into the database
|
||||
result, err := h.Database.Queries().CreateIncoming(ctx, params)
|
||||
if err != nil {
|
||||
h.Logger.Error("unable to perform database insert", "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error : %v\n", err)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("created database record", "insert_result", result)
|
||||
}
|
||||
|
||||
// Create record in incidents table
|
||||
incidentRecord, err := h.Database.Queries().CreateIncident(ctx, incident.ExternalID)
|
||||
if err != nil {
|
||||
h.Logger.Error("failed to create incident", "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error : %v\n", err)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("created database record", "incident_record", incidentRecord)
|
||||
}
|
||||
|
||||
// Simulate response
|
||||
ticket := fmt.Sprintf("TKT%06d", incidentRecord)
|
||||
sysID := uuid.New().String()
|
||||
recordLink := "https://server.fqdn.com/api/now/table/incident/" + sysID
|
||||
|
||||
response := models.IncidentResponse{
|
||||
ImportSet: randomImportSet(),
|
||||
StagingTable: "x_dusa2_itom_inc_imp",
|
||||
Result: []models.IncidentResultItem{
|
||||
{
|
||||
TransformMap: "Incident Import",
|
||||
Table: "incident",
|
||||
DisplayName: "number",
|
||||
DisplayValue: ticket,
|
||||
RecordLink: recordLink,
|
||||
Status: "inserted",
|
||||
SysID: sysID,
|
||||
},
|
||||
},
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
json.NewEncoder(w).Encode(response)
|
||||
}
|
||||
|
||||
func nullStr(s string) sql.NullString {
|
||||
return sql.NullString{String: s, Valid: s != ""}
|
||||
}
|
||||
|
||||
// Helper to generate random import_set ID
|
||||
func randomImportSet() string {
|
||||
const letters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789"
|
||||
b := make([]byte, 8)
|
||||
for i := range b {
|
||||
b[i] = letters[rand.Intn(len(letters))]
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
@@ -1,17 +1,19 @@
|
||||
package models
|
||||
|
||||
type IncidentResponse struct {
|
||||
ImportSet string `json:"import_set"`
|
||||
StagingTable string `json:"staging_table"`
|
||||
Result []struct {
|
||||
TransformMap string `json:"transform_map"`
|
||||
Table string `json:"table"`
|
||||
DisplayName string `json:"display_name"`
|
||||
DisplayValue string `json:"display_value"`
|
||||
RecordLink string `json:"record_link"`
|
||||
Status string `json:"status"`
|
||||
SysID string `json:"sys_id"`
|
||||
}
|
||||
ImportSet string `json:"import_set"`
|
||||
StagingTable string `json:"staging_table"`
|
||||
Result []IncidentResultItem `json:"result"`
|
||||
}
|
||||
|
||||
type IncidentResultItem struct {
|
||||
TransformMap string `json:"transform_map"`
|
||||
Table string `json:"table"`
|
||||
DisplayName string `json:"display_name"`
|
||||
DisplayValue string `json:"display_value"`
|
||||
RecordLink string `json:"record_link"`
|
||||
Status string `json:"status"`
|
||||
SysID string `json:"sys_id"`
|
||||
}
|
||||
|
||||
type Incident struct {
|
||||
@@ -33,3 +35,19 @@ type Incident struct {
|
||||
Category string `json:"category,omitempty"`
|
||||
SubCategory string `json:"subcategory,omitempty"`
|
||||
}
|
||||
|
||||
// {
|
||||
// "import_set": "ABC12345",
|
||||
// "staging_table": "foobar1",
|
||||
// "result": [
|
||||
// {
|
||||
// "transform_map": "Incident Import",
|
||||
// "table": "incident",
|
||||
// "display_name": "number",
|
||||
// "display_value": "TKT000123",
|
||||
// "record_link": "https://server.fqdn.com/api/now/table/incident/640f831720eb48a107653be1b4d87225",
|
||||
// "status": "inserted",
|
||||
// "sys_id": "640f831720eb48a107653be1b4d87225"
|
||||
// }
|
||||
// ]
|
||||
// }
|
||||
|
Reference in New Issue
Block a user