Skip to content

[Backend] Implement Automated Prescription Refill Reminder Scheduler #505

@llinsss

Description

@llinsss

⚠️ This is a backend issue — work is done inside the backend/ folder

Description

PrescriptionsService has a getRefillReminders() method that calculates which prescriptions are due for refill, and a RefillReminder interface is defined. However, this method is never called automatically — reminders are only returned if the frontend explicitly polls the endpoint. Pet owners miss refill windows because no proactive notification is sent.

Current State

  • backend/src/modules/prescriptions/prescriptions.service.tsgetRefillReminders() exists
  • RefillReminder interface is defined with daysUntilRefill and estimatedRefillDate
  • No cron job or BullMQ scheduler calls getRefillReminders()
  • NotificationsService is not injected into PrescriptionsService

What Needs to Be Built

1. Inject NotificationsService into PrescriptionsService

  • Add NotificationsService to PrescriptionsModule imports and constructor

2. Refill Reminder Scheduler (prescriptions/prescription-reminder.scheduler.ts)

@Injectable()
export class PrescriptionReminderScheduler {
  @Cron('0 9 * * *') // Daily at 9am
  async sendRefillReminders() {
    const reminders = await this.prescriptionsService.getRefillReminders();
    for (const reminder of reminders) {
      if (reminder.daysUntilRefill <= 3) {
        await this.notificationsService.create({ ... });
      }
    }
  }
}

3. Notification Content

  • Title: Refill Reminder: {medication}
  • Body: {petName}'s {medication} needs a refill in {daysUntilRefill} days (est. {estimatedRefillDate})
  • Category: MEDICATION
  • Deep link to prescription detail page

4. Deduplication

  • Track sent reminders in Redis with key refill_reminder:{prescriptionId}:{date} TTL 24h
  • Skip if reminder already sent today for this prescription

Acceptance Criteria

  • Reminders are sent daily at 9am for prescriptions due within 3 days
  • Each reminder is sent at most once per day per prescription (deduplication)
  • Notification includes medication name, pet name, and estimated refill date
  • Scheduler is registered in PrescriptionsModule
  • Unit tests cover scheduling logic and deduplication

Files to Create / Modify

  • backend/src/modules/prescriptions/prescription-reminder.scheduler.ts (new)
  • backend/src/modules/prescriptions/prescriptions.module.ts (register scheduler)
  • backend/src/modules/prescriptions/prescriptions.service.ts (inject NotificationsService)
  • backend/src/modules/prescriptions/prescription-reminder.scheduler.spec.ts (new)

Priority

High — refill reminders are calculated but never delivered

Estimated Effort

1–2 days

Metadata

Metadata

Assignees

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions