Skip to content

Commit 10af367

Browse files
committed
Fix the deleting of phone notifications
1 parent 90127e5 commit 10af367

4 files changed

Lines changed: 56 additions & 5 deletions

File tree

api/pkg/listeners/phone_notification_listener.go

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,7 @@ func NewNotificationListener(
3838
events.EventTypeMessageNotificationSend: l.onMessageNotificationSend,
3939
events.PhoneHeartbeatMissed: l.onPhoneHeartbeatMissed,
4040
events.UserAccountDeleted: l.onUserAccountDeleted,
41+
events.MessageAPIDeleted: l.onMessageAPIDeleted,
4142
}
4243
}
4344

@@ -167,3 +168,22 @@ func (listener *PhoneNotificationListener) onUserAccountDeleted(ctx context.Cont
167168

168169
return nil
169170
}
171+
172+
// onMessageAPIDeleted handles the events.MessageAPIDeleted event
173+
func (listener *PhoneNotificationListener) onMessageAPIDeleted(ctx context.Context, event cloudevents.Event) error {
174+
ctx, span := listener.tracer.Start(ctx)
175+
defer span.End()
176+
177+
var payload events.MessageAPIDeletedPayload
178+
if err := event.DataAs(&payload); err != nil {
179+
msg := fmt.Sprintf("cannot decode [%s] into [%T]", event.Data(), payload)
180+
return listener.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
181+
}
182+
183+
if err := listener.service.DeleteByMessageID(ctx, payload.UserID, payload.MessageID); err != nil {
184+
msg := fmt.Sprintf("cannot delete [entities.PhoneNotification] for user [%s] and message [%s] on [%s] event with ID [%s]", payload.UserID, payload.MessageID, event.Type(), event.ID())
185+
return listener.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
186+
}
187+
188+
return nil
189+
}

api/pkg/repositories/gorm_phone_notification_repository.go

Lines changed: 19 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -60,12 +60,26 @@ func (repository *gormPhoneNotificationRepository) DeleteAllForUser(
6060
return nil
6161
}
6262

63+
// DeleteByMessageID deletes all entities.PhoneNotification for a user and message ID.
64+
func (repository *gormPhoneNotificationRepository) DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error {
65+
ctx, span := repository.tracer.Start(ctx)
66+
defer span.End()
67+
68+
err := repository.db.WithContext(ctx).
69+
Where("user_id = ? AND message_id = ?", userID, messageID).
70+
Delete(&entities.PhoneNotification{}).Error
71+
if err != nil {
72+
msg := fmt.Sprintf("cannot delete [%T] for user [%s] and message with ID [%s]", &entities.PhoneNotification{}, userID, messageID)
73+
return repository.tracer.WrapErrorSpan(span,
74+
stacktrace.Propagate(err, msg),
75+
)
76+
}
77+
78+
return nil
79+
}
80+
6381
// UpdateStatus updates the status of a phone notification.
64-
func (repository *gormPhoneNotificationRepository) UpdateStatus(
65-
ctx context.Context,
66-
notificationID uuid.UUID,
67-
status entities.PhoneNotificationStatus,
68-
) error {
82+
func (repository *gormPhoneNotificationRepository) UpdateStatus(ctx context.Context, notificationID uuid.UUID, status entities.PhoneNotificationStatus) error {
6983
ctx, span := repository.tracer.Start(ctx)
7084
defer span.End()
7185

api/pkg/repositories/phone_notification_repository.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,4 +22,7 @@ type PhoneNotificationRepository interface {
2222

2323
// DeleteAllForUser deletes all entities.PhoneNotification for a user
2424
DeleteAllForUser(ctx context.Context, userID entities.UserID) error
25+
26+
// DeleteByMessageID deletes entities.PhoneNotification for a message and user
27+
DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error
2528
}

api/pkg/services/phone_notification_service.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,20 @@ func (service *PhoneNotificationService) DeleteAllForUser(ctx context.Context, u
6565
return nil
6666
}
6767

68+
// DeleteByMessageID deletes all entities.PhoneNotification for a user and message ID.
69+
func (service *PhoneNotificationService) DeleteByMessageID(ctx context.Context, userID entities.UserID, messageID uuid.UUID) error {
70+
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)
71+
defer span.End()
72+
73+
if err := service.phoneNotificationRepository.DeleteByMessageID(ctx, userID, messageID); err != nil {
74+
msg := fmt.Sprintf("could not delete [entities.PhoneNotification] for user [%s] and message with ID [%s]", userID, messageID)
75+
return service.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
76+
}
77+
78+
ctxLogger.Info(fmt.Sprintf("deleted [entities.PhoneNotification] for user [%s] and message with ID [%s]", userID, messageID))
79+
return nil
80+
}
81+
6882
// SendHeartbeatFCM sends a heartbeat message so the phone can request a heartbeat
6983
func (service *PhoneNotificationService) SendHeartbeatFCM(ctx context.Context, payload *events.PhoneHeartbeatMissedPayload) error {
7084
ctx, span, ctxLogger := service.tracer.StartWithLogger(ctx, service.logger)

0 commit comments

Comments
 (0)