eletrotupi / tcc/ commit / a772f94

api: enqueue insight after creating a mood entry

Pedro Lucas Porcellis porcellis@eletrotupi.com · 4 months ago a772f94df9c0c9031e8dab16598a675e26df835e Browse files
Parents 456b19f
Changed files
1
Additions
+74
Deletions
-27
api/src/services/mood.service.ts
1 -
import { prisma } from '@app/lib/prisma';
2 -
import { Mood } from '@prisma/client';
1 +
import { prisma } from "@app/lib/prisma";
2 +
import { Mood } from "@prisma/client";
3 +
import { rollingPeriod } from "@app/lib/queue/processors/insights/utils";
4 +
import { InsightJobName } from "@app/lib/queue/types";
5 +
import { getQueue } from "@app/lib/queue";
3 6

4 -
import { CreateMoodInput } from '@app/schemas';
7 +
import { CreateMoodInput } from "@app/schemas";
5 8

6 -
export const createMood = async (userId: number, input: CreateMoodInput): Promise<Mood> => {
9 +
export const createMood = async (
10 +
  userId: number,
11 +
  input: CreateMoodInput,
12 +
): Promise<Mood> => {
7 13
  const { moodComponents, ...moodData } = input;
8 14

9 -
  return prisma.mood.create({
15 +
  const mood = prisma.mood.create({
10 16
    data: {
11 17
      ...moodData,
12 18
      userId,
13 19
      moodComponents: {
14 -
        create: moodComponents
15 -
      }
20 +
        create: moodComponents,
21 +
      },
16 22
    },
17 -
    include: { moodComponents: true }
23 +
    include: { moodComponents: true },
18 24
  });
25 +

26 +
  void enqueueOnDemandInsights(userId);
27 +

28 +
  return mood;
19 29
};
20 30

21 31
export const getMoodsByUserId = async (
22 32
  userId: number,
23 33
  params?: {
24 34
    limit?: number;
25 35
    page?: number;
26 36
    from?: string;
27 37
    to?: string;
28 -
  }
29 -
): Promise<{ entries: Mood[]; total: number; page: number; nextPage: number | null }> => {
30 -
  const page  = params?.page  ?? 1;
38 +
  },
39 +
): Promise<{
40 +
  entries: Mood[];
41 +
  total: number;
42 +
  page: number;
43 +
  nextPage: number | null;
44 +
}> => {
45 +
  const page = params?.page ?? 1;
31 46
  const limit = params?.limit ?? 20;
32 -
  const skip  = (page - 1) * limit;
47 +
  const skip = (page - 1) * limit;
33 48

34 49
  const where = {
35 50
    userId,
36 -
    ...(params?.from || params?.to ? {
37 -
      moment: {
38 -
        ...(params.from && { gte: new Date(params.from) }),
39 -
        ...(params.to   && { lte: new Date(params.to)   }),
40 -
      }
41 -
    } : {}),
51 +
    ...(params?.from || params?.to
52 +
      ? {
53 +
          moment: {
54 +
            ...(params.from && { gte: new Date(params.from) }),
55 +
            ...(params.to && { lte: new Date(params.to) }),
56 +
          },
57 +
        }
58 +
      : {}),
42 59
  };
43 60

44 61
  const [entries, total] = await Promise.all([
45 62
    prisma.mood.findMany({
46 63
      where,
47 64
      include: { moodComponents: true },
48 -
      orderBy: { moment: 'desc' },
65 +
      orderBy: { moment: "desc" },
49 66
      take: limit,
50 67
      skip,
51 68
    }),
52 69
    prisma.mood.count({ where }),
53 70
  ]);
63 80
};
64 81

65 82
export const getMoodById = async (id: number): Promise<Mood | null> => {
66 83
  return await prisma.mood.findUnique({
67 84
    where: {
68 -
      id
85 +
      id,
69 86
    },
70 -
    include: { moodComponents: true }
87 +
    include: { moodComponents: true },
71 88
  });
72 -
}
89 +
};
73 90

74 -
export const destroyMoodById = async (userId: number, id: number): Promise<Mood> => {
91 +
export const destroyMoodById = async (
92 +
  userId: number,
93 +
  id: number,
94 +
): Promise<Mood> => {
75 95
  return await prisma.mood.delete({
76 96
    where: {
77 97
      id,
78 -
      userId
79 -
    }
80 -
  })
81 -
}
98 +
      userId,
99 +
    },
100 +
  });
101 +
};
102 +

103 +
export const enqueueOnDemandInsights = async (
104 +
  userId: number,
105 +
): Promise<void> => {
106 +
  const queue = getQueue("insights");
107 +
  const period = rollingPeriod(7);
108 +
  const payload = { userId, ...period };
109 +

110 +
  await queue.addBulk([
111 +
    {
112 +
      name: InsightJobName.MoodTrend,
113 +
      data: payload,
114 +
      opts: {
115 +
        jobId: `mood-trend-${userId}-${new Date().toDateString()}`,
116 +
        removeOnComplete: true,
117 +
      },
118 +
    },
119 +
    {
120 +
      name: InsightJobName.EnergySleep,
121 +
      data: payload,
122 +
      opts: {
123 +
        jobId: `energy-sleep-${userId}-${new Date().toDateString()}`,
124 +
        removeOnComplete: true,
125 +
      },
126 +
    },
127 +
  ]);
128 +
};