From 6aefac4425422564dae54529012cfcd5bfdd7e44 Mon Sep 17 00:00:00 2001 From: salmanabdurrahman Date: Sat, 25 Oct 2025 21:42:23 +0700 Subject: [PATCH 1/2] feat(routine): add endpoint to retrieve user relapse history - implement `findUserRelapses` function in `routine.service.ts` to query relapse data from the database - create `getRelapsesHandler` in `routine.controller.ts` to handle the request and response - add `/relapses` route in `routine.routes.ts` to expose the endpoint --- src/api/routine/routine.controller.ts | 17 ++++++++++++++++- src/api/routine/routine.routes.ts | 9 ++++++++- src/api/routine/routine.service.ts | 21 +++++++++++++++++++++ 3 files changed, 45 insertions(+), 2 deletions(-) diff --git a/src/api/routine/routine.controller.ts b/src/api/routine/routine.controller.ts index dfbc76d..e57b506 100644 --- a/src/api/routine/routine.controller.ts +++ b/src/api/routine/routine.controller.ts @@ -1,5 +1,5 @@ import type { Request, Response } from 'express'; -import { getUserStatistics, processDailyCheckin } from './routine.service.js'; +import { findUserRelapses, getUserStatistics, processDailyCheckin } from './routine.service.js'; import { asyncHandler } from '../../handler/async.handler.js'; import { errorResponse, successResponse } from '../../core/response.js'; @@ -34,3 +34,18 @@ export const getStatisticsHandler = asyncHandler(async (req: Request, res: Respo const statistics = await getUserStatistics(userId); return successResponse(res, 200, 'Statistik berhasil diambil', statistics); }); + +export const getRelapsesHandler = asyncHandler(async (req: Request, res: Response) => { + const userId = req.user?.id || req.body.userId; // Temporary support for userId in body for testing purposes + if (!userId) { + return errorResponse( + res, + 401, + 'Tidak diizinkan', + 'ID pengguna tidak ditemukan dalam permintaan' + ); + } + + const relapses = await findUserRelapses(userId); + return successResponse(res, 200, 'Riwayat relapse berhasil diambil', relapses); +}); diff --git a/src/api/routine/routine.routes.ts b/src/api/routine/routine.routes.ts index b63d3c6..ba3a529 100644 --- a/src/api/routine/routine.routes.ts +++ b/src/api/routine/routine.routes.ts @@ -1,5 +1,9 @@ import { Router } from 'express'; -import { dailyCheckinHandler, getStatisticsHandler } from './routine.controller.js'; +import { + dailyCheckinHandler, + getRelapsesHandler, + getStatisticsHandler, +} from './routine.controller.js'; import { requireAuth } from '../../middleware/auth.middleware.js'; import { validate } from '../../middleware/validate.middleware.js'; import { dailyCheckinSchema } from './routine.validation.js'; @@ -12,4 +16,7 @@ router.post('/checkin', validate(dailyCheckinSchema), dailyCheckinHandler); // router.get('/statistics', requireAuth, getStatisticsHandler); router.get('/statistics', getStatisticsHandler); +// router.get('/relapses', requireAuth, getRelapsesHandler); +router.get('/relapses', getRelapsesHandler); + export default router; diff --git a/src/api/routine/routine.service.ts b/src/api/routine/routine.service.ts index 4c08804..95fdeba 100644 --- a/src/api/routine/routine.service.ts +++ b/src/api/routine/routine.service.ts @@ -132,3 +132,24 @@ export async function getUserStatistics(userId: string) { streakCalendar: streakCalendarDates, }; } + +export async function findUserRelapses(userId: string) { + const relapses = await prisma.checkin.findMany({ + where: { + userId, + isSuccessful: false, + }, + orderBy: { + createdAt: 'desc', + }, + include: { + journal: { + select: { + content: true, + }, + }, + }, + }); + + return relapses; +} From 17f74b65a746c4ede5c7e0da32b2a9f15d97c48f Mon Sep 17 00:00:00 2001 From: salmanabdurrahman Date: Sat, 25 Oct 2025 21:43:17 +0700 Subject: [PATCH 2/2] feat(routine): add endpoint to retrieve user relapse history - add GET /relapses endpoint under /api/v1/routine - implement logic to fetch relapse data from database --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index 3ed0192..26e5133 100644 --- a/README.md +++ b/README.md @@ -208,6 +208,7 @@ Semua endpoint berada di bawah prefix: **`/api/v1`**. Pengaturan rute utama terd - **`/api/v1/routine`**: Rute untuk rutinitas harian dan statistik. - `POST /checkin` - Check-in harian. - `GET /statistics` - Statistik (streak, dll). + - `GET /relapses` - Ambil riwayat relapse pengguna. - **`/api/v1/journals`**: Rute untuk jurnal pribadi pengguna. - `GET /` - Ambil semua entri jurnal.