Update Order Draft Functionality
Theupdate_order_draft tool is LeClerk’s core order management system. Unlike a traditional shopping cart with simple add/remove operations, this system uses a sophisticated validation and processing pipeline to ensure orders are valid, properly priced, and ready for fulfillment.
What is an Order Draft?
An order draft represents a customer’s order-in-progress within a conversation. It’s a stateful entity that accumulates customer choices and gets validated/updated through theCheckAndUpdateOrderDraftUsecase.
Key characteristics:
- Conversation-Scoped: Each order draft belongs to a specific WhatsApp conversation
- Stateful: Maintains items, deals, delivery details, timing, and customer preferences
- Validated: Every update triggers comprehensive validation before persistence
- Mode-Aware: Handles both single-location and multi-location organization scenarios
The Update and Validation Pipeline
The system uses a processor-based architecture rather than simple CRUD operations:How Order Updates Work
The system receives anUpdateOrderDraftDTO which can contain:
Common Validation Errors
The system returns specific error codes when validation fails:Item-Related Errors
SKU_NOT_FOUND$${skuId}: The requested SKU doesn’t exist in the catalogSKU_OUT_OF_STOCK$${skuId}: The SKU is temporarily unavailableSKU_ONLY_AVAILABLE_IN_DEALS$${skuId}: This SKU can only be ordered as part of a dealOPTION_NOT_FOUND$${optionId}: The selected option doesn’t existNOT_ENOUGH_OPTIONS_SELECTED$${skuId}$${optionListId}$${min}$${selected}: More options requiredTOO_MANY_OPTIONS_SELECTED$${skuId}$${optionListId}$${max}$${selected}: Too many options selected
Product Restriction Errors
PRODUCT_NOT_AVAILABLE_TODAY$${productId}$${date}$${reason}: Product not available on this datePRODUCT_NOT_AVAILABLE_AT_THIS_TIME_TOO_EARLY$${productId}$${time}$${availableFrom}$${date}: Ordered too earlyPRODUCT_NOT_AVAILABLE_FOR_DELIVERY$${productId}$${reason}: Not available for delivery service
Deal-Related Errors
DEAL_NOT_FOUND$${dealId}: The deal doesn’t existDEAL_SKU_NOT_IN_DEAL$${dealId}$${skuId}: SKU is not part of this dealDEAL_INCORRECT_SKU_COUNT$${dealId}$${line}$${expected}: Wrong number of SKUs for deal line
Service and Delivery Errors
DELIVERY_ADDRESS_REQUIRED: Address needed for delivery ordersOUTSIDE_OF_DELIVERY_RADIUS$${distance}: Address outside delivery zoneMINIMUM_ORDER_NOT_MET$${minimum}$${current}$${currency}: Order below minimum valueLOCATION_IS_PAUSED: Restaurant is temporarily closed
Response Structure
TheCheckAndUpdateOrderDraftUsecase returns a CheckAndUpdateOrderDraftReturnDTO:
Validation Types
The validation object indicates the order’s readiness state:1. Validation Errors
2. Payment Setup Required
3. Ready for Payment
Timing and Scheduling
Time handling is complex and considers:Business Hours
- Restaurant operating hours
- Kitchen preparation times
- Delivery time windows
Service Type Impact
- Collection: Only needs ready time (when food is prepared)
- Delivery: Needs both ready time and delivery time (includes travel)
Timing Validation
OUTSIDE_OF_STORE_HOURS: Requested time outside restaurant hoursOUTSIDE_OF_DELIVERY_HOURS: Delivery requested outside delivery windowREQUESTED_TIME_REQUIRED: Must specify ASAP or scheduled time
Delivery Mode Handling
The delivery processor handles different delivery modes based on location settings:UberDirect Integration
- Validates addresses using Uber’s API
- Calculates real-time delivery quotes
- Handles special Uber timing and fee requirements
- Error codes:
UBER_ADDRESS_UNDELIVERABLE,INVALID_UBER_TIMINGS
Chaskis Delivery
- Validates delivery zones using geographic boundaries
- Calculates distance-based fees
- Considers traffic and route optimization
Postal Code Delivery
- Simple postcode-based zone validation
- Fixed fees per postcode area
- Error:
POSTAL_CODE_NOT_SERVED$${postalCode}
Free Delivery
- Validates minimum order requirements
- May have time or zone restrictions
Multi-Location Considerations
In organization mode, the system handles:Location Assignment
- If no
locationIdis provided - Error:
LOCATION_ID_NOT_FOUND_IN_BUSINESS_ORGANIZATION
Location Switching
- Updating
locationIdswitches the context to a different restaurant - All subsequent validations use the new location’s catalog and settings
Advanced Features
Promotion Code Processing
Thecodes array in UpdateOrderDraftDTO triggers automatic discount validation:
- Validates code existence and eligibility
- Applies percentage, fixed amount, BOGO, or free product discounts
- Calculates cumulative effects of multiple codes
Customer Memory Integration
The system can update customer preferences:- Language preferences affect future interactions
- Name updates sync across the customer profile
- Notes and instructions are stored for reuse
Order Summary Generation
After successful validation, the system provides:- Itemized pricing with options
- Applied discounts and their effects
- Delivery fees broken down by type (delivery + service)
- Timing estimates based on kitchen capacity and delivery distance
Development Considerations
Error Recovery
The validation pipeline stops at the first processor that encounters errors, ensuring:- Fast failure for invalid requests
- Consistent error reporting
- No unnecessary computation after validation fails
Integration Points
The update order draft system integrates with:- Catalog Service: For product, SKU, and deal validation
- Customer Service: For profile updates and preferences
- Location Service: For business rules and operational status
- Pricing Service: For discount calculation and fee computation
- Delivery Services: For address validation and quote generation
- Payment Service: For payment method validation and setup