This commit is contained in:
@@ -11,6 +11,7 @@ import (
|
||||
"net/http"
|
||||
"os"
|
||||
"strconv"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"github.com/google/uuid"
|
||||
@@ -57,7 +58,7 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
AssignmentGroup: nullStr(incoming.AssignmentGroup),
|
||||
AssignedTo: nullStr(incoming.AssignedTo),
|
||||
Category: nullStr(incoming.Category),
|
||||
Subcategory: nullStr(incoming.SubCategory),
|
||||
SubCategory: nullStr(incoming.SubCategory),
|
||||
CreatedAt: sql.NullTime{Time: createdTime, Valid: true},
|
||||
}
|
||||
|
||||
@@ -99,7 +100,7 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
AssignmentGroup: nullStr(incoming.AssignmentGroup),
|
||||
AssignedTo: nullStr(incoming.AssignedTo),
|
||||
Category: nullStr(incoming.Category),
|
||||
Subcategory: nullStr(incoming.SubCategory),
|
||||
SubCategory: nullStr(incoming.SubCategory),
|
||||
SysID: nullStr(sysID),
|
||||
CreatedAt: sql.NullTime{Time: createdTime, Valid: true},
|
||||
}
|
||||
@@ -116,11 +117,11 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
// Use the returned incidentRecordId to generate the ticket number, and update the database correspondingly
|
||||
ticket := fmt.Sprintf("TKT%06d", incidentRecord.ID)
|
||||
|
||||
incNumParams := queries.UpdateIncidentNumberParams{
|
||||
ExternalId: incoming.ExternalID,
|
||||
incNumParams := queries.UpdateIncidentNumberSysIdParams{
|
||||
SysId: nullStr(sysID),
|
||||
IncidentNumber: nullStr(ticket),
|
||||
}
|
||||
err = h.Database.Queries().UpdateIncidentNumber(ctx, incNumParams)
|
||||
err = h.Database.Queries().UpdateIncidentNumberSysId(ctx, incNumParams)
|
||||
if err != nil {
|
||||
h.Logger.Error("failed to update incident with incident number", "number", ticket, "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
@@ -173,13 +174,56 @@ func (h *Handler) NewSnow(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", "application/json")
|
||||
w.WriteHeader(http.StatusOK)
|
||||
json.NewEncoder(w).Encode(response)
|
||||
return
|
||||
|
||||
} else if len(incoming.IncidentNumber) > 0 {
|
||||
// Incident already exists because we know the incident number, so update status or worknotes
|
||||
h.Logger.Debug("updating incident database record")
|
||||
|
||||
// TODO
|
||||
inc, err := h.Database.Queries().GetIncident(ctx, nullStr(incoming.IncidentNumber))
|
||||
if err != nil {
|
||||
h.Logger.Error("failed to find existing incident to update", "number", incoming.IncidentNumber, "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error : %v\n", err)
|
||||
return
|
||||
} else {
|
||||
prettyPrint(inc)
|
||||
}
|
||||
|
||||
updateParams := h.populateChangedFields(inc, incoming)
|
||||
|
||||
err = h.Database.Queries().UpdateIncident(ctx, updateParams)
|
||||
if err != nil {
|
||||
h.Logger.Error("failed to update incident", "number", incoming.IncidentNumber, "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error : %v\n", err)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("updated incident database record", "number", incoming.IncidentNumber)
|
||||
}
|
||||
|
||||
// TODO - add any worknotes specified
|
||||
if len(incoming.WorkNotes) > 0 {
|
||||
h.Logger.Debug("TODO add worknotes")
|
||||
|
||||
// Create the worknotes entry
|
||||
wnParms := queries.CreateWorkNoteParams{
|
||||
IncidentNumber: incoming.IncidentNumber,
|
||||
Note: nullStr(incoming.WorkNotes),
|
||||
}
|
||||
_, err = h.Database.Queries().CreateWorkNote(ctx, wnParms)
|
||||
if err != nil {
|
||||
h.Logger.Error("failed to create worknotes for incident with incident number", "number", incoming.IncidentNumber, "error", err)
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
fmt.Fprintf(w, "Error : %v\n", err)
|
||||
return
|
||||
} else {
|
||||
h.Logger.Debug("created worknotes database record", "number", incoming.IncidentNumber)
|
||||
}
|
||||
}
|
||||
|
||||
} else {
|
||||
// Unexpected condition
|
||||
// TODO - return error
|
||||
@@ -215,3 +259,84 @@ func randomImportSet() string {
|
||||
}
|
||||
return string(b)
|
||||
}
|
||||
|
||||
func (h *Handler) populateChangedFields(incRecord queries.Incident, newData models.Incoming) queries.UpdateIncidentParams {
|
||||
params := queries.UpdateIncidentParams{
|
||||
IncidentNumber: incRecord.IncidentNumber, // Always required
|
||||
ExternalID: incRecord.ExternalID,
|
||||
}
|
||||
|
||||
if strings.TrimSpace(newData.Description) != strings.TrimSpace(incRecord.Description.String) && len(strings.TrimSpace(newData.Description)) > 0 {
|
||||
h.Logger.Debug("Updating Description", "new", newData.Description)
|
||||
params.Description = nullStr(newData.Description)
|
||||
} else {
|
||||
params.Description = incRecord.Description
|
||||
}
|
||||
if strings.TrimSpace(newData.ShortDescription) != strings.TrimSpace(incRecord.ShortDescription.String) && len(strings.TrimSpace(newData.ShortDescription)) > 0 {
|
||||
h.Logger.Debug("Updating ShortDescription", "new", newData.ShortDescription)
|
||||
params.ShortDescription = nullStr(newData.ShortDescription)
|
||||
} else {
|
||||
params.ShortDescription = incRecord.ShortDescription
|
||||
}
|
||||
|
||||
if strings.TrimSpace(newData.Urgency) != strconv.FormatInt(incRecord.Urgency.Int64, 10) {
|
||||
if n, err := strconv.ParseInt(newData.Urgency, 10, 64); err == nil {
|
||||
h.Logger.Debug("Updating Urgency", "new", newData.Urgency)
|
||||
params.Urgency = nullInt64(n)
|
||||
} else {
|
||||
params.Urgency = incRecord.Urgency
|
||||
}
|
||||
} else {
|
||||
params.Urgency = incRecord.Urgency
|
||||
}
|
||||
if strings.TrimSpace(newData.State) != strconv.FormatInt(incRecord.State.Int64, 10) {
|
||||
if n, err := strconv.ParseInt(newData.State, 10, 64); err == nil {
|
||||
h.Logger.Debug("Updating State", "new", newData.State)
|
||||
params.State = nullInt64(n)
|
||||
} else {
|
||||
params.State = incRecord.State
|
||||
}
|
||||
} else {
|
||||
params.State = incRecord.State
|
||||
}
|
||||
if strings.TrimSpace(newData.Impact) != strconv.FormatInt(incRecord.Impact.Int64, 10) {
|
||||
if n, err := strconv.ParseInt(newData.Impact, 10, 64); err == nil {
|
||||
h.Logger.Debug("Updating Impact", "new", newData.Impact)
|
||||
params.Impact = nullInt64(n)
|
||||
} else {
|
||||
params.Impact = incRecord.Impact
|
||||
}
|
||||
} else {
|
||||
params.Impact = incRecord.Impact
|
||||
}
|
||||
|
||||
if strings.TrimSpace(newData.AssignmentGroup) != strings.TrimSpace(incRecord.AssignmentGroup.String) && len(strings.TrimSpace(newData.AssignmentGroup)) > 0 {
|
||||
h.Logger.Debug("Updating AssignmentGroup", "new", newData.AssignmentGroup)
|
||||
params.AssignmentGroup = nullStr(newData.AssignmentGroup)
|
||||
} else {
|
||||
params.AssignmentGroup = incRecord.AssignmentGroup
|
||||
}
|
||||
if strings.TrimSpace(newData.AssignedTo) != strings.TrimSpace(incRecord.AssignedTo.String) && len(strings.TrimSpace(newData.AssignedTo)) > 0 {
|
||||
h.Logger.Debug("Updating AssignedTo", "new", newData.AssignedTo)
|
||||
params.AssignedTo = nullStr(newData.AssignedTo)
|
||||
} else {
|
||||
params.AssignedTo = incRecord.AssignedTo
|
||||
}
|
||||
if strings.TrimSpace(newData.Category) != strings.TrimSpace(incRecord.Category.String) && len(strings.TrimSpace(newData.Category)) > 0 {
|
||||
h.Logger.Debug("Updating Category", "new", newData.Category)
|
||||
params.Category = nullStr(newData.Category)
|
||||
} else {
|
||||
params.Category = incRecord.Category
|
||||
}
|
||||
if strings.TrimSpace(newData.SubCategory) != strings.TrimSpace(incRecord.SubCategory.String) && len(strings.TrimSpace(newData.SubCategory)) > 0 {
|
||||
h.Logger.Debug("Updating SubCategory", "new", newData.SubCategory)
|
||||
params.Category = nullStr(newData.SubCategory)
|
||||
} else {
|
||||
params.SubCategory = incRecord.SubCategory
|
||||
}
|
||||
// TODO
|
||||
|
||||
h.Logger.Debug("populateChangedFields returning", "params", params)
|
||||
|
||||
return params
|
||||
}
|
||||
|
Reference in New Issue
Block a user