Skip to content

feat: enhance user and order constants for improved clarity and funct…#48

Open
ammar-codeable wants to merge 1 commit into
mainfrom
feat-convert-all-error-response-strings-to-constants
Open

feat: enhance user and order constants for improved clarity and funct…#48
ammar-codeable wants to merge 1 commit into
mainfrom
feat-convert-all-error-response-strings-to-constants

Conversation

@ammar-codeable
Copy link
Copy Markdown
Collaborator

@ammar-codeable ammar-codeable commented Mar 29, 2025

…ionality

Summary by CodeRabbit

  • New Features

    • Added enhanced order retrieval, allowing users to view their complete order history.
  • Bug Fixes

    • Corrected minor discrepancies in payment and user feedback messages for improved clarity.
  • Refactor

    • Standardized status and error notifications in canteen, payment, and account operations to ensure consistent and reliable messaging.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai Bot commented Mar 29, 2025

Walkthrough

The pull request refactors multiple controller files by reordering import/export statements and reformatting code to improve readability. In addition, hardcoded messages have been replaced with constants across the admin, canteen, payment, and user controllers. New constants have been added to centralize error and status messages. A new method (getAllOrders) has been introduced in the user controller. The changes in the constants packages remove outdated constants and add new, categorized ones for a more consistent approach to response handling.

Changes

File(s) Change Summary
apps/.../adminController.ts Reordered import and export statements; standardized formatting and indentation; no change to functionality.
apps/.../canteenController.ts Introduced new constants for canteen operations (e.g., CANTEEN_CLOSED, CANTEEN_OPENED, etc.); updated response messages to use these constants for error handling.
apps/.../paymentController.ts Added new constants for error handling in checkout and payment verification (e.g., ITEMS_UNAVAILABLE, CHECKOUT_FAILED, etc.); fixed typos in error messages; rearranged the export order.
apps/.../userController.ts Replaced hardcoded response messages with new constants (e.g., LOGOUT_SUCCESS, PASSWORD_CHANGE_SUCCESS, etc.); improved error handling; added a new method, getAllOrders, to enhance order retrieval functionality.
packages/constants/src/index.ts Removed legacy constants and added multiple new ones, organized by category (canteen, payment, etc.) for better consistency across the application.
packages/constants/src/userConstants.ts Updated existing constants (e.g., corrected OTP_VERIFICATION_SUCCESSFUL and USER_NOT_AUTHORISED messages); added new constants for password management, account actions, payment, and order handling to improve clarity and maintainability.

Sequence Diagram(s)

sequenceDiagram
    participant U as User
    participant UC as UserController
    participant DB as Database

    U->>UC: Request all orders
    UC->>DB: Query orders from database
    DB-->>UC: Return order data
    UC-->>U: Respond with orders using updated constants
Loading

Poem

I'm a rabbit in a code garden so neat,
Hopping through changes with a happy beat,
Imports and constants now perfectly align,
Reordered exports make our code divine.
With a twitch of my nose, I celebrate this treat –
A well-refined codebase so clever and sweet!
🐇💻

✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai plan to trigger planning for file edits and PR creation.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai Bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 2

🧹 Nitpick comments (7)
apps/http/src/controllers/adminController.ts (2)

174-180: Use optional chaining to prevent potential null reference errors.

The static analysis tool flagged this code. Using optional chaining would make the code more robust by gracefully handling the case where order.customer might be null.

-if (order.customer && order.customer.fcmToken) {
+if (order.customer?.fcmToken) {
🧰 Tools
🪛 Biome (1.9.4)

[error] 174-174: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)


89-89: Consider fixing typo in function name.

Although this appears to be pre-existing code, the function name "chageToPickup" has a typo (missing 'n'). Consider renaming it to "changeToPickup" in a future PR.

apps/http/src/controllers/userController.ts (2)

710-764: Good implementation of the getAllOrders function.

The new getAllOrders function provides paginated access to user orders with proper error handling and validation.

Consider clarifying the take limit calculation:

-take: 5 + 5,
+take: 10, // 5 items per page + 5 extra to check if there are more

This makes the intention clearer that you're fetching 10 items to check if there are more than 5.


682-682: Consider using a more specific error message.

The error message ACTIVE_ORDERS_EXIST is used when a replacement user is not found, which doesn't accurately describe the error condition. Consider using a more specific constant for this error.

-return res.status(400).json({ message: ACTIVE_ORDERS_EXIST });
+return res.status(400).json({ message: "Replacement user not found" }); // Consider adding a specific constant for this error
apps/http/src/controllers/canteenController.ts (1)

296-296: Consider fixing typo in function name.

Although this appears to be pre-existing code, the function name "getCanteenAvilabality" has a typo. Consider renaming it to "getCanteenAvailability" in a future PR.

packages/constants/src/index.ts (1)

6-56: Improve organization of exported constants to match categorization in userConstants.ts

The exported constants are organized with some category comments, but the structure doesn't fully match the clear categorization in userConstants.ts. Consider reorganizing the exports to follow the same grouping as in the source file (Payment, Canteen, and Order categories).

For example, instead of having scattered comments like at lines 6, 11, 16, and 24, consider grouping all constants by their category with clear section headers.

export {
	ACCOUNT_DELETED,
	ACTIVE_ORDERS_EXIST,
	BROADCAST_QUANTITY,
-	CANTEEN_CLOSED,
-	// Canteen related constants
-	CANTEEN_NOT_FOUND,
-	CANTEEN_OPENED,
-	CANTEEN_STATUS_UPDATED,
-	CANTEENS_NOT_FOUND,
-	// Payment related constants
-	CHECKOUT_FAILED,
	DISHES_NOT_FOUND,
	IMAGE_UPLOAD_FAILED,
-	// Existing constants
	INVALID_CREDENTIALS,
	INVALID_GOOGLE_TOKEN,
	INVALID_INPUT,
	INVALID_OTP,
	INVALID_PAGE_NUMBER,
	INVALID_PHONE_FORMAT,
-	INVALID_SIGNATURE,
-	INVALID_WEBHOOK_PAYLOAD,
-	// Order related constants
-	ITEMS_UNAVAILABLE,
	LOGOUT_SUCCESS,
-	MENU_ITEM_ADDED,
-	MENU_ITEM_NOT_FOUND,
-	MENU_ITEM_UPDATED,
-	MISSING_SIGNATURE,
-	NO_CANTEEN_ASSOCIATED,

+	// User related constants
+	OTP_SEND_FAILED,
+	OTP_SENT,
+	OTP_VERIFICATION_SUCCESSFUL,
+	PASSWORD_CHANGE_SUCCESS,
+	PASSWORD_MISMATCH,
+	PHONE_UPDATE_SUCCESS,
+	REDIS_CONNECTION_ERROR,
+	SERVER_ERROR,
+	UNAUTHORIZED,
+	USER_ALREADY_EXISTS,
+	USER_NOT_AUTHORISED,
+	USER_NOT_REGISTERED,
+	USER_NOT_VERIFIED,

+	// Payment related constants
+	CHECKOUT_FAILED,
+	INVALID_SIGNATURE,
+	INVALID_WEBHOOK_PAYLOAD,
+	MISSING_SIGNATURE,
+	PAYMENT_ACKNOWLEDGED,
+	PAYMENT_ALREADY_PROCESSED,
+	PAYMENT_PROCESSED,

+	// Canteen related constants
+	CANTEEN_CLOSED,
+	CANTEEN_NOT_FOUND,
+	CANTEEN_OPENED,
+	CANTEEN_STATUS_UPDATED,
+	CANTEENS_NOT_FOUND,
+	MENU_ITEM_ADDED,
+	MENU_ITEM_NOT_FOUND,
+	MENU_ITEM_UPDATED,
+	NO_CANTEEN_ASSOCIATED,

+	// Order related constants
	ORDER_ALREADY_PAID,
	ORDER_COMPLETED,
	ORDER_HANDOVER,
	ORDER_ITEM_NOT_FOUND,
	ORDER_ITEM_READY,
	ORDER_MISMATCHED,
	ORDER_NOT_FOUND,
	ORDER_UPDATED,
-	OTP_SEND_FAILED,
-	OTP_SENT,
-	OTP_VERIFICATION_SUCCESSFUL,
-	PASSWORD_CHANGE_SUCCESS,
-	PASSWORD_MISMATCH,
-	PAYMENT_ACKNOWLEDGED,
-	PAYMENT_ALREADY_PROCESSED,
-	PAYMENT_PROCESSED,
-	PHONE_UPDATE_SUCCESS,
+	ITEMS_UNAVAILABLE,
	QUANTITY_EXCEEDS_LIMIT,
-	REDIS_CONNECTION_ERROR,
-	SERVER_ERROR,
-	UNAUTHORIZED,
-	USER_ALREADY_EXISTS,
-	USER_NOT_AUTHORISED,
-	USER_NOT_REGISTERED,
-	USER_NOT_VERIFIED,
} from "./userConstants";
packages/constants/src/userConstants.ts (1)

1-58: Consider adding JSDoc comments for better documentation

The constants are well-organized with category comments, but consider adding JSDoc comments to provide more context about when each constant is used. This would help developers understand when to use a specific message.

Example:

/**
 * Error message when a user tries to delete their account while they have active orders.
 * Used in the account deletion flow to prevent deletion with pending transactions.
 */
export const ACTIVE_ORDERS_EXIST = "Cannot delete account with active orders";
📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between d2f78f6 and 28fc3fb.

📒 Files selected for processing (6)
  • apps/http/src/controllers/adminController.ts (1 hunks)
  • apps/http/src/controllers/canteenController.ts (1 hunks)
  • apps/http/src/controllers/paymentController.ts (1 hunks)
  • apps/http/src/controllers/userController.ts (12 hunks)
  • packages/constants/src/index.ts (1 hunks)
  • packages/constants/src/userConstants.ts (1 hunks)
🧰 Additional context used
🧬 Code Definitions (4)
apps/http/src/controllers/paymentController.ts (4)
apps/http/src/schemas/ordersSchemas.ts (1)
  • CheckoutInputSchema (102-110)
packages/constants/src/index.ts (10)
  • INVALID_INPUT (18-18)
  • CHECKOUT_FAILED (12-12)
  • MISSING_SIGNATURE (30-30)
  • INVALID_SIGNATURE (22-22)
  • INVALID_WEBHOOK_PAYLOAD (23-23)
  • ORDER_NOT_FOUND (38-38)
  • PAYMENT_ALREADY_PROCESSED (46-46)
  • PAYMENT_PROCESSED (47-47)
  • PAYMENT_ACKNOWLEDGED (45-45)
  • SERVER_ERROR (51-51)
packages/db/src/index.ts (2)
  • MenuItemType (23-23)
  • OrderItemStatus (24-24)
apps/http/src/index.ts (1)
  • razorpayInstance (25-28)
apps/http/src/controllers/userController.ts (2)
packages/constants/src/index.ts (9)
  • LOGOUT_SUCCESS (26-26)
  • PASSWORD_MISMATCH (44-44)
  • PASSWORD_CHANGE_SUCCESS (43-43)
  • INVALID_PHONE_FORMAT (21-21)
  • UNAUTHORIZED (52-52)
  • OTP_SEND_FAILED (40-40)
  • ACTIVE_ORDERS_EXIST (3-3)
  • ACCOUNT_DELETED (2-2)
  • INVALID_PAGE_NUMBER (20-20)
packages/constants/src/userConstants.ts (9)
  • LOGOUT_SUCCESS (21-21)
  • PASSWORD_MISMATCH (17-17)
  • PASSWORD_CHANGE_SUCCESS (18-18)
  • INVALID_PHONE_FORMAT (23-23)
  • UNAUTHORIZED (24-24)
  • OTP_SEND_FAILED (26-26)
  • ACTIVE_ORDERS_EXIST (19-19)
  • ACCOUNT_DELETED (20-20)
  • INVALID_PAGE_NUMBER (25-25)
apps/http/src/controllers/canteenController.ts (1)
packages/constants/src/index.ts (9)
  • DISHES_NOT_FOUND (13-13)
  • ORDER_MISMATCHED (37-37)
  • INVALID_INPUT (18-18)
  • CANTEEN_NOT_FOUND (7-7)
  • CANTEEN_OPENED (8-8)
  • CANTEEN_CLOSED (5-5)
  • NO_CANTEEN_ASSOCIATED (31-31)
  • IMAGE_UPLOAD_FAILED (14-14)
  • MENU_ITEM_NOT_FOUND (28-28)
packages/constants/src/userConstants.ts (1)
packages/constants/src/index.ts (41)
  • OTP_VERIFICATION_SUCCESSFUL (42-42)
  • DISHES_NOT_FOUND (13-13)
  • BROADCAST_QUANTITY (4-4)
  • ORDER_HANDOVER (34-34)
  • USER_NOT_AUTHORISED (54-54)
  • REDIS_CONNECTION_ERROR (50-50)
  • PASSWORD_MISMATCH (44-44)
  • PASSWORD_CHANGE_SUCCESS (43-43)
  • ACTIVE_ORDERS_EXIST (3-3)
  • ACCOUNT_DELETED (2-2)
  • LOGOUT_SUCCESS (26-26)
  • PHONE_UPDATE_SUCCESS (48-48)
  • INVALID_PHONE_FORMAT (21-21)
  • UNAUTHORIZED (52-52)
  • INVALID_PAGE_NUMBER (20-20)
  • OTP_SEND_FAILED (40-40)
  • CHECKOUT_FAILED (12-12)
  • PAYMENT_ALREADY_PROCESSED (46-46)
  • PAYMENT_PROCESSED (47-47)
  • PAYMENT_ACKNOWLEDGED (45-45)
  • MISSING_SIGNATURE (30-30)
  • INVALID_SIGNATURE (22-22)
  • INVALID_WEBHOOK_PAYLOAD (23-23)
  • ORDER_NOT_FOUND (38-38)
  • CANTEEN_NOT_FOUND (7-7)
  • NO_CANTEEN_ASSOCIATED (31-31)
  • CANTEEN_STATUS_UPDATED (9-9)
  • CANTEEN_OPENED (8-8)
  • CANTEEN_CLOSED (5-5)
  • MENU_ITEM_NOT_FOUND (28-28)
  • MENU_ITEM_ADDED (27-27)
  • IMAGE_UPLOAD_FAILED (14-14)
  • MENU_ITEM_UPDATED (29-29)
  • ITEMS_UNAVAILABLE (25-25)
  • QUANTITY_EXCEEDS_LIMIT (49-49)
  • ORDER_ITEM_NOT_FOUND (35-35)
  • ORDER_UPDATED (39-39)
  • ORDER_COMPLETED (33-33)
  • ORDER_ALREADY_PAID (32-32)
  • ORDER_ITEM_READY (36-36)
  • ORDER_MISMATCHED (37-37)
🪛 Biome (1.9.4)
apps/http/src/controllers/adminController.ts

[error] 174-174: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

🔇 Additional comments (16)
apps/http/src/controllers/paymentController.ts (3)

1-14: Good use of centralized constants for error messages.

Importing error message constants from a central location enhances maintainability and ensures consistent messaging across the application. This change will make future updates to error messages easier to manage.


43-44: Well-implemented consistent error handling using constants.

Replacing hardcoded error messages with constants throughout the payment controller improves code maintainability and ensures consistent error responses. The constants provide clear context about the error conditions being handled.

Also applies to: 106-107, 111-113, 128-129, 139-140, 166-167, 175-176, 270-271, 278-279, 301-302


306-306: Improved export organization.

The export statement is now properly organized, making it easier to see all the exported functions at a glance.

apps/http/src/controllers/adminController.ts (2)

1-5: Good organization of imports.

The reorganization of import statements improves readability by grouping related imports together. This is a good practice.

Also applies to: 7-16


324-331: Improved export organization.

The export statement is now alphabetically ordered, making it easier to find specific functions.

apps/http/src/controllers/userController.ts (2)

1-19: Good organization of imports and use of constants.

The reorganization of import statements and use of constants for error messages improves code maintainability and ensures consistent messaging across the application.


358-358: Well implemented use of constants for error messages.

Replacing hardcoded strings with constants for error messages improves maintainability and ensures consistent responses across the application.

Also applies to: 423-423, 542-542, 566-567, 573-573, 621-621, 669-669, 699-699

apps/http/src/controllers/canteenController.ts (3)

1-12: Good organization of imports and use of constants.

The reorganization of import statements and use of constants for error messages improves code maintainability and ensures consistent messaging across the application.


66-66: Well implemented use of constants for error messages.

Replacing hardcoded strings with constants for error messages improves maintainability and ensures consistent responses across the application.

Also applies to: 71-71, 105-105, 122-122, 141-142, 152-152, 203-203, 213-213, 225-225, 303-303


522-531: Improved export organization.

The export statement is now alphabetically ordered, making it easier to find specific functions.

packages/constants/src/userConstants.ts (6)

10-10: Improved error message: OTP verification successful

Good update to the OTP_VERIFICATION_SUCCESSFUL constant with a proper success message rather than an error message.


15-15: Fixed typo in USER_NOT_AUTHORISED message

Fixed the typo from "Aceess" to "Access" in the user authorization error message.


17-26: Well-structured user-related constants added

Good addition of password management, account status, and validation constants with clear, descriptive messages.


28-38: Good organization of payment-related constants

The payment-related constants are well-organized with a descriptive comment header and include comprehensive error and success messages for the payment flow.


39-49: Well-structured canteen-related constants

The canteen-related constants provide clear messages for various canteen operations like status updates, item management, and error conditions.


50-58: Comprehensive order-related constants added

The order-related constants cover all the necessary states and error conditions for order management, including availability, quantity limits, and status updates.

});
res.json(orders);
} catch (e) {
res.status(500).json({ mesage: SERVER_ERROR });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

⚠️ Potential issue

Fix typo in error message property.

There's a typo in the error response: "mesage" should be "message".

-res.status(500).json({ mesage: SERVER_ERROR });
+res.status(500).json({ message: SERVER_ERROR });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
res.status(500).json({ mesage: SERVER_ERROR });
res.status(500).json({ message: SERVER_ERROR });

const TEST_EMAILS = ["developer@cuttheq.in"];
const canteenId = req.user?.canteenId;
if (!canteenId) {
return res.status(403).json({ message: "Canteen not found" });
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🛠️ Refactor suggestion

Replace hardcoded error message with constant.

For consistency, the hardcoded error message should be replaced with the CANTEEN_NOT_FOUND constant.

-return res.status(403).json({ message: "Canteen not found" });
+return res.status(403).json({ message: CANTEEN_NOT_FOUND });
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
return res.status(403).json({ message: "Canteen not found" });
return res.status(403).json({ message: CANTEEN_NOT_FOUND });

@iamnithishraja
Copy link
Copy Markdown
Owner

@ammar-codeable can i merge this

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants