PageSourceSearch

https://nearestone.com/assets/BusinessDelivery-DbOfsfnP.js

js nearestone.com collected 2026-10-02 01:37:17 UTC 10,859 bytes, 1 lines download raw bytes

1import{c as J,r as l,s as C,j as e,N as X,f as Z,C as h,g as E,e as _,H as ee,T as se,o,h as $,M as re,Q as ae}from"./index-BBNW_iBz.js";import{u as te}from"./useBusinessKey-BcecJZP9.js";import{P as ie,L as ne}from"./LiveDeliveryMap-CoS0oopI.js";import{c as de}from"./countryRouting-39JifOLM.js";import{A as le}from"./arrow-left-B2-kuRGx.js";import{f as B}from"./format-DZ-oazm4.js";import"./liveTracking-CMIoeWyf.js";import"./en-US-B7LwhDDJ.js";const c=C,ce={assigned:["picked_up","cancelled"],picked_up:["en_route","failed"],en_route:["arrived","failed"],arrived:["delivered","failed"]},_e=()=>{const{businessId:u,country:L}=te(),j=L||"us",{user:g,loading:q}=J(),[D,I]=l.useState(!1);l.useEffect(()=>{g&&C.from("user_roles").select("role").eq("user_id",g.id).eq("role","super_admin").maybeSingle().then(({data:s})=>I(!!s))},[g]);const[p,O]=l.useState(null),[f,T]=l.useState([]),[F,z]=l.useState([]),[N,M]=l.useState({}),[w,H]=l.useState({}),[U,V]=l.useState(!0),[k,A]=l.useState(null),x=async()=>{if(!u)return;const[{data:s},{data:a},{data:t},{data:i}]=await Promise.all([c.from("businesses").select("*").eq("id",u).maybeSingle(),c.from("orders").select("*, order_items(name,qty), riders:assigned_rider_id(id,full_name,phone,vehicle_type)").eq("business_id",u).eq("fulfilment_type","delivery").order("created_at",{ascending:!1}).limit(60),c.rpc("list_delivery_riders",{_business_id:u}),c.from("orders").select("assigned_rider_id, shipping, currency, riders:assigned_rider_id(id,full_name,phone,vehicle_type)").eq("business_id",u).eq("delivery_status","delivered").is("rider_payout_id",null).not("assigned_rider_id","is",null)]);O(s),T(a??[]),z(t??[]);const n={};(i??[]).forEach(r=>{const d=r.assigned_rider_id;d&&(n[d]||(n[d]={rider:r.riders,count:0,amount:0,currency:r.currency||"INR"}),n[d].count+=1,n[d].amount+=Number(r.shipping||0))}),H(n);const m=(a??[]).map(r=>r.id);if(m.length){const{data:r}=await c.from("delivery_events").select("*").in("order_id",m).order("created_at",{ascending:!1}),d={};(r??[]).forEach(P=>{var S;(d[S=P.order_id]||(d[S]=[])).push(P)}),M(d)}V(!1)};l.useEffect(()=>{x()},[u]),l.useEffect(()=>{if(!u)return;const s=async()=>{await c.rpc("expire_stale_offers");const t=f.filter(n=>!n.assigned_rider_id&&["placed","accepted","preparing","ready"].includes(n.status));if(t.length===0)return;let i=0;for(const n of t){const{data:m,error:r}=await c.functions.invoke("dispatch-order",{body:{order_id:n.id}});!r&&(m!=null&&m.assigned)&&i++}
1i>0&&(o.success(`${i} order${i===1?"":"s"} assigned to nearby rider${i===1?"":"s"}`),x())},a=window.setInterval(s,3e4);return()=>window.clearInterval(a)},[u,f]);const K=async s=>{const a=window.prompt("Payment method (upi / bank / cash / other):","upi");if(!a)return;const t=window.prompt("Transaction reference / UTR (optional):","")||null;A(s);const{error:i}=await c.rpc("create_rider_payout",{_rider_id:s,_business_id:u,_method:a,_reference:t,_notes:null});if(A(null),i)return o.error(i.message);o.success("Payout released"),x()};if(q)return null;if(!g)return e.jsx(X,{to:de(j),replace:!0});if(U)return e.jsx(Z,{label:"Loading delivery board…"});if(!p||p.owner_user_id!==g.id&&!D)return e.jsx("div",{className:"min-h-screen flex items-center justify-center p-6",children:e.jsxs(h,{className:"p-8 text-center",children:[e.jsx("h2",{className:"font-black mb-2",children:"Not your business"}),e.jsx(E,{to:`/${j}/dashboard`,children:e.jsx(_,{children:"Back"})})]})});const Q=ee(p.country||j).currency,W=async(s,a)=>{const{error:t}=await c.rpc("assign_rider_to_order",{_order_id:s,_rider_id:a});if(t)return o.error(t.message);o.success("Rider assigned"),x()},Y=async s=>{const{data:a,error:t}=await c.functions.invoke("dispatch-order",{body:{order_id:s}});if(t)return o.error(t.message);if(a!=null&&a.assigned){const i=a.eta_sec?` · ETA ~${Math.round(a.eta_sec/60)} min`:"";o.success(`Assigned to nearest available rider${a.is_batch?" (batch)":""}${i}`)}else o.message("No nearby available rider yet — will retry every 30s");x()},G=async(s,a)=>{const{error:t}=await c.rpc("update_delivery_status",{_order_id:s,_status:a,_note:null,_lat:null,_lng:null});if(t)return o.error(t.message);o.success(`Marked ${a}`),x()},y=f.filter(s=>!s.assigned_rider_id&&["placed","accepted","preparing","ready"].includes(s.status)),b=f.filter(s=>s.assigned_rider_id&&!["delivered","completed","cancelled","refunded"].includes(s.status)),R=f.filter(s=>["delivered","completed"].includes(s.status)),v=({o:s,showAssign:a,showAdvance:t})=>{var i,n,m;return e.jsxs(h,{className:"p-4 border-2",children:[e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{className:"min-w-[220px]",children:[e.jsxs("div",{className:"flex items-center gap-2 flex-wrap",children:[e.jsx("span",{className:"font-black",children:s.order_number}),e.jsx($,{className:s.delivery_status==="delivered"?"bg-emerald-600":s.delivery_status?"bg-amber-600":"bg-slate-500",children:s.delivery_status||"not-assigned"}),e.jsx($,{variant:"outline",children:s.status})]}),e.jsx("p",{className:"text-xs text-muted-foreground mt-1",children:B(new Date(s.created_at),"PPp")}),e.jsxs("p",{className:"text-sm mt-1 flex items-center gap-1",children:[e.jsx(re,{className:"w-3 h-3"})," ",s.recipient_name," · ",s.recipient_phone]}),s.address_snapshot&&e.jsx("p",{className:"text-xs text-muted-foreground line-clamp-2",children:[s.address_snapshot.line1,s.address_snapshot.line2,s.address_snapshot.city,s.address_snapshot.pincode].filter(Boolean).join(", ")}),e.jsx("ul",{className:"text-xs mt-2 text-muted-foreground",children:(s.order_items||[]).slice(0,4).map((r,d)=>e.jsxs("li",{children:["· ",r.qty,"× ",r.name]},d))}),e.jsxs("p",{className:"mt-1 font-black text-primary",children:[Q," ",Number(s.total).toFixed(2)]}),s.riders&&e.jsxs("p",{className:"text-xs mt-2 flex items-center gap-1",children:[e.jsx(ae,{className:"w-3 h-3"})," ",s.riders.full_name," · ",s.riders.vehicle_type," · ",e.jsx(ie,{className:"w-3 h-3"})," ",s.riders.phone]}),s.pickup_otp&&s.assigned_rider_id&&!["delivered","cancelled","failed"].includes(s.delivery_status)&&["assigned"].includes(s.delivery_status)&&e.jsxs("div",{className:"mt-2 p-2 rounded-md border-2 border-amber-400 bg-amber-50 inline-flex items-center gap-2",children:[e.jsx("span",{className:"text-[10px] font-black uppercase tracking-wider text-amber-800",children:"Share with rider · Pickup OTP"}),e.jsx("span",{className:"font-black text-lg tracking-[0.3em] text-amber-900",children:s.pickup_otp})]})]}),e.jsxs("div",{className:"flex flex-col gap-2 min-w-[200px]",children:[a&&e.jsxs(e.Fragment,{children:[e.jsx(_,{size:"sm",variant:"default",onClick:()=>Y(s.id),children:"Assign nearest available rider"}),e.jsxs("select",{className:"text-sm border rounded px-2 py-1",defaultValue:"",onChange:r=>r.target.value&&W(s.id,r.target.value),children:[e.jsx("option",{value:"",children:"Or assign manually…"}),F.map(r=>
1e.jsxs("option",{value:r.id,children:[r.full_name," · ",r.vehicle_type,r.is_available?"":" (busy)"]},r.id))]})]}),t&&e.jsx("div",{className:"flex flex-wrap gap-1",children:(ce[s.delivery_status||"assigned"]||[]).map(r=>e.jsx(_,{size:"sm",variant:r==="failed"||r==="cancelled"?"outline":"default",onClick:()=>G(s.id,r),children:r},r))}),(i=N[s.id])!=null&&i.length?e.jsxs("details",{className:"text-xs",children:[e.jsx("summary",{className:"cursor-pointer text-muted-foreground",children:"Tracking log"}),e.jsx("ul",{className:"mt-1 space-y-0.5",children:N[s.id].map(r=>e.jsxs("li",{className:"text-muted-foreground",children:[B(new Date(r.created_at),"HH:mm")," · ",r.status," ",r.note?`— ${r.note}`:""]},r.id))})]}):null]})]}),t&&e.jsx("div",{className:"mt-3",children:e.jsx(ne,{orderId:s.id,destination:(n=s.address_snapshot)!=null&&n.latitude&&((m=s.address_snapshot)!=null&&m.longitude)?{lat:Number(s.address_snapshot.latitude),lng:Number(s.address_snapshot.longitude)}:null,height:240})})]})};return e.jsxs("div",{className:"min-h-screen bg-background",children:[e.jsx("header",{className:"bg-secondary text-secondary-foreground px-4 py-4",children:e.jsxs("div",{className:"max-w-6xl mx-auto flex items-center gap-3",children:[e.jsx(E,{to:`/${j}/business/${p.slug??p.id}/manage`,children:e.jsx(_,{variant:"ghost",size:"icon",className:"text-secondary-foreground hover:bg-white/10",children:e.jsx(le,{className:"w-4 h-4"})})}),e.jsxs("div",{children:[e.jsx("h1",{className:"font-black text-lg leading-tight",children:p.name}),e.jsx("p",{className:"text-xs opacity-80",children:"Delivery board · rider assignment & tracking"})]})]})}),e.jsxs("main",{className:"max-w-6xl mx-auto px-4 py-6 space-y-8",children:[e.jsxs("section",{children:[e.jsxs("div",{className:"flex items-center gap-2 mb-3",children:[e.jsx(se,{className:"w-4 h-4"}),e.jsxs("h2",{className:"font-black",children:["Awaiting rider (",y.length,")"]})]}),e.jsx("div",{className:"grid md:grid-cols-2 gap-3",children:y.length===0?e.jsx(h,{className:"p-6 text-center text-sm text-muted-foreground border-dashed",children:"Nothing to assign."}):y.map(s=>e.jsx(v,{o:s,showAssign:!0,showAdvance:!1},s.id))})]}),e.jsxs("section",{children:[e.jsxs("h2",{className:"font-black mb-3",children:["In flight (",b.length,")"]}),e.jsx("div",{className:"grid md:grid-cols-2 gap-3",children:b.length===0?e.jsx(h,{className:"p-6 text-center text-sm text-muted-foreground border-dashed",children:"No active deliveries."}):b.map(s=>e.jsx(v,{o:s,showAssign:!1,showAdvance:!0},s.id))})]}),e.jsxs("section",{children:[e.jsxs("h2",{className:"font-black mb-3",children:["Delivered (",R.length,")"]}),e.jsx("div",{className:"grid md:grid-cols-2 gap-3",children:R.slice(0,10).map(s=>e.jsx(v,{o:s,showAssign:!1,showAdvance:!1},s.id))})]}),e.jsxs("section",{children:[e.jsx("h2",{className:"font-black mb-3",children:"Rider payouts"}),e.jsx("div",{className:"grid md:grid-cols-2 gap-3",children:Object.keys(w).length===0?e.jsx(h,{className:"p-6 text-center text-sm text-muted-foreground border-dashed",children:"All rider payouts are settled."}):Object.entries(w).map(([s,a])=>{var t,i,n;return e.jsx(h,{className:"p-4 border-2",children:e.jsxs("div",{className:"flex items-start justify-between gap-3 flex-wrap",children:[e.jsxs("div",{children:[e.jsx("p",{className:"font-black",children:((t=a.rider)==null?void 0:t.full_name)||"Rider"}),e.jsxs("p",{className:"text-xs text-muted-foreground",children:[(i=a.rider)==null?void 0:i.vehicle_type," · ",(n=a.rider)==null?void 0:n.phone]}),e.jsxs("p",{className:"text-xs mt-1",children:[a.count," unpaid deliver",a.count===1?"y":"ies"]})]}),e.jsxs("div",{className:"text-right",children:[e.jsxs("p",{className:"font-black text-primary text-lg",children:[a.currency," ",a.amount.toFixed(2)]}),e.jsx(_,{size:"sm",className:"mt-2",disabled:k===s,onClick:()=>K(s),children:k===s?"Releasing…":"Release payout"})]})]})},s)})})]})]})]})};export{_e as default};

Line numbers count LF bytes from the start of the resource, as the search results do. Vendor segments are library code the classifier recognised; they are stored but not indexed. Bytes are shown as Latin1 characters, one per byte.