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