feat: enhance user and order constants for improved clarity and funct…#48
feat: enhance user and order constants for improved clarity and funct…#48ammar-codeable wants to merge 1 commit into
Conversation
WalkthroughThe 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 ( Changes
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
Poem
✨ Finishing Touches
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this comment.
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 moreThis 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 errorapps/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.tsThe 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 documentationThe 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
📒 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 successfulGood 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 messageFixed the typo from "Aceess" to "Access" in the user authorization error message.
17-26: Well-structured user-related constants addedGood addition of password management, account status, and validation constants with clear, descriptive messages.
28-38: Good organization of payment-related constantsThe 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 constantsThe canteen-related constants provide clear messages for various canteen operations like status updates, item management, and error conditions.
50-58: Comprehensive order-related constants addedThe 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 }); |
There was a problem hiding this comment.
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.
| 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" }); |
There was a problem hiding this comment.
🛠️ 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.
| return res.status(403).json({ message: "Canteen not found" }); | |
| return res.status(403).json({ message: CANTEEN_NOT_FOUND }); |
|
@ammar-codeable can i merge this |
…ionality
Summary by CodeRabbit
New Features
Bug Fixes
Refactor