Skip to content

Commit

Permalink
fix: inline check for notification fallback
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Mar 3, 2025
1 parent fee61b2 commit df4fb8c
Show file tree
Hide file tree
Showing 2 changed files with 62 additions and 5 deletions.
50 changes: 46 additions & 4 deletions models/notifications.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/google/uuid"
"github.com/lib/pq"
"github.com/samber/lo"
"gorm.io/gorm"
)

// Notification represents the notifications table
Expand Down Expand Up @@ -51,6 +52,10 @@ type Notification struct {
Error *string `json:"error,omitempty"`
}

func (n Notification) HasFallbackSet() bool {
return n.FallbackTeamID != nil || n.FallbackPersonID != nil || len(n.FallbackCustomServices) != 0 || n.FallbackPlaybookID != nil
}

func (n Notification) TableName() string {
return "notifications"
}
Expand Down Expand Up @@ -85,10 +90,7 @@ const (
// to wait for the incremental scraper to re-evaluate the health.
NotificationStatusEvaluatingWaitFor = "evaluating-waitfor"

// Checking fallback channel configuration after primary channel failure
NotificationStatusCheckingFallback = "checking_fallback"

// Attempting delivery through fallback channel
// Attempting delivery through a fallback channel
NotificationStatusAttemptingFallback = "attempting_fallback"
)

Expand Down Expand Up @@ -226,3 +228,43 @@ func (n NotificationSilence) AsMap(removeFields ...string) map[string]any {
func (t *NotificationSilence) TableName() string {
return "notification_silences"
}

// GenerateFallbackAttempt creates a new notification history record
// based on the provided history for retrying with fallback recipients.
func GenerateFallbackAttempt(db *gorm.DB, notification Notification, history NotificationSendHistory) error {
// We need to create a new payload whose recipient points towards the fallback recipients
payload := make(types.JSONStringMap)
for k, v := range history.Payload {
payload[k] = v
}

if notification.FallbackTeamID != nil {
payload["team_id"] = notification.FallbackTeamID.String()
}
if notification.FallbackPersonID != nil {
payload["person_id"] = notification.FallbackPersonID.String()
}
if notification.FallbackPlaybookID != nil {
payload["playbook_id"] = notification.FallbackPlaybookID.String()
}
if len(notification.CustomServices) != 0 {
payload["custom_service"] = string(notification.FallbackCustomServices)
}

newHistory := NotificationSendHistory{
Payload: payload,
NotificationID: history.NotificationID,
Status: NotificationStatusAttemptingFallback,
FirstObserved: history.FirstObserved,
SourceEvent: history.SourceEvent,
ParentID: &history.ID,
ResourceID: history.ResourceID,
NotBefore: lo.ToPtr(time.Now()),
}

if notification.FallbackDelay != nil {
newHistory.NotBefore = lo.ToPtr(time.Now().Add(*notification.FallbackDelay))
}

return db.Create(&newHistory).Error
}
17 changes: 16 additions & 1 deletion models/playbooks.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ func (p PlaybookRun) End(db *gorm.DB) error {
if p.NotificationSendID != nil {
updates := map[string]any{}
if status == PlaybookRunStatusFailed {
updates["status"] = NotificationStatusCheckingFallback
updates["status"] = NotificationStatusError
updates["error"] = "playbook failed"
} else {
updates["status"] = NotificationStatusSent
Expand All @@ -282,6 +282,21 @@ func (p PlaybookRun) End(db *gorm.DB) error {
if err := db.Model(&NotificationSendHistory{}).Where("id = ?", *p.NotificationSendID).Updates(updates).Error; err != nil {
return err
}

var notif Notification
var sendHistory NotificationSendHistory
if err := db.Where("id = ?", *p.NotificationSendID).First(&sendHistory).Error; err != nil {
return fmt.Errorf("failed to get notification send history: %w", err)
}
if err := db.Where("id = ?", sendHistory.NotificationID).First(&notif).Error; err != nil {
return fmt.Errorf("failed to get notification: %w", err)
}

if notif.HasFallbackSet() {
if err := GenerateFallbackAttempt(db, notif, sendHistory); err != nil {
return fmt.Errorf("failed to generate fallback attempt: %w", err)
}
}
}

return nil
Expand Down

0 comments on commit df4fb8c

Please sign in to comment.