diff --git a/src/common/utils/constants.ts b/src/common/utils/constants.ts index 9c68651..c7f2f1b 100644 --- a/src/common/utils/constants.ts +++ b/src/common/utils/constants.ts @@ -1,3 +1,5 @@ +import type { ZodString } from "zod"; + export const DEFAULT_PAGE = 1; export const DEFAULT_LIMIT = 10; export const MAX_LIMIT = 100; @@ -8,5 +10,7 @@ export const AUTHORIZATION_ERROR_MESSAGE export const FORBIDDEN_ERROR_MESSAGE = "You are not allowed to perform this action."; export const INTERNAL_SERVER_ERROR_MESSAGE - = "Something went wrong, please try again later"; + = "Something went wrong, please try again later."; export const VALIDATION_ERROR_MESSAGE = "The validation error(s)"; +export const NOT_AUTHORIZED = "User is not authorized"; +export const RESOURCE_NOT_FOUND = (resourceName: string, resourceId: ZodString) => `${resourceName} with id ${resourceId} does not exist`; diff --git a/src/modules/activities/activity.handlers.ts b/src/modules/activities/activity.handlers.ts index dca794b..874e4d2 100644 --- a/src/modules/activities/activity.handlers.ts +++ b/src/modules/activities/activity.handlers.ts @@ -1,6 +1,7 @@ import { AuthRoles } from "@/common/enums"; import { generateMetadata } from "@/common/helpers/metadata.helper"; import type { AppRouteHandler } from "@/common/lib/types"; +import { AUTHORIZATION_ERROR_MESSAGE } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; import { getActivitiesRepository } from "./activity.repository"; @@ -19,7 +20,7 @@ export const getActivities: AppRouteHandler = async ( return c.json( { success: false, - message: "You are not authorized, please login", + message: AUTHORIZATION_ERROR_MESSAGE, }, HTTPStatusCodes.UNAUTHORIZED, ); diff --git a/src/modules/activities/activity.routes.ts b/src/modules/activities/activity.routes.ts index f28ef93..eea08b3 100644 --- a/src/modules/activities/activity.routes.ts +++ b/src/modules/activities/activity.routes.ts @@ -8,6 +8,7 @@ import { } from "@/common/middlewares/auth.middleware"; import createErrorSchema from "@/common/schema/create-error.schema"; import { metadataSchema } from "@/common/schema/metadata.schema"; +import { AUTHORIZATION_ERROR_MESSAGE, VALIDATION_ERROR_MESSAGE } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; import { activityQuerySchema } from "./activity.schema"; @@ -42,11 +43,11 @@ export const getActivitiesRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), [HTTPStatusCodes.UNPROCESSABLE_ENTITY]: jsonContent( createErrorSchema(activityQuerySchema), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), }, }); diff --git a/src/modules/auth/auth.handlers.ts b/src/modules/auth/auth.handlers.ts index af8245b..a20cf0a 100644 --- a/src/modules/auth/auth.handlers.ts +++ b/src/modules/auth/auth.handlers.ts @@ -2,6 +2,7 @@ import { deleteCookie, setCookie } from "hono/cookie"; import { AuthRoles } from "@/common/enums"; import type { AppRouteHandler } from "@/common/lib/types"; +import { AUTHORIZATION_ERROR_MESSAGE } from "@/common/utils/constants"; import { hashPassword, verifyPasswordHash, @@ -199,7 +200,7 @@ export const logout: AppRouteHandler = async (c) => { return c.json( { success: false, - message: "You are not authorized, please login", + message: AUTHORIZATION_ERROR_MESSAGE, }, HTTPStatusCodes.UNAUTHORIZED, ); @@ -233,7 +234,7 @@ export const loggedInUserDetails: AppRouteHandler< return c.json( { success: false, - message: "You are not authorized, please login", + message: AUTHORIZATION_ERROR_MESSAGE, }, HTTPStatusCodes.UNAUTHORIZED, ); @@ -247,7 +248,7 @@ export const loggedInUserDetails: AppRouteHandler< return c.json( { success: false, - message: "You are not authorized, please login", + message: AUTHORIZATION_ERROR_MESSAGE, }, HTTPStatusCodes.UNAUTHORIZED, ); diff --git a/src/modules/auth/auth.routes.ts b/src/modules/auth/auth.routes.ts index 939d1d2..7afba2c 100644 --- a/src/modules/auth/auth.routes.ts +++ b/src/modules/auth/auth.routes.ts @@ -7,6 +7,7 @@ import { requireAuth, } from "@/common/middlewares/auth.middleware"; import createErrorSchema from "@/common/schema/create-error.schema"; +import { AUTHORIZATION_ERROR_MESSAGE, INTERNAL_SERVER_ERROR_MESSAGE, VALIDATION_ERROR_MESSAGE } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; import { insertUserSchema, selectUserSchema } from "@/db/schemas/user.model"; @@ -48,7 +49,7 @@ export const registerRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.CONFLICT]: jsonContent( z.object({ @@ -63,14 +64,14 @@ export const registerRoute = createRoute({ password: z.string().min(8).max(60), }), ), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "Server side error(s)", + INTERNAL_SERVER_ERROR_MESSAGE, ), }, }); @@ -112,14 +113,14 @@ export const loginRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "Server side error(s)", + INTERNAL_SERVER_ERROR_MESSAGE, ), }, }); @@ -142,7 +143,7 @@ export const logoutRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), }, }); @@ -166,7 +167,7 @@ export const loggedinUserDetails = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), }, }); diff --git a/src/modules/categories/category.handlers.ts b/src/modules/categories/category.handlers.ts index d999d70..a92f716 100644 --- a/src/modules/categories/category.handlers.ts +++ b/src/modules/categories/category.handlers.ts @@ -2,6 +2,7 @@ import { ActivityType, AuthRoles } from "@/common/enums"; import { logActivity } from "@/common/helpers/activity-log.helper"; import { generateMetadata } from "@/common/helpers/metadata.helper"; import type { AppRouteHandler } from "@/common/lib/types"; +import { AUTHORIZATION_ERROR_MESSAGE } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; import type { TSelectCategorySchema } from "@/db/schemas/category.model"; @@ -35,7 +36,7 @@ export const createCategory: AppRouteHandler = async ( return c.json( { success: false, - message: "You are not authorized, please login", + message: AUTHORIZATION_ERROR_MESSAGE, }, HTTPStatusCodes.UNAUTHORIZED, ); diff --git a/src/modules/categories/category.routes.ts b/src/modules/categories/category.routes.ts index c5afec4..409cf98 100644 --- a/src/modules/categories/category.routes.ts +++ b/src/modules/categories/category.routes.ts @@ -11,6 +11,7 @@ import { } from "@/common/middlewares/auth.middleware"; import createErrorSchema from "@/common/schema/create-error.schema"; import { metadataSchema } from "@/common/schema/metadata.schema"; +import { AUTHORIZATION_ERROR_MESSAGE, FORBIDDEN_ERROR_MESSAGE, VALIDATION_ERROR_MESSAGE } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; import { insertCategorySchema, @@ -55,14 +56,14 @@ export const createCategoryRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.UNAUTHORIZED]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), [HTTPStatusCodes.CONFLICT]: jsonContent( z.object({ @@ -73,7 +74,7 @@ export const createCategoryRoute = createRoute({ ), [HTTPStatusCodes.UNPROCESSABLE_ENTITY]: jsonContent( createErrorSchema(insertCategorySchema), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( z.object({ @@ -181,21 +182,21 @@ export const updateCategoryRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "The validation error(s)", + VALIDATION_ERROR_MESSAGE, ), [HTTPStatusCodes.UNAUTHORIZED]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), [HTTPStatusCodes.FORBIDDEN]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "You are not allowed to perform this action", + FORBIDDEN_ERROR_MESSAGE, ), [HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( z.object({ @@ -242,14 +243,14 @@ export const deleteCategoryRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "You are not authorized, please login", + AUTHORIZATION_ERROR_MESSAGE, ), [HTTPStatusCodes.FORBIDDEN]: jsonContent( z.object({ success: z.boolean().default(false), message: z.string(), }), - "You are not allowed to perform this action", + FORBIDDEN_ERROR_MESSAGE, ), [HTTPStatusCodes.INTERNAL_SERVER_ERROR]: jsonContent( z.object({ diff --git a/src/modules/group/group.routes.ts b/src/modules/group/group.routes.ts index 778576f..2f7e016 100644 --- a/src/modules/group/group.routes.ts +++ b/src/modules/group/group.routes.ts @@ -12,6 +12,7 @@ import { AUTHORIZATION_ERROR_MESSAGE, FORBIDDEN_ERROR_MESSAGE, INTERNAL_SERVER_ERROR_MESSAGE, + RESOURCE_NOT_FOUND, VALIDATION_ERROR_MESSAGE, } from "@/common/utils/constants"; import * as HTTPStatusCodes from "@/common/utils/http-status-codes.util"; @@ -127,7 +128,7 @@ export const getGroupById = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "Group with id does not exist", + RESOURCE_NOT_FOUND("group", idSchema), ), [HTTPStatusCodes.UNAUTHORIZED]: jsonContent( z.object({ @@ -186,7 +187,7 @@ export const updateGroupRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "Group with id does not exist", + RESOURCE_NOT_FOUND("group", idSchema), ), [HTTPStatusCodes.UNAUTHORIZED]: jsonContent( z.object({ @@ -235,7 +236,7 @@ export const deleteGroupRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "Group with id does not exist", + RESOURCE_NOT_FOUND("group", idSchema), ), [HTTPStatusCodes.UNAUTHORIZED]: jsonContent( z.object({ @@ -308,7 +309,7 @@ export const addUsersToGroupRoute = createRoute({ success: z.boolean().default(false), message: z.string(), }), - "Group with id does not exist", + RESOURCE_NOT_FOUND("group", idSchema), ), [HTTPStatusCodes.UNPROCESSABLE_ENTITY]: jsonContent( createErrorSchema(insertGroupSchema),