Skip to content
Snippets Groups Projects
Verified Commit e86cf33b authored by Gwenn Le Bihan's avatar Gwenn Le Bihan :sparkling_heart:
Browse files

fix: test notification was not sent to any subscription

parent 8bf412e6
Branches
Tags v0.11.1
No related merge requests found
Pipeline #18559 passed
......@@ -14,16 +14,9 @@ func (msg Message) ShouldRun() bool {
}
func (msg Message) Run() error {
users, err := Receivers(msg)
subs, users, err := msg.ShouldSendTo()
if err != nil {
return fmt.Errorf("could not determine who to send the notification to: %w", err)
}
ll.Debug("Sending notification for %s on %s to %d users: %v", msg.Event, msg.ChurrosObjectId, len(users), users)
subs, err := subscriptionsOfUsers(users)
if err != nil {
return fmt.Errorf("could not determine which subscriptions to send the notification to: %w", err)
return fmt.Errorf("could not determine what subscriptions to send the notification to: %w", err)
}
if len(subs) == 0 {
......
......@@ -21,6 +21,27 @@ type Subscription struct {
Owner SubscriptionOwner `json:"owner"`
}
func (msg Message) ShouldSendTo() (subs []Subscription, userIds []string, err error) {
if msg.Event == EventTest {
sub, err := prisma.NotificationSubscription.FindUnique(db.NotificationSubscription.ID.Equals(msg.ChurrosObjectId)).With(db.NotificationSubscription.Owner.Fetch()).Exec(context.Background())
return []Subscription{SubscriptionFromDatabase(*sub)}, []string{}, err
}
users, err := Receivers(msg)
if err != nil {
return []Subscription{}, users, fmt.Errorf("could not determine who to send the notification to: %w", err)
}
ll.Debug("Sending notification for %s on %s to %d users: %v", msg.Event, msg.ChurrosObjectId, len(users), users)
subs, err = subscriptionsOfUsers(users)
if err != nil {
return []Subscription{}, users, fmt.Errorf("could not determine which subscriptions to send the notification to: %w", err)
}
return subs, []string{}, nil
}
func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error) {
if err := prisma.Prisma.Connect(); err != nil {
return nil, fmt.Errorf("could not connect to prisma: %w", err)
......@@ -34,7 +55,16 @@ func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error
}
for _, sub := range subs {
subscriptions = append(subscriptions, Subscription{
subscriptions = append(subscriptions, SubscriptionFromDatabase(sub))
}
ll.Debug("Found %d subscriptions for %d users %v", len(subscriptions), len(ids), ids)
return subscriptions, nil
}
func SubscriptionFromDatabase(sub db.NotificationSubscriptionModel) Subscription {
return Subscription{
Webpush: webpush.Subscription{
Endpoint: sub.Endpoint,
Keys: webpush.Keys{
......@@ -48,12 +78,7 @@ func subscriptionsOfUsers(ids []string) (subscriptions []Subscription, err error
FirstName: sub.Owner().FirstName,
LastName: sub.Owner().LastName,
},
})
}
ll.Debug("Found %d subscriptions for %d users %v", len(subscriptions), len(ids), ids)
return subscriptions, nil
}
func (sub Subscription) Destroy() error {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment