Skip to content

Commit

Permalink
Merge branch 'master' of github.com:teknologi-umum/semyi into feat/in…
Browse files Browse the repository at this point in the history
…cident-reader
  • Loading branch information
takadev15 committed May 27, 2024
2 parents 1ead96c + 17bd6a6 commit 0ba858a
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 20 deletions.
18 changes: 11 additions & 7 deletions backend/incident.go
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package main

import (
"fmt"
"time"
)

Expand Down Expand Up @@ -44,24 +43,29 @@ type Incident struct {
MonitorID string `json:"monitor_id"`
Title string `json:"title"`
Description string `json:"description"`
Timestamp string `json:"timestamp"`
Timestamp time.Time `json:"timestamp"`
Severity IncidentSeverity `json:"severity"`
Status IncidentStatus `json:"status"`
CreatedBy string `json:"created_by"`
}

func (i Incident) Validate() error {
_, err := time.Parse(time.RFC3339, i.Timestamp)
if err != nil {
return err
err := NewValidationError()

if i.Timestamp.IsZero() {
err.AddIssue("timestamp", "shouldn't be zero")
}

if !i.Severity.IsValid() {
return fmt.Errorf("invalid incident severity")
err.AddIssue("severity", "invalid")
}

if !i.Status.IsValid() {
return fmt.Errorf("invalid incident status")
err.AddIssue("status", "invalid")
}

if err.HasIssues() {
return err
}

return nil
Expand Down
27 changes: 20 additions & 7 deletions backend/incident_test.go
Original file line number Diff line number Diff line change
@@ -1,29 +1,27 @@
package main_test

import (
"errors"
main "semyi"
"testing"
"time"
)

func TestIncidentValidate(t *testing.T) {
validPayload := main.Incident{
MonitorID: "a84c2c59-748c-48d0-b628-4a73b1c3a8d7",
Title: "test",
Description: "description test",
Timestamp: "2024-05-26T15:04:05+07:00",
Timestamp: time.Date(2000, 7, 24, 4, 30, 15, 0, time.UTC),
Severity: main.IncidentSeverityError,
Status: main.IncidentStatusInvestigating,
}

t.Run("Should return error if payload is invalid", func(t *testing.T) {
t.Run("Timestamp", func(t *testing.T) {
validPayloadCopy := validPayload
mockTimestamps := []string{
"2024-05-26T15:04:05",
"2024-05-26",
"15:04:05",
"arbitary",
"2024-05-26 15:04:05",
mockTimestamps := []time.Time{
time.Date(1, 1, 1, 0, 0, 0, 0, time.UTC),
}

for _, timestamp := range mockTimestamps {
Expand All @@ -33,6 +31,11 @@ func TestIncidentValidate(t *testing.T) {
if err == nil {
t.Error("expect error, got nil")
}

var expectError *main.ValidationError
if !errors.As(err, &expectError) {
t.Errorf("expect error: %T, but got : %T", expectError, err)
}
}
})
t.Run("severity", func(t *testing.T) {
Expand All @@ -46,6 +49,11 @@ func TestIncidentValidate(t *testing.T) {
if err == nil {
t.Error("expect error, got nil")
}

var expectError *main.ValidationError
if !errors.As(err, &expectError) {
t.Errorf("expect error: %T, but got : %T", expectError, err)
}
}
})
t.Run("status", func(t *testing.T) {
Expand All @@ -59,6 +67,11 @@ func TestIncidentValidate(t *testing.T) {
if err == nil {
t.Error("expect error, got nil")
}

var expectError *main.ValidationError
if !errors.As(err, &expectError) {
t.Errorf("expect error: %T, but got : %T", expectError, err)
}
}
})
})
Expand Down
10 changes: 5 additions & 5 deletions backend/incident_writer.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,18 +23,18 @@ func (w *IncidentWriter) Write(ctx context.Context, incident Incident) error {
return fmt.Errorf("failed to get database connection: %w", err)
}

timestamp, err := time.Parse(time.RFC3339, incident.Timestamp)
if err != nil {
return fmt.Errorf("failed to parse timestamp: %w", err)
incidentStatus := incident.Status
if incident.Timestamp.After(time.Now()) {
incidentStatus = IncidentStatusScheduled
}

_, err = conn.ExecContext(ctx, "INSERT INTO incident_data (monitor_id, title, description, timestamp, severity, status, created_by) VALUES (?, ?, ?, ?, ?, ?, ?)",
incident.MonitorID,
incident.Title,
incident.Description,
timestamp,
incident.Timestamp,
incident.Severity,
incident.Status,
incidentStatus,
incident.CreatedBy,
)
if err != nil {
Expand Down
2 changes: 1 addition & 1 deletion backend/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func main() {

apiKey, ok := os.LookupEnv("API_KEY")
if !ok {
log.Fatal().Msg("API_KEY is required")
log.Warn().Msg("API_KEY is not set")
}

if os.Getenv("ENV") == "" {
Expand Down

0 comments on commit 0ba858a

Please sign in to comment.