Skip to content
Open
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
6 changes: 5 additions & 1 deletion src/common/utils/constants.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import type { ZodString } from "zod";

export const DEFAULT_PAGE = 1;
export const DEFAULT_LIMIT = 10;
export const MAX_LIMIT = 100;
Expand All @@ -8,5 +10,7 @@ export const AUTHORIZATION_ERROR_MESSAGE
export const FORBIDDEN_ERROR_MESSAGE
= "You are not allowed to perform this action.";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For these static messages also better to have them as functions for consistency
Eg: () => 'Error message';

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";
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We already have an unauthorised error message constant.

export const RESOURCE_NOT_FOUND = (resourceName: string, resourceId: ZodString) => `${resourceName} with id ${resourceId} does not exist`;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably have an enum for resourceName.

3 changes: 2 additions & 1 deletion src/modules/activities/activity.handlers.ts
Original file line number Diff line number Diff line change
@@ -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";
Expand All @@ -19,7 +20,7 @@ export const getActivities: AppRouteHandler<TGetActivitiesRoute> = async (
return c.json(
{
success: false,
message: "You are not authorized, please login",
message: AUTHORIZATION_ERROR_MESSAGE,
},
HTTPStatusCodes.UNAUTHORIZED,
);
Expand Down
5 changes: 3 additions & 2 deletions src/modules/activities/activity.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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,
),
},
});
Expand Down
7 changes: 4 additions & 3 deletions src/modules/auth/auth.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -199,7 +200,7 @@ export const logout: AppRouteHandler<TLogoutRoute> = async (c) => {
return c.json(
{
success: false,
message: "You are not authorized, please login",
message: AUTHORIZATION_ERROR_MESSAGE,
},
HTTPStatusCodes.UNAUTHORIZED,
);
Expand Down Expand Up @@ -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,
);
Expand All @@ -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,
);
Expand Down
15 changes: 8 additions & 7 deletions src/modules/auth/auth.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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({
Expand All @@ -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,
),
},
});
Expand Down Expand Up @@ -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,
),
},
});
Expand All @@ -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,
),
},
});
Expand All @@ -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,
),
},
});
Expand Down
3 changes: 2 additions & 1 deletion src/modules/categories/category.handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -35,7 +36,7 @@ export const createCategory: AppRouteHandler<TCreateCategoryRoute> = async (
return c.json(
{
success: false,
message: "You are not authorized, please login",
message: AUTHORIZATION_ERROR_MESSAGE,
},
HTTPStatusCodes.UNAUTHORIZED,
);
Expand Down
17 changes: 9 additions & 8 deletions src/modules/categories/category.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -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({
Expand All @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down
9 changes: 5 additions & 4 deletions src/modules/group/group.routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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";
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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({
Expand Down Expand Up @@ -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),
Expand Down
Loading