Skip to content

Vendor Coupon Create Serializer Documentation


VendorCouponCreateSerializer

Fields

  • id: Integer, Auto-generated primary key.
  • code: String, Unique alphanumeric coupon code (max length: 10, automatically uppercased).
  • description: Text, Detailed description of the coupon (optional).
  • discount_percent: Decimal, Discount percentage applied to the purchase (default: 0.00).
  • money_value_off: Decimal, Fixed amount discount instead of a percentage (default: 0.00).
  • total_issues: Integer, Maximum times this coupon can be issued.
  • coupon_limit_per_customer: Integer, Maximum times a single customer can use this coupon.
  • used_count: Integer, Number of times this coupon has been used.
  • start_at: DateTime, Start date of coupon validity (optional).
  • end_at: DateTime, End date of coupon validity (optional).
  • min_purchase: Decimal, Minimum purchase amount required to apply the coupon.
  • is_active: Boolean, Status of the coupon (default: True).
  • coupon_type: String, Determines if the coupon is a "Percentage Discount" or "Money Value Off".

Validations

  1. Code Validation:
  2. Must be alphanumeric (letters and numbers only).
  3. Cannot exceed 10 characters.
  4. Must be unique (case-insensitive check).
  5. Automatically converted to uppercase.

  6. User Role Validation:

  7. Only vendors can create coupons.
  8. Users with vendor_role must have the Coupon Management permission.
  9. Vendor must have at least one shop.

  10. Discount Validation:

  11. At least one of discount_percent or money_value_off must be greater than zero.
  12. Cannot use both discount_percent and money_value_off simultaneously.

  13. Date Validation:

  14. end_at must be later than start_at if both are specified.

Create Logic

  • Ensures only vendors can create coupons.
  • Attaches the coupon to the vendor’s shop.
  • Ensures code is stored in uppercase.
  • Sets is_global = False (vendor-specific coupons only).

Coupon Type Determination

  • If discount_percent > 0, the coupon is categorized as "Percentage Discount".
  • If money_value_off > 0, the coupon is categorized as "Money Value Off".
  • If neither applies, it defaults to "Unknown".

Error Handling

  • Raises an error if the user is not a vendor.
  • Raises an error if the vendor does not own a shop.
  • Ensures coupon constraints are met before saving.

This documentation describes the VendorCouponCreateSerializer and its validation and processing logic for handling vendor coupon creation.