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