api: store/fetch triggers
Parents:
4ef759a8 file(s) changed
- api/prisma/migrations/20260604225204_rename_triggers_enums/migration.sql +14 -0
- api/prisma/schema.prisma +3 -2
- api/src/controllers/triggers.ts +58 -0
- api/src/createApp.ts +2 -0
- api/src/routes/triggers.ts +11 -0
- api/src/schemas/index.ts +5 -0
- api/src/schemas/trigger.schema.ts +12 -0
- api/src/services/trigger.service.ts +69 -0
api/prisma/migrations/20260604225204_rename_triggers_enums/migration.sql
@@ -0,0 +1,14 @@
1 + /*
2 + Warnings:
3 +
4 + - The values [ENVIRONMENTAL,EMOTIONAL] on the enum `TriggerType` will be removed. If these variants are still used in the database, this will fail.
5 +
6 + */
7 + -- AlterEnum
8 + BEGIN;
9 + CREATE TYPE "TriggerType_new" AS ENUM ('SOCIAL', 'WORK', 'HEALTH', 'PHYSICAL', 'FAMILY', 'OTHER');
10 + ALTER TABLE "triggers" ALTER COLUMN "category" TYPE "TriggerType_new" USING ("category"::text::"TriggerType_new");
11 + ALTER TYPE "TriggerType" RENAME TO "TriggerType_old";
12 + ALTER TYPE "TriggerType_new" RENAME TO "TriggerType";
13 + DROP TYPE "public"."TriggerType_old";
14 + COMMIT;
api/prisma/schema.prisma
@@ -127,9 +127,10 @@ }
127 127
128 128 enum TriggerType {
129 129 SOCIAL
130 - ENVIRONMENTAL
131 - EMOTIONAL
130 + WORK
131 + HEALTH
132 132 PHYSICAL
133 + FAMILY
133 134 OTHER
134 135 }
135 136
api/src/controllers/triggers.ts
@@ -0,0 +1,58 @@
1 + import { Request, Response, NextFunction } from 'express';
2 + import { AuthenticatedRequest } from '@app/middleware/auth';
3 + import {
4 + createTrigger,
5 + getTriggersByUserId,
6 + destroyTriggerById
7 + } from '@app/services/trigger.service';
8 +
9 + import {
10 + CreateTriggerSchema
11 + } from '@app/schemas'
12 +
13 + export const TriggersController = {
14 + create: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
15 + try {
16 + const parsed = CreateTriggerSchema.safeParse(req.body.trigger);
17 +
18 + if (!parsed.success) {
19 + return res.status(400).json({
20 + errors: parsed.error!.issues
21 + });
22 + }
23 +
24 + const trigger = await createTrigger(req.userId!, parsed.data);
25 +
26 + return res.status(201).json({ trigger });
27 + } catch (err) {
28 + next(err);
29 + }
30 + },
31 +
32 + index: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
33 + try {
34 + const result = await getTriggersByUserId(Number(req.userId), {
35 + limit: req.query.limit ? Number(req.query.limit) : undefined,
36 + page: req.query.page ? Number(req.query.page) : undefined,
37 + from: req.query.from as string | undefined,
38 + to: req.query.to as string | undefined,
39 + });
40 +
41 + return res.status(200).json(
42 + result
43 + )
44 + } catch (err) {
45 + next(err);
46 + }
47 + },
48 +
49 + destroy: async (req: AuthenticatedRequest, res: Response, next: NextFunction) => {
50 + try {
51 + const result = await destroyTriggerById(req.userId!, Number(req.params.id!));
52 +
53 + return res.status(200).json({})
54 + } catch (err) {
55 + next(err);
56 + }
57 + }
58 + };
api/src/createApp.ts
@@ -6,6 +6,7 @@ import mainRouter from '@app/routes/main';
6 6 import userRouter from '@app/routes/users';
7 7 import authRouter from '@app/routes/auth';
8 8 import moodRouter from '@app/routes/moods';
9 + import triggerRouter from '@app/routes/triggers';
9 10 import sleepRecordRouter from '@app/routes/sleepRecords';
10 11 import { errorHandler } from '@app/middleware/errorHandler';
11 12 import { PrismaClient } from '@prisma/client';
@@ -46,6 +47,7 @@ app.use("/users", userRouter);
46 47 app.use("/auth", authRouter);
47 48 app.use("/moods", moodRouter);
48 49 app.use("/sleep_records", sleepRecordRouter);
50 + app.use("/triggers", triggerRouter);
49 51
50 52 app.use(errorHandler); // always last
51 53
api/src/routes/triggers.ts
@@ -0,0 +1,11 @@
1 + import { Router } from "express";
2 + import { TriggersController } from '@app/controllers/triggers'
3 + import { requireAuth } from '@app/middleware/auth';
4 +
5 + const router = Router();
6 +
7 + router.get('/', requireAuth, TriggersController.index);
8 + router.post('/', requireAuth, TriggersController.create);
9 + router.delete('/:id', requireAuth, TriggersController.destroy);
10 +
11 + export default router;
api/src/schemas/index.ts
@@ -29,3 +29,8 @@ export {
29 29 type CreateSleepRecordInput,
30 30 CreateSleepRecordSchema
31 31 } from '@app/schemas/sleepRecord.schema';
32 +
33 + export {
34 + type CreateTriggerInput,
35 + CreateTriggerSchema
36 + } from '@app/schemas/trigger.schema';
api/src/schemas/trigger.schema.ts
@@ -0,0 +1,12 @@
1 + import { z } from 'zod';
2 + import {
3 + TriggerType
4 + } from '@prisma/client';
5 +
6 + export const CreateTriggerSchema = z.object({
7 + comment: z.string().optional(),
8 + moment: z.coerce.date(),
9 + category: z.nativeEnum(TriggerType)
10 + });
11 +
12 + export type CreateTriggerInput = z.infer<typeof CreateTriggerSchema>;
api/src/services/trigger.service.ts
@@ -0,0 +1,69 @@
1 + import { prisma } from '@app/lib/prisma';
2 + import { Trigger } from '@prisma/client';
3 +
4 + import { CreateTriggerInput } from '@app/schemas';
5 +
6 + export const createTrigger = async (
7 + userId: number, input: CreateTriggerInput
8 + ): Promise<Trigger> => {
9 + return prisma.trigger.create({
10 + data: {
11 + ...input,
12 + userId
13 + }
14 + });
15 + };
16 +
17 + export const getTriggersByUserId = async (
18 + userId: number,
19 + params?: {
20 + limit?: number;
21 + page?: number;
22 + from?: string;
23 + to?: string;
24 + }
25 + ): Promise<{ entries: Trigger[]; total: number; page: number; nextPage: number | null }> => {
26 + const page = params?.page ?? 1;
27 + const limit = params?.limit ?? 20;
28 + const skip = (page - 1) * limit;
29 +
30 + const where = {
31 + userId,
32 + ...(params?.from || params?.to ? {
33 + moment: {
34 + ...(params.from && { gte: new Date(params.from) }),
35 + ...(params.to && { lte: new Date(params.to) }),
36 + }
37 + } : {}),
38 + };
39 +
40 + const [entries, total] = await Promise.all([
41 + prisma.trigger.findMany({
42 + where,
43 + orderBy: { moment: 'desc' },
44 + take: limit,
45 + skip,
46 + }),
47 + prisma.trigger.count({ where }),
48 + ]);
49 +
50 + const totalPages = Math.ceil(total / limit);
51 +
52 + return {
53 + entries,
54 + total,
55 + page,
56 + nextPage: page < totalPages ? page + 1 : null,
57 + };
58 + };
59 +
60 + export const destroyTriggerById = async (
61 + userId: number, id: number
62 + ): Promise<Trigger> => {
63 + return await prisma.trigger.delete({
64 + where: {
65 + id,
66 + userId
67 + }
68 + })
69 + }