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