Dispatch Flow

%%{init: { "theme": "base", "themeVariables": { "fontSize": "18px", "background": "transparent", "actorBkg": "#038851", "actorTextColor": "#ffffff", "actorBorder": "#026139", "actorLineColor": "#94a3b8", "signalColor": "#64748b", "signalTextColor": "#059669", "noteBkgColor": "#e6f4ee", "noteTextColor": "#02502f", "noteBorderColor": "#038851", "activationBkgColor": "#c6ebdc", "activationBorderColor": "#026139", "sequenceNumberColor": "#ffffff" }, "sequence": { "useMaxWidth": true, "actorMargin": 70, "width": 150, "height": 60, "boxMargin": 12, "noteMargin": 14, "messageMargin": 50, "diagramMarginX": 20, "diagramMarginY": 20, "actorFontSize": 18, "actorFontWeight": 700, "noteFontSize": 15, "noteFontFamily": "ui-monospace, SFMono-Regular, Menlo, Consolas, monospace", "messageFontSize": 16, "messageFontWeight": 700, "noteAlign": "left", "mirrorActors": false } }}%%

sequenceDiagram
    autonumber
    actor Customer
    participant Channel as Ordering Channel
    participant Deliverect
    participant Dispatch as Dispatch Partner<br/>(courier network)
    participant POS

    Note over Deliverect,Dispatch: 1. Validate Availability
    Customer->>Channel: Places order needing delivery
    Channel->>Deliverect: Order with delivery address
    Deliverect->>Dispatch: POST validate (channelLinkId,<br/>deliveryLocations, pickupTime)
    Dispatch-->>Deliverect: 200 OK { available, validationId<br/>(valid 10 min), price, ETAs }
    Note right of Deliverect: Check "available" flag, not just 200.<br/>If false -> no job created

    Note over Deliverect,Dispatch: 2. Create Delivery Job (if canDeliver/available)
    Deliverect->>Dispatch: POST create_job (jobId, pickupLocation,<br/>deliveryLocations, ageCheck)
    Dispatch-->>Deliverect: 200 { canDeliver, externalJobId,<br/>courier, price, ETAs }
    Deliverect->>POS: Inject order into POS

    Note over Deliverect,Dispatch: 3. Update Delivery Job (optional changes)
    Deliverect->>Dispatch: POST updateJob (pickup time / items /<br/>dropoff changes)
    Dispatch-->>Deliverect: 200 { canDeliver, courier, ETAs }

    Note over Dispatch,Channel: 4. Delivery Status Updates (Partner -> Deliverect)
    loop For each status change
        Dispatch->>Deliverect: POST /fulfillment/generic/events<br/>(status, ETAs, courier, trackingUrl)
        Deliverect-->>Dispatch: 200 OK
        Deliverect->>Channel: Relay courier/delivery update
    end
    Note right of Dispatch: Statuses: <br>EN_ROUTE_TO_PICKUP (83),<br/>ARRIVED_AT_PICKUP (85),<br/>EN_ROUTE_TO_DROPOFF (87),<br/>ARRIVED_AT_DROPOFF (89),<br/>DELIVERED (90)

    Note over Deliverect,Dispatch: 5. Cancellation (optional)
    Channel->>Deliverect: Order / delivery canceled
    Deliverect->>Dispatch: POST cancel_job (jobId, deliveryLocations, courier)
    Dispatch-->>Deliverect: { status: confirmed, reason, price }