Skip to content

Order Views Documentation

This document explains the views implemented in the order-related API, their permissions, actions, and endpoints. It provides an overview of the logic and functionality, without repeating the code itself.

General Information

  • Base URL: /api/v1/order/
  • Permission Class: IsAuthenticated
  • Pagination Class: CustomPagination
  • Error Handling: All views include exception handling to provide meaningful error messages in case of failures.
  • Transaction Management: @transaction.atomic is used for create actions to ensure atomicity.
  • Email Notifications: Emails are sent to relevant stakeholders (users and vendors) for cancellation and return requests.

Endpoints and Their Details

1. Cancellation Reasons

  • Endpoint: cancel/reason/
  • HTTP Methods: GET
  • Description: Retrieves a list of active cancellation reasons.
  • View: CancellationReasonViewSet
  • Fetches active reasons from the CancellationReason model.
  • Orders results by created_at.

2. Order Cancellation

  • Endpoint:
  • List and Create: cancel/
  • Retrieve: cancel/<str:pk>/
  • HTTP Methods:
  • GET for list and retrieve.
  • POST for create.
  • View: OrderCancellationViewSet
  • List:
    • Filters cancellations based on the logged-in user’s orders.
    • Supports query parameters:
    • product_name: Filters by product name.
    • order_id: Filters by order ID.
    • Paginates results if necessary.
  • Retrieve:
    • Fetches details of a specific cancellation request.
    • Includes related models such as OrderTracking and Reason for detailed information.
  • Create:
    • Validates and creates a new cancellation request.
    • Sends notification emails to the user and vendor.
    • Saves details such as refund amount and bank account information.

3. Return Reasons

  • Endpoint: return/reason/
  • HTTP Methods: GET
  • Description: Retrieves a list of active return reasons.
  • View: ReturnReasonViewSet
  • Fetches active reasons from the ReturnReason model.
  • Orders results by created_at.

4. Order Return

  • Endpoint:
  • List and Create: return/
  • Retrieve: return/<str:pk>/
  • HTTP Methods:
  • GET for list and retrieve.
  • POST for create.
  • View: OrderReturnViewSet
  • List:
    • Filters returns based on the logged-in user’s orders.
    • Supports query parameters:
    • product_name: Filters by product name.
    • order_id: Filters by order ID.
    • Paginates results if necessary.
  • Retrieve:
    • Fetches details of a specific return request.
    • Includes related models such as OrderTracking and Reason for detailed information.
  • Create:
    • Validates and creates a new return request.
    • Sends notification emails to the user and vendor.

5. Order Tracking

  • Endpoint: tracking/
  • HTTP Methods: GET
  • Description: Retrieves tracking details for orders placed by the logged-in user.
  • View: OrderTrackingViewSet
  • Filters tracking data based on the logged-in user’s orders.
  • Supports query parameters:
    • product_name: Filters by product name.
    • order_id: Filters by order ID.
  • Orders results by created_at.

Permissions

  • All views require the user to be authenticated via the IsAuthenticated permission class.

Actions Overview

Read-Only Actions

  • List: Retrieves a list of records based on user-specific filters and query parameters.
  • Retrieve: Fetches details of a specific record.

Write Actions

  • Create:
  • Validates input data using serializers.
  • Ensures atomicity with @transaction.atomic.
  • Sends email notifications upon successful creation.

Logging

  • Errors and exceptions are logged using the logging module for debugging and monitoring.

Email Notifications

  • Templates are used for crafting user and vendor emails.
  • The send_email task (via Celery) handles asynchronous email delivery.

Query Parameters

For endpoints supporting list actions, the following query parameters are available: - product_name: Filter results based on product name (case-insensitive). - order_id: Filter results based on order ID (case-insensitive).

Example Request and Response

Cancellation Reasons

Request:

GET /api/v1/order/cancel/reason/
Response:
[
  {
    "id": 1,
    "reason": "Customer changed mind",
    "is_active": true
  },
  {
    "id": 2,
    "reason": "Product out of stock",
    "is_active": true
  }
]

Order Cancellation

Request:

POST /api/v1/order/cancel/
{
  "order_tracking_id": "123",
  "reason_id": "1",
  "refund_amount": "100.00",
  "account_name": "John Doe",
  "account_number": "123456789",
  "bank_name": "XYZ Bank",
  "bank_branch": "Main"
}
Response:
{
  "id": 45,
  "order_tracking_id": "123",
  "reason": "Customer changed mind",
  "refund_amount": "100.00",
  "account_name": "John Doe",
  "account_number": "123456789",
  "bank_name": "XYZ Bank",
  "bank_branch": "Main",
  "status": "Pending"
}

Order Tracking

Request:

GET /api/v1/order/tracking/?product_name=silk%20Saree
Response:
[
  {
    "id": 23,
    "order_id": "ORD12345",
    "product_name": "Silk Saree",
    "status": "Shipped",
    "estimated_delivery": "2024-12-25"
  }
]