eletrotupi / tcc/ api/src/services/insight.service.ts96b6b65fa3a441d97c317cf1c2ee53661ad5b664
774 B | 31 lines Raw
1
import { prisma } from "@app/lib/prisma";
2
import { Insight, InsightType, InsightPeriod } from "@prisma/client";
3

4
export const getInsightsByUserId = async (
5
  userId: number,
6
  params?: {
7
    type?: InsightType;
8
    period?: InsightPeriod;
9
    limit?: number;
10
  },
11
): Promise<Insight[]> => {
12
  return prisma.insight.findMany({
13
    where: {
14
      userId,
15
      ...(params?.type && { type: params.type }),
16
      ...(params?.period && { period: params.period }),
17
    },
18
    orderBy: { generatedAt: "desc" },
19
    take: params?.limit ?? 10,
20
  });
21
};
22

23
export const getLatestInsightByType = async (
24
  userId: number,
25
  type: InsightType,
26
): Promise<Insight | null> => {
27
  return prisma.insight.findFirst({
28
    where: { userId, type },
29
    orderBy: { generatedAt: "desc" },
30
  });
31
};