| 1 | import { prisma } from "@app/lib/prisma";
|
| 2 | import { Mood } from "@prisma/client";
|
| 3 | import {
|
| 4 | rollingPeriod,
|
| 5 | appDayPeriod,
|
| 6 | } from "@app/lib/queue/processors/insights/utils";
|
| 7 | import { InsightJobName } from "@app/lib/queue/types";
|
| 8 | import { getQueue } from "@app/lib/queue";
|
| 9 |
|
| 10 | import { CreateMoodInput } from "@app/schemas";
|
| 11 |
|
| 12 | export const createMood = async (
|
| 13 | userId: number,
|
| 14 | input: CreateMoodInput,
|
| 15 | ): Promise<Mood> => {
|
| 16 | const { moodComponents, ...moodData } = input;
|
| 17 |
|
| 18 | const mood = prisma.mood.create({
|
| 19 | data: {
|
| 20 | ...moodData,
|
| 21 | userId,
|
| 22 | moodComponents: {
|
| 23 | create: moodComponents,
|
| 24 | },
|
| 25 | },
|
| 26 | include: { moodComponents: true },
|
| 27 | });
|
| 28 |
|
| 29 | void enqueueOnDemandInsights(userId);
|
| 30 |
|
| 31 | return mood;
|
| 32 | };
|
| 33 |
|
| 34 | export const getMoodsByUserId = async (
|
| 35 | userId: number,
|
| 36 | params?: {
|
| 37 | limit?: number;
|
| 38 | page?: number;
|
| 39 | from?: string;
|
| 40 | to?: string;
|
| 41 | },
|
| 42 | ): Promise<{
|
| 43 | entries: Mood[];
|
| 44 | total: number;
|
| 45 | page: number;
|
| 46 | nextPage: number | null;
|
| 47 | }> => {
|
| 48 | const page = params?.page ?? 1;
|
| 49 | const limit = params?.limit ?? 20;
|
| 50 | const skip = (page - 1) * limit;
|
| 51 |
|
| 52 | const where = {
|
| 53 | userId,
|
| 54 | ...(params?.from || params?.to
|
| 55 | ? {
|
| 56 | moment: {
|
| 57 | ...(params.from && { gte: new Date(params.from) }),
|
| 58 | ...(params.to && { lte: new Date(params.to) }),
|
| 59 | },
|
| 60 | }
|
| 61 | : {}),
|
| 62 | };
|
| 63 |
|
| 64 | const [entries, total] = await Promise.all([
|
| 65 | prisma.mood.findMany({
|
| 66 | where,
|
| 67 | include: { moodComponents: true },
|
| 68 | orderBy: { moment: "desc" },
|
| 69 | take: limit,
|
| 70 | skip,
|
| 71 | }),
|
| 72 | prisma.mood.count({ where }),
|
| 73 | ]);
|
| 74 |
|
| 75 | const totalPages = Math.ceil(total / limit);
|
| 76 |
|
| 77 | return {
|
| 78 | entries,
|
| 79 | total,
|
| 80 | page,
|
| 81 | nextPage: page < totalPages ? page + 1 : null,
|
| 82 | };
|
| 83 | };
|
| 84 |
|
| 85 | export const getMoodById = async (id: number) => {
|
| 86 | return await prisma.mood.findUnique({
|
| 87 | where: {
|
| 88 | id,
|
| 89 | },
|
| 90 | include: {
|
| 91 | moodComponents: true,
|
| 92 | triggerLinks: {
|
| 93 | include: { trigger: true },
|
| 94 | orderBy: { linkedAt: 'desc' },
|
| 95 | },
|
| 96 | careActions: {
|
| 97 | include: {
|
| 98 | medicineLog: { include: { regimen: true } },
|
| 99 | appointment: true,
|
| 100 | activity: true,
|
| 101 | },
|
| 102 | orderBy: { moment: 'desc' },
|
| 103 | },
|
| 104 | },
|
| 105 | });
|
| 106 | };
|
| 107 |
|
| 108 | export const destroyMoodById = async (
|
| 109 | userId: number,
|
| 110 | id: number,
|
| 111 | ): Promise<Mood> => {
|
| 112 | return await prisma.mood.delete({
|
| 113 | where: {
|
| 114 | id,
|
| 115 | userId,
|
| 116 | },
|
| 117 | });
|
| 118 | };
|
| 119 |
|
| 120 | export const enqueueOnDemandInsights = async (
|
| 121 | userId: number,
|
| 122 | ): Promise<void> => {
|
| 123 | const queue = getQueue("insights");
|
| 124 | const week = { userId, ...rollingPeriod(7) };
|
| 125 | const day = { userId, ...appDayPeriod() };
|
| 126 |
|
| 127 | await queue.addBulk([
|
| 128 | {
|
| 129 | name: InsightJobName.MoodTrend,
|
| 130 | data: week,
|
| 131 | opts: {
|
| 132 | jobId: `mood-trend-${userId}-${new Date().toDateString()}`,
|
| 133 | removeOnComplete: true,
|
| 134 | removeOnFail: true,
|
| 135 | },
|
| 136 | },
|
| 137 | {
|
| 138 | name: InsightJobName.EnergySleep,
|
| 139 | data: week,
|
| 140 | opts: {
|
| 141 | jobId: `energy-sleep-${userId}-${new Date().toDateString()}`,
|
| 142 | removeOnComplete: true,
|
| 143 | removeOnFail: true,
|
| 144 | },
|
| 145 | },
|
| 146 | {
|
| 147 | name: InsightJobName.DailyEnergy,
|
| 148 | data: day,
|
| 149 | opts: {
|
| 150 | jobId: `daily-energy-${userId}-${new Date().toDateString()}`,
|
| 151 | removeOnComplete: true,
|
| 152 | removeOnFail: true,
|
| 153 | },
|
| 154 | },
|
| 155 | ]);
|
| 156 | };
|