eletrotupi / tcc/ api/src/lib/queue/processors/dailyReminders.ts8194319ceb81e1e0f245796b7019fa74c1babd4e
1.2 KB | 44 lines Raw
1
import { Job } from "bullmq";
2
import { prisma } from "@app/lib/prisma";
3
import { sendPushNotification } from "@app/lib/sendPushNotification";
4
import { DailyReminderPayload } from "@app/lib/queue/types";
5

6
export async function dailyReminderProcessor(job: Job): Promise<void> {
7
  const { userId } = job.data as DailyReminderPayload;
8

9
  console.log(`[daily-reminder] job ${job.id}: user=${userId}`);
10

11
  const user = await prisma.user.findUnique({
12
    where: { id: userId },
13
    select: { pushToken: true, notificationsEnabled: true },
14
  });
15

16
  if (!user) {
17
    console.warn(`[daily-reminder] user ${userId} not found, skipping`);
18

19
    return;
20
  }
21

22
  if (!user.notificationsEnabled) {
23
    console.warn(`[daily-reminder] user ${userId} has notifications disabled, skipping`);
24

25
    return;
26
  }
27

28
  if (!user.pushToken) {
29
    console.warn(`[daily-reminder] user ${userId} has no pushToken, skipping`);
30

31
    return;
32
  }
33

34
  // TODO: These should be a side-effect from a new entry on the notifications
35
  // table
36
  await sendPushNotification({
37
    token: user.pushToken,
38
    title: "Como você está?",
39
    body: "Registre seu humor para acompanhar sua saúde.",
40
    data: { screen: "moods" },
41
  });
42

43
  console.log(`[daily-reminder] sent OK to user ${userId}`);
44
}