add CommentsAndWorkNotes
All checks were successful
continuous-integration/drone/push Build is passing
All checks were successful
continuous-integration/drone/push Build is passing
This commit is contained in:
@@ -67,3 +67,10 @@ INSERT into worknotes (
|
||||
?, ?, CURRENT_TIMESTAMP
|
||||
)
|
||||
RETURNING *;
|
||||
|
||||
-- name: GetIncidentWorkNotes :many
|
||||
SELECT * from worknotes
|
||||
WHERE incident_number = sqlc.arg('incidentNumber');
|
||||
|
||||
-- name: ListWorkNotes :many
|
||||
SELECT * from worknotes;
|
@@ -246,6 +246,39 @@ func (q *Queries) GetIncidentReport(ctx context.Context) ([]GetIncidentReportRow
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const getIncidentWorkNotes = `-- name: GetIncidentWorkNotes :many
|
||||
SELECT id, incident_number, note, created_at from worknotes
|
||||
WHERE incident_number = ?1
|
||||
`
|
||||
|
||||
func (q *Queries) GetIncidentWorkNotes(ctx context.Context, incidentnumber string) ([]Worknote, error) {
|
||||
rows, err := q.db.QueryContext(ctx, getIncidentWorkNotes, incidentnumber)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Worknote
|
||||
for rows.Next() {
|
||||
var i Worknote
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.IncidentNumber,
|
||||
&i.Note,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listIncidents = `-- name: ListIncidents :many
|
||||
SELECT id, external_id, created_at, incident_number, description, short_description, urgency, impact, state, assignment_group, assigned_to, category, sub_category, sys_id FROM incidents
|
||||
`
|
||||
@@ -331,6 +364,38 @@ func (q *Queries) ListIncoming(ctx context.Context) ([]Incoming, error) {
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const listWorkNotes = `-- name: ListWorkNotes :many
|
||||
SELECT id, incident_number, note, created_at from worknotes
|
||||
`
|
||||
|
||||
func (q *Queries) ListWorkNotes(ctx context.Context) ([]Worknote, error) {
|
||||
rows, err := q.db.QueryContext(ctx, listWorkNotes)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
defer rows.Close()
|
||||
var items []Worknote
|
||||
for rows.Next() {
|
||||
var i Worknote
|
||||
if err := rows.Scan(
|
||||
&i.ID,
|
||||
&i.IncidentNumber,
|
||||
&i.Note,
|
||||
&i.CreatedAt,
|
||||
); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
items = append(items, i)
|
||||
}
|
||||
if err := rows.Close(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
if err := rows.Err(); err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return items, nil
|
||||
}
|
||||
|
||||
const updateIncident = `-- name: UpdateIncident :exec
|
||||
UPDATE incidents
|
||||
SET
|
||||
|
@@ -87,8 +87,32 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
}
|
||||
}
|
||||
|
||||
wknList, err := h.Database.Queries().ListWorkNotes(ctx)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
h.Logger.Debug("No incident worknotes found")
|
||||
} 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 incident worknotes: '%s'", err),
|
||||
})
|
||||
return
|
||||
}
|
||||
}
|
||||
|
||||
// convert incList into json output
|
||||
for _, inc := range incList {
|
||||
var wkn []string
|
||||
// get worknotes for this incident
|
||||
for _, note := range wknList {
|
||||
if note.IncidentNumber == inc.IncidentNumber.String {
|
||||
wkn = append(wkn, note.Note.String)
|
||||
}
|
||||
}
|
||||
|
||||
// transform inc to SingleIncidentResponse
|
||||
r := models.SingleIncidentResponse{
|
||||
Number: inc.IncidentNumber.String,
|
||||
@@ -101,6 +125,7 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
AssignedTo: inc.AssignedTo.String,
|
||||
Category: inc.Category.String,
|
||||
Subcategory: inc.SubCategory.String,
|
||||
CommentsAndWorkNotes: strings.Join(wkn, "\n\n"),
|
||||
// TODO
|
||||
}
|
||||
// add to responseList
|
||||
@@ -167,16 +192,31 @@ func (h *Handler) GetIncident(w http.ResponseWriter, r *http.Request) {
|
||||
|
||||
func (h *Handler) getSingleIncident(number string, ctx context.Context) ([]byte, error) {
|
||||
var b []byte
|
||||
var wkn []string
|
||||
incResult, err := h.Database.Queries().GetIncident(ctx, nullStr(number))
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
h.Logger.Debug("No incident record found", "number", number)
|
||||
} else {
|
||||
h.Logger.Error("Unable to query database for incident number", "error", err)
|
||||
h.Logger.Error("Unable to query database for incident number", "number", number, "error", err)
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
|
||||
wknotes, err := h.Database.Queries().GetIncidentWorkNotes(ctx, number)
|
||||
if err != nil {
|
||||
if errors.Is(err, sql.ErrNoRows) {
|
||||
h.Logger.Debug("No incident worknotes found", "number", number)
|
||||
} else {
|
||||
h.Logger.Error("Unable to query database for incident worknotes", "number", number, "error", err)
|
||||
return b, err
|
||||
}
|
||||
}
|
||||
|
||||
for _, note := range wknotes {
|
||||
wkn = append(wkn, note.Note.String)
|
||||
}
|
||||
|
||||
// convert from database resposne to expected json format
|
||||
r := models.SingleIncidentResponse{
|
||||
Number: incResult.IncidentNumber.String,
|
||||
@@ -189,6 +229,7 @@ func (h *Handler) getSingleIncident(number string, ctx context.Context) ([]byte,
|
||||
AssignedTo: incResult.AssignedTo.String,
|
||||
Category: incResult.Category.String,
|
||||
Subcategory: incResult.SubCategory.String,
|
||||
CommentsAndWorkNotes: strings.Join(wkn, "\n\n"),
|
||||
// TODO
|
||||
}
|
||||
|
||||
|
@@ -29,6 +29,7 @@ type SingleIncidentResponse struct {
|
||||
Impact string `json:"impact"`
|
||||
Category string `json:"category"`
|
||||
Subcategory string `json:"subcategory"`
|
||||
CommentsAndWorkNotes string `json:"comments_and_work_notes"`
|
||||
// TODO - unvalidated from here on
|
||||
|
||||
// Associated DeviceID (UUID from CPDB)
|
||||
|
Reference in New Issue
Block a user