Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
17 changes: 16 additions & 1 deletion src/api/routine/routine.controller.ts
Original file line number Diff line number Diff line change
@@ -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';

Expand Down Expand Up @@ -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);
});
9 changes: 8 additions & 1 deletion src/api/routine/routine.routes.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
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';

Check warning on line 7 in src/api/routine/routine.routes.ts

View workflow job for this annotation

GitHub Actions / Test and Lint

'requireAuth' is defined but never used. Allowed unused vars must match /^_/u
import { validate } from '../../middleware/validate.middleware.js';
import { dailyCheckinSchema } from './routine.validation.js';

Expand All @@ -12,4 +16,7 @@
// router.get('/statistics', requireAuth, getStatisticsHandler);
router.get('/statistics', getStatisticsHandler);

// router.get('/relapses', requireAuth, getRelapsesHandler);
router.get('/relapses', getRelapsesHandler);

export default router;
21 changes: 21 additions & 0 deletions src/api/routine/routine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Loading