| 1 | import { prisma } from '@app/lib/prisma';
|
| 2 | import { SleepRecord } from '@prisma/client';
|
| 3 |
|
| 4 | import { CreateSleepRecordInput, UpdateSleepRecordInput } from '@app/schemas';
|
| 5 | import {
|
| 6 | rollingPeriod,
|
| 7 | appDayPeriod,
|
| 8 | } from "@app/lib/queue/processors/insights/utils";
|
| 9 | import { InsightJobName } from "@app/lib/queue/types";
|
| 10 | import { getQueue } from "@app/lib/queue";
|
| 11 |
|
| 12 | export const createSleepRecord = async (
|
| 13 | userId: number, input: CreateSleepRecordInput
|
| 14 | ): Promise<SleepRecord> => {
|
| 15 | const sleepRecord = await prisma.sleepRecord.create({
|
| 16 | data: {
|
| 17 | ...input,
|
| 18 | userId
|
| 19 | }
|
| 20 | });
|
| 21 |
|
| 22 | void enqueueEnergySleepInsights(userId);
|
| 23 |
|
| 24 | return sleepRecord;
|
| 25 | };
|
| 26 |
|
| 27 | export const getSleepRecordsByUserId = async (
|
| 28 | userId: number,
|
| 29 | params?: {
|
| 30 | limit?: number;
|
| 31 | page?: number;
|
| 32 | from?: string;
|
| 33 | to?: string;
|
| 34 | }
|
| 35 | ): Promise<{ entries: SleepRecord[]; total: number; page: number; nextPage: number | null }> => {
|
| 36 | const page = params?.page ?? 1;
|
| 37 | const limit = params?.limit ?? 20;
|
| 38 | const skip = (page - 1) * limit;
|
| 39 |
|
| 40 | const where = {
|
| 41 | userId,
|
| 42 | ...(params?.from || params?.to ? {
|
| 43 | date: {
|
| 44 | ...(params.from && { gte: new Date(params.from) }),
|
| 45 | ...(params.to && { lte: new Date(params.to) }),
|
| 46 | }
|
| 47 | } : {}),
|
| 48 | };
|
| 49 |
|
| 50 | const [entries, total] = await Promise.all([
|
| 51 | prisma.sleepRecord.findMany({
|
| 52 | where,
|
| 53 | orderBy: { date: 'desc' },
|
| 54 | take: limit,
|
| 55 | skip,
|
| 56 | }),
|
| 57 | prisma.sleepRecord.count({ where }),
|
| 58 | ]);
|
| 59 |
|
| 60 | const totalPages = Math.ceil(total / limit);
|
| 61 |
|
| 62 | return {
|
| 63 | entries,
|
| 64 | total,
|
| 65 | page,
|
| 66 | nextPage: page < totalPages ? page + 1 : null,
|
| 67 | };
|
| 68 | };
|
| 69 |
|
| 70 | export const destroySleepRecordById = async (
|
| 71 | userId: number, id: number
|
| 72 | ): Promise<SleepRecord> => {
|
| 73 | return await prisma.sleepRecord.delete({
|
| 74 | where: {
|
| 75 | id,
|
| 76 | userId
|
| 77 | }
|
| 78 | })
|
| 79 | }
|
| 80 |
|
| 81 | export const getSleepRecordById = async (userId: number, id: number): Promise<SleepRecord | null> => {
|
| 82 | return await prisma.sleepRecord.findUnique({
|
| 83 | where: { userId, id }
|
| 84 | })
|
| 85 | }
|
| 86 |
|
| 87 | export const updateSleepRecordById = async (
|
| 88 | userId: number, id: number, input: UpdateSleepRecordInput
|
| 89 | ): Promise<SleepRecord> => {
|
| 90 | const sleepRecord = await prisma.sleepRecord.update({
|
| 91 | where: {
|
| 92 | id,
|
| 93 | userId
|
| 94 | },
|
| 95 | data: input
|
| 96 | });
|
| 97 |
|
| 98 | void enqueueEnergySleepInsights(userId);
|
| 99 |
|
| 100 | return sleepRecord;
|
| 101 | }
|
| 102 |
|
| 103 | async function enqueueEnergySleepInsights(userId: number): Promise<void> {
|
| 104 | const queue = getQueue("insights");
|
| 105 | const week = { userId, ...rollingPeriod(7) };
|
| 106 | const day = { userId, ...appDayPeriod() };
|
| 107 |
|
| 108 | await queue.addBulk([
|
| 109 | {
|
| 110 | name: InsightJobName.EnergySleep,
|
| 111 | data: week,
|
| 112 | opts: {
|
| 113 | jobId: `energy-sleep-${userId}-${new Date().toDateString()}`,
|
| 114 | removeOnComplete: true,
|
| 115 | removeOnFail: true,
|
| 116 | }
|
| 117 | },
|
| 118 | {
|
| 119 | name: InsightJobName.DailySleep,
|
| 120 | data: day,
|
| 121 | opts: {
|
| 122 | jobId: `daily-sleep-${userId}-${new Date().toDateString()}`,
|
| 123 | removeOnComplete: true,
|
| 124 | removeOnFail: true,
|
| 125 | },
|
| 126 | },
|
| 127 | ]);
|
| 128 | }
|