From 9c4637528cf2295aa360656bf62c56a8433259be Mon Sep 17 00:00:00 2001 From: louiscklaw Date: Wed, 18 Jun 2025 13:38:14 +0800 Subject: [PATCH] feat: add event count endpoints and improve auth logging with constants --- .../cms_backend/src/app/api/auth/me/route.ts | 3 +-- .../src/app/api/event/helloworld/route.ts | 21 +++++++++++++++++ .../src/app/api/event/helloworld/test.http | 3 +++ .../src/app/api/event/numOfEvent/route.ts | 22 ++++++++++++++++++ .../src/app/api/event/numOfEvent/test.http | 3 +++ .../app/api/party-event/numOfEvent/route.ts | 23 +++++++++++++++++++ .../app/api/party-event/numOfEvent/test.http | 3 +++ .../api/party-user-auth/sign-in/constants.ts | 6 +++++ .../app/api/party-user-auth/sign-in/route.ts | 20 +++++++++------- .../src/app/services/eventItem.service.ts | 12 ++++++++++ 10 files changed, 106 insertions(+), 10 deletions(-) create mode 100644 03_source/cms_backend/src/app/api/event/helloworld/route.ts create mode 100644 03_source/cms_backend/src/app/api/event/helloworld/test.http create mode 100644 03_source/cms_backend/src/app/api/event/numOfEvent/route.ts create mode 100644 03_source/cms_backend/src/app/api/event/numOfEvent/test.http create mode 100644 03_source/cms_backend/src/app/api/party-event/numOfEvent/route.ts create mode 100644 03_source/cms_backend/src/app/api/party-event/numOfEvent/test.http create mode 100644 03_source/cms_backend/src/app/api/party-user-auth/sign-in/constants.ts diff --git a/03_source/cms_backend/src/app/api/auth/me/route.ts b/03_source/cms_backend/src/app/api/auth/me/route.ts index 9c24677..848b274 100644 --- a/03_source/cms_backend/src/app/api/auth/me/route.ts +++ b/03_source/cms_backend/src/app/api/auth/me/route.ts @@ -40,6 +40,7 @@ const INVALID_AUTH_TOKEN = 'Invalid authorization token'; const USER_ID_NOT_FOUND = 'userId not found'; const USER_TOKEN_OK = 'user token check ok'; const AUTHORIZATION_TOKEN_MISSING_OR_INVALID = 'Authorization token missing or invalid'; +const USER_BANNED = 'user banned'; export async function GET(req: NextRequest) { const debug = { 'req.headers': flattenNextjsRequest(req) }; @@ -55,8 +56,6 @@ export async function GET(req: NextRequest) { const accessToken = `${authorization}`.split(' ')[1]; const data = await verify(accessToken, JWT_SECRET); - console.log({ data }); - if (data.userId) { const { userId } = data; diff --git a/03_source/cms_backend/src/app/api/event/helloworld/route.ts b/03_source/cms_backend/src/app/api/event/helloworld/route.ts new file mode 100644 index 0000000..fbfa774 --- /dev/null +++ b/03_source/cms_backend/src/app/api/event/helloworld/route.ts @@ -0,0 +1,21 @@ +import { logger } from 'src/utils/logger'; +import { STATUS, response, handleError } from 'src/utils/response'; + +import { countTotalEvents } from 'src/app/services/eventItem.service'; + +// ---------------------------------------------------------------------- + +/** ************************************** + * GET - Events, obsoleted + *************************************** */ +export async function GET() { + try { + const numOfEvent = await countTotalEvents(); + + logger('[Event] list', numOfEvent); + + return response({ numOfEvent }, STATUS.OK); + } catch (error) { + return handleError('Event - Get list', error); + } +} diff --git a/03_source/cms_backend/src/app/api/event/helloworld/test.http b/03_source/cms_backend/src/app/api/event/helloworld/test.http new file mode 100644 index 0000000..6bc15cf --- /dev/null +++ b/03_source/cms_backend/src/app/api/event/helloworld/test.http @@ -0,0 +1,3 @@ +### + +GET http://localhost:7272/api/event/helloworld diff --git a/03_source/cms_backend/src/app/api/event/numOfEvent/route.ts b/03_source/cms_backend/src/app/api/event/numOfEvent/route.ts new file mode 100644 index 0000000..6dc9282 --- /dev/null +++ b/03_source/cms_backend/src/app/api/event/numOfEvent/route.ts @@ -0,0 +1,22 @@ +import { logger } from 'src/utils/logger'; +import { STATUS, response, handleError } from 'src/utils/response'; + +// src/app/api/event/list/route.ts +import { countTotalEvents, listEvents } from 'src/app/services/eventItem.service'; + +// ---------------------------------------------------------------------- + +/** ************************************** + * GET - Events, obsoleted + *************************************** */ +export async function GET() { + try { + const numOfEvents = await countTotalEvents(); + + // logger('[Event] list', numOfEvents.length); + + return response({ numOfEvents }, STATUS.OK); + } catch (error) { + return handleError('Event - Get list', error); + } +} diff --git a/03_source/cms_backend/src/app/api/event/numOfEvent/test.http b/03_source/cms_backend/src/app/api/event/numOfEvent/test.http new file mode 100644 index 0000000..c307a90 --- /dev/null +++ b/03_source/cms_backend/src/app/api/event/numOfEvent/test.http @@ -0,0 +1,3 @@ +### + +GET http://localhost:7272/api/event/numOfEvent diff --git a/03_source/cms_backend/src/app/api/party-event/numOfEvent/route.ts b/03_source/cms_backend/src/app/api/party-event/numOfEvent/route.ts new file mode 100644 index 0000000..2929f9e --- /dev/null +++ b/03_source/cms_backend/src/app/api/party-event/numOfEvent/route.ts @@ -0,0 +1,23 @@ +// +// +import { logger } from 'src/utils/logger'; +import { STATUS, response, handleError } from 'src/utils/response'; + +import { countTotalEvents } from 'src/app/services/eventItem.service'; + +// ---------------------------------------------------------------------- + +/** ************************************** + * GET - Events, obsoleted + *************************************** */ +export async function GET() { + try { + const numOfEvent = await countTotalEvents(); + + logger('[Event] list', numOfEvent); + + return response({ numOfEvent }, STATUS.OK); + } catch (error) { + return handleError('Event - Get list', error); + } +} diff --git a/03_source/cms_backend/src/app/api/party-event/numOfEvent/test.http b/03_source/cms_backend/src/app/api/party-event/numOfEvent/test.http new file mode 100644 index 0000000..6100db8 --- /dev/null +++ b/03_source/cms_backend/src/app/api/party-event/numOfEvent/test.http @@ -0,0 +1,3 @@ +### + +GET http://localhost:7272/api/party-event/numOfEvent diff --git a/03_source/cms_backend/src/app/api/party-user-auth/sign-in/constants.ts b/03_source/cms_backend/src/app/api/party-user-auth/sign-in/constants.ts new file mode 100644 index 0000000..81aae45 --- /dev/null +++ b/03_source/cms_backend/src/app/api/party-user-auth/sign-in/constants.ts @@ -0,0 +1,6 @@ +export const ERR_USER_NOT_FOUND = 'There is no user corresponding to the email address.'; +export const ERR_WRONG_PASSWORD = 'Wrong password'; +export const LOG_USER_TRIED_LOGIN_WITH_EMAIL = `user tried login with email`; +export const LOG_USER_LOGGED_WITH_WRONG_PASSWORD = 'user logged with wrong password'; +export const LOG_ACCESS_GRANTED = 'access granted'; +export const LOG_ATTEMPTED_LOGIN_BUT_FAILED = 'attempted login but failed'; diff --git a/03_source/cms_backend/src/app/api/party-user-auth/sign-in/route.ts b/03_source/cms_backend/src/app/api/party-user-auth/sign-in/route.ts index 27135ad..9997a19 100644 --- a/03_source/cms_backend/src/app/api/party-user-auth/sign-in/route.ts +++ b/03_source/cms_backend/src/app/api/party-user-auth/sign-in/route.ts @@ -10,9 +10,16 @@ import { createAccessLog } from 'src/app/services/access-log.service'; import prisma from '../../../lib/prisma'; import { flattenNextjsRequest } from './flattenNextjsRequest'; +import { + LOG_USER_TRIED_LOGIN_WITH_EMAIL, + ERR_USER_NOT_FOUND, + LOG_USER_LOGGED_WITH_WRONG_PASSWORD, + ERR_WRONG_PASSWORD, + LOG_ACCESS_GRANTED, + LOG_ATTEMPTED_LOGIN_BUT_FAILED, +} from './constants'; // ---------------------------------------------------------------------- - /** * This API is used for demo purpose only * You should use a real database @@ -21,9 +28,6 @@ import { flattenNextjsRequest } from './flattenNextjsRequest'; * You should not expose the JWT_SECRET in the client side */ -const ERR_USER_NOT_FOUND = 'There is no user corresponding to the email address.'; -const ERR_WRONG_PASSWORD = 'Wrong password'; - export async function POST(req: NextRequest) { const debug = { 'req.headers': flattenNextjsRequest(req) }; @@ -32,12 +36,12 @@ export async function POST(req: NextRequest) { const currentUser = await prisma.partyUser.findFirst({ where: { email } }); if (!currentUser) { - await createAccessLog('', `user tried login with email ${email}`, { debug }); + await createAccessLog('', LOG_USER_TRIED_LOGIN_WITH_EMAIL, { email, debug }); return response({ message: ERR_USER_NOT_FOUND }, STATUS.UNAUTHORIZED); } if (currentUser?.password !== password) { - await createAccessLog(currentUser.id, 'user logged with wrong password', { debug }); + await createAccessLog(currentUser.id, LOG_USER_LOGGED_WITH_WRONG_PASSWORD, { debug }); return response({ message: ERR_WRONG_PASSWORD }, STATUS.UNAUTHORIZED); } @@ -45,11 +49,11 @@ export async function POST(req: NextRequest) { expiresIn: JWT_EXPIRES_IN, }); - await createAccessLog(currentUser.id, 'access granted', { debug }); + await createAccessLog(currentUser.id, LOG_ACCESS_GRANTED, { debug }); return response({ user: currentUser, accessToken }, STATUS.OK); } catch (error) { - await createAccessLog('', 'attempted login but failed', { debug, error }); + await createAccessLog('', LOG_ATTEMPTED_LOGIN_BUT_FAILED, { debug, error }); return handleError('Auth - Sign in', error); } diff --git a/03_source/cms_backend/src/app/services/eventItem.service.ts b/03_source/cms_backend/src/app/services/eventItem.service.ts index 61f067c..e260658 100644 --- a/03_source/cms_backend/src/app/services/eventItem.service.ts +++ b/03_source/cms_backend/src/app/services/eventItem.service.ts @@ -51,6 +51,17 @@ async function getEventItemById(eventId: string): Promise { return prisma.eventItem.findFirst({ where: { id: eventId } }); } +async function countTotalEvents(): Promise { + try { + const result = await prisma.eventItem.findMany(); + console.log({ result }); + return result.length; + } catch (error) { + console.log(error); + return -1; + } +} + // async function createNewEvent(createForm: CreateEvent) { // return prisma.event.create({ data: createForm }); // } @@ -73,4 +84,5 @@ export { // deleteEvent, // createNewEvent, getEventItemById, + countTotalEvents, };