eletrotupi / tcc/ commit / f6425d4

api: trace pushes back to their notification record

A pushed notification couldn't be tied to the row the app stored, so a
delivery couldn't be checked against the inbox. The payload now carries
the row's id alongside the existing screen and regimen
Pedro Lucas Porcellis porcellis@eletrotupi.com · 11 days ago f6425d4096f950efeafb0cfb4405841fd35f2f8e Browse files
Parents 60e277a
Changed files
2
Additions
+6
Deletions
-1
api/src/services/notification.service.ts
1 1
import { Notification, NotificationKind } from "@prisma/client";
2 2
import { endOfDay } from "date-fns";
3 3

4 4
import { prisma } from "@app/lib/prisma";
5 5
import { APP_TIMEZONE } from "@app/lib/appTime";
81 81

82 82
    return notification;
83 83
  }
84 84

85 85
  const data = {
86 +
    notificationId: notification.id,
86 87
    ...(rule.screen ? { screen: rule.screen } : {}),
87 88
    ...(regimenId !== null ? { regimenId } : {}),
88 89
  };
89 90

90 91
  try {
145 146
  return {
146 147
    notifications,
147 148
    nextCursor: hasMore ? notifications[notifications.length - 1].id : null,
148 149
  };
149 150
};
api/tests/jobs/push-notifications.test.ts
1 1
import { describe, it, expect, afterAll, beforeEach, vi } from 'vitest'
2 2
import { medicineReminderProcessor } from '@app/lib/queue/processors/medicineReminders'
3 3
import { dailyReminderProcessor } from '@app/lib/queue/processors/dailyReminders'
4 4
import { buildPrisma, cleanupTestDb, createTestUser } from '../test-helper'
5 5

64 64
        title: 'Hora de tomar Sertralina',
65 65
        body: '50mg',
66 66
        data: expect.objectContaining({
67 67
          screen: 'medicine-regimens',
68 68
          regimenId: regimen.id,
69 +
          notificationId: row!.id,
69 70
        }),
70 71
      })
71 72
    )
72 73
  })
73 74

141 142
    expect(mockedSend).toHaveBeenCalledOnce()
142 143
    expect(mockedSend).toHaveBeenCalledWith(
143 144
      expect.objectContaining({
144 145
        token: 'ExponentPushToken[daily123]',
145 146
        title: 'Como você está?',
146 -
        data: expect.objectContaining({ screen: 'moods' }),
147 +
        data: expect.objectContaining({
148 +
          screen: 'moods',
149 +
          notificationId: row!.id,
150 +
        }),
147 151
      })
148 152
    )
149 153
  })
150 154

151 155
  it('stores the notification without pushing when notifications are disabled', async () => {
171 175

172 176
    expect(mockedSend).not.toHaveBeenCalled()
173 177
    expect(await db.notification.count()).toBe(0)
174 178
  })
175 179
})