Serializer Documentation
This documentation provides an explanation of the serializers defined in the serializers.py file. These serializers are used to validate, transform, and serialize the data for API interactions related to orders, cancellations, returns, and associated models.
TransactionSerializer
Purpose:
Serializes the Transaction model, which contains information about payment transactions.
Fields:
- transaction_id: Unique identifier for the transaction.
- amount: The amount involved in the transaction.
- payment_method: The method used for payment (e.g., card, bank transfer).
- status: Current status of the transaction.
- created_at and updated_at: Timestamps for transaction creation and updates.
PurchasedCartItemSerializer
Purpose:
Serializes the PurchasedCartItem model, including product details associated with the purchase.
Fields:
- order_id: Unique identifier for the order.
- product_name: Name of the product in the cart.
- product_image: Image of the product in the cart.
- status: Current status of the purchased item.
- created_at and updated_at: Timestamps for item creation and updates.
OrderStatusSerializer
Purpose:
Serializes the OrderStatus model, providing details about the current status of an order.
Fields:
- id: Unique identifier for the status.
- status_name: Name of the order status (e.g., "Delivered", "Cancelled").
OrderTrackingSerializer
Purpose:
Handles serialization for the OrderTracking model, which tracks the order's lifecycle.
Key Features:
- Nested serialization for OrderStatus.
- A custom method (get_product_image) retrieves the first product image if available.
Fields:
- id: Unique identifier for the tracking entry.
- order_id: Order identifier (related to PurchasedCartItem).
- product_name: Name of the product in the order.
- product_image: URL of the product's image.
- transaction: Related transaction details.
- Status-related fields (e.g., has_ordered_confirmed, has_cancelled, etc.).
- Timestamps for actions like confirmation, cancellation, shipping, delivery, etc.
- updated_at: Timestamp for the latest update.
CancellationReasonSerializer
Purpose:
Serializes the CancellationReason model, listing reasons for order cancellations.
Fields:
- id: Unique identifier for the reason.
- reason: Text describing the cancellation reason.
- is_active: Indicates if the reason is currently active.
OrderCancellationSerializer
Purpose:
Handles serialization for the OrderCancellation model, validating and creating cancellation requests.
Key Features:
- Nested serializers for CancellationReason and OrderTracking.
- Custom validation ensures:
- The cancellation reason is valid and active.
- The order belongs to the requesting user.
- The order is in a cancellable state (not shipped or already cancelled).
- No duplicate cancellation requests exist.
Fields:
- id: Unique identifier for the cancellation.
- reason: Cancellation reason details.
- order_tracking: Related tracking information.
- Refund-related fields (account_name, account_number, bank_name, etc.).
- Status fields (has_approved, refund_processed).
- cancelled_at and updated_at: Timestamps for cancellation and updates.
Custom Methods:
- validate: Enforces business rules for valid cancellations.
- create: Automatically calculates refund amount and updates order status to "Cancelled".
ReturnReasonSerializer
Purpose:
Serializes the ReturnReason model, listing reasons for order returns.
Fields:
- id: Unique identifier for the reason.
- reason: Text describing the return reason.
- is_active: Indicates if the reason is currently active.
OrderReturnSerializer
Purpose:
Handles serialization for the OrderReturn model, validating and creating return requests.
Key Features:
- Nested serializers for ReturnReason and OrderTracking.
- Custom logic to calculate the refund amount.
- Validation rules ensure:
- The return reason is valid and active.
- The order belongs to the requesting user.
- The order has been delivered and is within the return time limit (48 hours).
- No duplicate return requests exist.
- The return request is either approved or rejected with a valid reason.
- Refund details are recorded when processed.
Fields:
- id: Unique identifier for the return.
- reason: Return reason details.
- order_tracking: Related tracking information.
- refund_amount: Calculated refund amount.
- account_name: Name of the account for refund processing.
- account_number: Bank account number for refund.
- bank_name: Name of the bank.
- bank_branch: Bank branch details.
- refund_processed: Indicates whether the refund has been completed.
- has_approved: Status of return approval.
- is_rejected: Indicates if the return request was rejected.
- rejection_reason: Stores the reason for rejection.
- returned_at and updated_at: Timestamps for the return and updates.
Custom Methods:
- get_refund_amount: Calculates the refund based on product price and quantity.
- validate: Enforces business rules for valid returns, including refund eligibility.
- create: Creates the return request and updates order status to "Returned".
- process_refund: Marks the refund as processed and records payment details.