Skip to content

Commit

Permalink
fix: set proper status on notification send history
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Sep 25, 2024
1 parent 847f19c commit 81f32a5
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 3 deletions.
1 change: 1 addition & 0 deletions notification/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ func (t *Context) WithRecipientType(recipientType RecipientType) {
}

func (t *Context) WithError(err error) {
t.log.Status = models.NotificationStatusError
if o, ok := oops.AsOops(err); ok {
oopsErr := map[string]any{
"error": o.ToMap(),
Expand Down
2 changes: 2 additions & 0 deletions notification/events.go
Original file line number Diff line number Diff line change
Expand Up @@ -308,6 +308,8 @@ func sendNotifications(ctx context.Context, events models.Events) models.Events
e.SetError(err.Error())
failedEvents = append(failedEvents, e)
notificationContext.WithError(err)
} else {
notificationContext.log.Sent()
}

logs.IfError(notificationContext.EndLog(), "error persisting end of notification send history")
Expand Down
14 changes: 11 additions & 3 deletions notification/notification_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -370,10 +370,18 @@ var _ = ginkgo.Describe("Notifications", ginkgo.Ordered, func() {
})

ginkgo.It("only one notification must have been sent", func() {
var sentHistoryCount int64
err := DefaultContext.DB().Model(&models.NotificationSendHistory{}).Where("notification_id = ?", goodNotif.ID).Count(&sentHistoryCount).Error
var sentHistory []models.NotificationSendHistory
err := DefaultContext.DB().Where("notification_id = ?", goodNotif.ID).Find(&sentHistory).Error
Expect(err).To(BeNil())
Expect(sentHistoryCount).To(Equal(int64(1)))
Expect(len(sentHistory)).To(Equal(1))
Expect(sentHistory[0].Status).To(Equal(models.NotificationStatusSent))
})

ginkgo.It("should correctly set error status", func() {
var sentHistory models.NotificationSendHistory
err := DefaultContext.DB().Where("notification_id = ?", badNotif.ID).Find(&sentHistory).Error
Expect(err).To(BeNil())
Expect(sentHistory.Status).To(Equal(models.NotificationStatusError))
})
})

Expand Down

0 comments on commit 81f32a5

Please sign in to comment.