Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Backend Support for Managing Malfunctioning Products with Enhanced Validation and Error Handling #1006

Merged
merged 2 commits into from
Nov 10, 2024

Conversation

IkkiOcean
Copy link
Contributor

@IkkiOcean IkkiOcean commented Nov 10, 2024

Description:

This pull request introduces the backend logic for managing malfunctioning products in the system. The key features include:

  • Schema Definition: Created a new schema for MalfunctioningProduct that links to a Product and stores details about the malfunction, actions taken, and the user responsible.
  • Controller Logic: Added robust CRUD operations (Create, Read, Update, Delete) for malfunctioning products.
    • createMalfunctioningProduct: Handles product creation with validation.
    • getMalfunctioningProduct: Fetches a specific malfunctioning product by ID.
    • updateMalfunctioningProduct: Updates existing malfunctioning product data.
    • deleteMalfunctioningProduct: Deletes a malfunctioning product.
    • getAllMalfunctioningProducts: Supports pagination and search functionality.
    • getMalfunctioningProductCountByStatus: Provides counts of malfunctioning products by their status.
  • Validation Middleware: Includes middleware for validating product creation and updates.
  • Authentication Middleware: Ensures that all operations are authenticated.
  • Edge Case Handling: Adds error handling for invalid product IDs, missing or incorrect fields, non-existing users, etc.

Changes Introduced:

  1. Model: Created MalfunctioningProduct model in models/MalfunctioningProduct.js.
  2. Controllers: Implemented functions in controllers/MalfunctioningProductController.js for managing CRUD operations and handling errors.
  3. Validation Middleware: Added validation in middlewares/validationMiddleware.js for the input fields for both product creation and updates.
  4. Routes: Defined the routes in routes/malfunctioningProductRoutes.js to handle the backend logic for the malfunctioning products.
  5. Authentication Middleware: Introduced middlewares/authenticationMiddleware.js to authenticate requests.
  6. Edge Case Handling: Added error handling for various edge cases like invalid product IDs, missing required fields, and non-existing users.

Why This PR is Necessary:

  • Error Handling: By adding comprehensive error handling, the application can gracefully handle unexpected issues and provide meaningful responses to the client.
  • Data Validation: Ensures that only valid data is processed and stored in the database, reducing the risk of incorrect or incomplete data.
  • Improved User Experience: Users can now manage malfunctioning products with proper feedback on errors and status updates.
  • Scalability: With features like pagination and search for product listings, the system can scale better and handle larger datasets.

Test Cases to Be Covered:

  • Create Product:

    • Test creating a new malfunctioning product with valid inputs.
    • Test with invalid productId or createdBy fields.
    • Test missing required fields (description, actionsTaken).
  • Get Product:

    • Test retrieving a malfunctioning product by a valid ID.
    • Test with an invalid or non-existent product ID.
  • Update Product:

    • Test updating an existing product with valid data.
    • Test attempting to update a non-existent product.
    • Test missing required fields in the update payload.
  • Delete Product:

    • Test deleting a product with a valid ID.
    • Test trying to delete a product with a non-existent ID.
  • Pagination and Search:

    • Test pagination with valid page and limit query parameters.
    • Test searching with a valid search query (description field).
    • Test with an invalid search query.
  • Get Count by Status:

    • Test fetching the count of products by status (pending, in progress, resolved).

How to Test:

  1. Test the APIs: Use Postman or cURL to test the newly added routes for creating, updating, retrieving, and deleting malfunctioning products.
  2. Run Unit Tests: Ensure that validation logic works correctly, and edge cases are handled.
  3. Check Authentication: Ensure that all routes are properly authenticated and unauthorized users cannot access the routes.

Files Changed:

  1. models/MalfunctioningProduct.js - Defines the Malfunctioning Product model.
  2. controllers/MalfunctioningProductController.js - Contains CRUD operations and business logic for malfunctioning products.
  3. middlewares/validationMiddleware.js - Includes validation logic for the creation and update of malfunctioning products.
  4. middlewares/authenticationMiddleware.js - Implements authentication for routes.
  5. routes/malfunctioningProductRoutes.js - Defines API routes for malfunctioning products.

Example Requests:

  1. Create Malfunctioning Product:

    POST /api/malfunctioning-products

    Request Body:

    {
      "productId": "60c72b2f9f1b2c001f8b6e78",
      "description": "Faulty power supply",
      "actionsTaken": "Replaced the capacitor",
      "createdBy": "60c72b2f9f1b2c001f8b6e79"
    }
  2. Get Malfunctioning Product by ID:

    GET /api/malfunctioning-products/:id

    Example Response:

    {
      "productId": "60c72b2f9f1b2c001f8b6e78",
      "description": "Faulty power supply",
      "actionsTaken": "Replaced the capacitor",
      "createdBy": "60c72b2f9f1b2c001f8b6e79"
    }
  3. Update Malfunctioning Product:

    PUT /api/malfunctioning-products/:id

    Request Body:

    {
      "actionsTaken": "Replaced the fuse"
    }
  4. Delete Malfunctioning Product:

    DELETE /api/malfunctioning-products/:id


Additional Notes:

  • Please ensure all routes are tested for proper access control and validation.
  • Review and confirm the correctness of error messages and status codes returned to the client.
  • Ensure that no sensitive data is logged in production environments.

Copy link

vercel bot commented Nov 10, 2024

Deployment failed with the following error:

Resource is limited - try again in 12 minutes (more than 100, code: "api-deployments-free-per-day").

Copy link
Contributor

Thank you for submitting your pull request! 🙌 We'll review it as soon as possible. In the meantime, please ensure that your changes align with our CONTRIBUTING.md. If there are any specific instructions or feedback regarding your PR, we'll provide them here. Thanks again for your contribution! 😊

@manikumarreddyu manikumarreddyu merged commit 28f24f9 into manikumarreddyu:main Nov 10, 2024
1 of 2 checks passed
Copy link
Contributor

🎉 Your pull request has been successfully merged! 🎉 Thank you for your valuable contribution to our project. Your efforts are greatly appreciated. Feel free to reach out if you have any more contributions or if there's anything else we can assist you with. Keep up the fantastic work! 🚀

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

Successfully merging this pull request may close these issues.

Implement Warehouse Schema and Controllers for Warehouse Management
2 participants