diff --git a/internal/repository/postgres_device.go b/internal/repository/postgres_device.go index 440e3ce..c146471 100644 --- a/internal/repository/postgres_device.go +++ b/internal/repository/postgres_device.go @@ -90,7 +90,9 @@ func (p *postgresDeviceRepository) GetInboxNotifiableByAccountID(ctx context.Con SELECT devices.id, apns_token, sandbox, active_until, grace_period_until FROM devices INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id - WHERE devices_accounts.account_id = $1 AND devices_accounts.inbox_notifiable = TRUE` + WHERE devices_accounts.account_id = $1 AND + devices_accounts.inbox_notifiable = TRUE AND + grace_period_until > NOW()` return p.fetch(ctx, query, id) } @@ -100,7 +102,9 @@ func (p *postgresDeviceRepository) GetWatcherNotifiableByAccountID(ctx context.C SELECT devices.id, apns_token, sandbox, active_until, grace_period_until FROM devices INNER JOIN devices_accounts ON devices.id = devices_accounts.device_id - WHERE devices_accounts.account_id = $1 AND devices_accounts.watcher_notifiable = TRUE` + WHERE devices_accounts.account_id = $1 AND + devices_accounts.watcher_notifiable = TRUE AND + grace_period_until > NOW()` return p.fetch(ctx, query, id) } diff --git a/internal/worker/notifications.go b/internal/worker/notifications.go index 0da3249..d03d107 100644 --- a/internal/worker/notifications.go +++ b/internal/worker/notifications.go @@ -314,6 +314,13 @@ func (nc *notificationsConsumer) Consume(delivery rmq.Delivery) { return } + if len(devices) == 0 { + nc.logger.WithFields(logrus.Fields{ + "account#username": account.NormalizedUsername(), + }).Debug("no notifiable devices, finishing job") + return + } + // Iterate backwards so we notify from older to newer for i := msgs.Count - 1; i >= 0; i-- { msg := msgs.Children[i]