eletrotupi / tcc/ api/src/lib/queue/processors/dailyReminders.tsea1475564abe0f26ea6bb9c6c840b714261aad91
1.3 KB | 45 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
import { logger } from "@app/lib/logger";
6

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

10
  logger.info({ jobId: job.id, userId }, "[daily-reminder] job started");
11

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

17
  if (!user) {
18
    logger.warn({ userId }, "[daily-reminder] user not found, skipping");
19

20
    return;
21
  }
22

23
  if (!user.notificationsEnabled) {
24
    logger.warn({ userId }, "[daily-reminder] notifications disabled, skipping");
25

26
    return;
27
  }
28

29
  if (!user.pushToken) {
30
    logger.warn({ userId }, "[daily-reminder] no pushToken, skipping");
31

32
    return;
33
  }
34

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

44
  logger.info({ userId }, "[daily-reminder] push sent");
45
}