Skip to content

Batch achievement notifications query never matches (uses unlockedAt = now, ignores today boundary) #1391

Description

@RUKAYAT-CODER

Overview

AchievementsNotificationsService.sendBatchNotifications() in src/achievements/achievements-notifications.service.ts is meant to send notifications for achievements unlocked today. It computes a midnight boundary:

const today = new Date();
today.setHours(0, 0, 0, 0);

const achievements = await this.userAchievementRepository.find({
  where: {
    unlockedAt: new Date(),        // <-- exact current timestamp
    notificationSent: false,
  },
  relations: ['user', 'achievement'],
});

There are two problems:

  1. The filter never matches. unlockedAt: new Date() compares unlockedAt for exact equality with the current instant (millisecond precision), so it will essentially never match any stored row. The computed today boundary is never used — the intent was clearly a range filter such as MoreThanOrEqual(today).
  2. The query is unbounded. Unlike resendFailedNotifications() (which uses take: 100 to batch), this query has no limit, so once the filter is fixed it could load an unbounded number of rows and send them all in a single tight loop.

The net effect is that scheduled batch achievement notifications are silently never sent.

Specifications

Features:

  • The batch job selects achievements unlocked within the intended window (e.g. since midnight today) that have not yet been notified.
  • The batch is processed in bounded chunks.

Tasks:

  • Fix the where clause to use a range predicate against the computed boundary (e.g. unlockedAt: MoreThanOrEqual(today)) instead of unlockedAt: new Date().
  • Add a take limit (and loop/paginate) so the batch is processed in bounded chunks like resendFailedNotifications().
  • Add a unit test asserting that an achievement unlocked earlier today with notificationSent = false is selected and notified.

Impacted Files:

  • src/achievements/achievements-notifications.service.ts
  • src/achievements/achievements-notifications.service.spec.ts (new/updated test)

Acceptance Criteria

  • Achievements unlocked during the intended window with notificationSent = false are selected and notified.
  • The batch query is bounded and processes large backlogs in chunks.
  • A regression test covers the corrected date filter.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Stellar WaveIssues in the Stellar wave program

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions