| 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";
|
| 6 |
|
| 7 | import { CreateMoodInput } from "@app/schemas";
|
| 8 |
|
| 9 | export const createMood = async (
|
| 10 | userId: number,
|
| 11 | input: CreateMoodInput,
|
| 12 | ): Promise<Mood> => {
|
| 13 | const { moodComponents, ...moodData } = input;
|
| 14 |
|
| 15 | const mood = prisma.mood.create({
|
| 16 | data: {
|
| 17 | ...moodData,
|
| 18 | userId,
|
| 19 | moodComponents: {
|
| 20 | create: moodComponents,
|
| 21 | },
|
| 22 | },
|
| 23 | include: { moodComponents: true },
|
| 24 | });
|
| 25 |
|
| 26 | void enqueueOnDemandInsights(userId);
|
| 27 |
|
| 28 | return mood;
|
| 29 | };
|
| 30 |
|
| 31 | export const getMoodsByUserId = async (
|
| 32 | userId: number,
|
| 33 | params?: {
|
| 34 | limit?: number;
|
| 35 | page?: number;
|
| 36 | from?: string;
|
| 37 | to?: string;
|
| 38 | },
|
| 39 | ): Promise<{
|
| 40 | entries: Mood[];
|
| 41 | total: number;
|
| 42 | page: number;
|
| 43 | nextPage: number | null;
|
| 44 | }> => {
|
| 45 | const page = params?.page ?? 1;
|
| 46 | const limit = params?.limit ?? 20;
|
| 47 | const skip = (page - 1) * limit;
|
| 48 |
|
| 49 | const where = {
|
| 50 | userId,
|
| 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 | : {}),
|
| 59 | };
|
| 60 |
|
| 61 | const [entries, total] = await Promise.all([
|
| 62 | prisma.mood.findMany({
|
| 63 | where,
|
| 64 | include: { moodComponents: true },
|
| 65 | orderBy: { moment: "desc" },
|
| 66 | take: limit,
|
| 67 | skip,
|
| 68 | }),
|
| 69 | prisma.mood.count({ where }),
|
| 70 | ]);
|
| 71 |
|
| 72 | const totalPages = Math.ceil(total / limit);
|
| 73 |
|
| 74 | return {
|
| 75 | entries,
|
| 76 | total,
|
| 77 | page,
|
| 78 | nextPage: page < totalPages ? page + 1 : null,
|
| 79 | };
|
| 80 | };
|
| 81 |
|
| 82 | export const getMoodById = async (id: number): Promise<Mood | null> => {
|
| 83 | return await prisma.mood.findUnique({
|
| 84 | where: {
|
| 85 | id,
|
| 86 | },
|
| 87 | include: { moodComponents: true },
|
| 88 | });
|
| 89 | };
|
| 90 |
|
| 91 | export const destroyMoodById = async (
|
| 92 | userId: number,
|
| 93 | id: number,
|
| 94 | ): Promise<Mood> => {
|
| 95 | return await prisma.mood.delete({
|
| 96 | where: {
|
| 97 | id,
|
| 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 | };
|