feat(admin): implement Ready for Delivery workflow with Shiprocket dispatch and optimize catalog queries
This commit is contained in:
parent
d94f74b0c2
commit
ad952166cd
4 changed files with 193 additions and 33 deletions
|
|
@ -302,10 +302,22 @@ export default function MigrationPage() {
|
|||
setElapsedSeconds(0);
|
||||
toast.success(`Import job #${String(activeId).slice(0, 8)} enqueued! Telemetry active.`);
|
||||
|
||||
// Connect SSE Live Stream
|
||||
// Connect SSE Live Stream with one-time single-use ticket
|
||||
if (activeId) {
|
||||
let sseUrl = `${apiBase}/jobs/${activeId}/stream`;
|
||||
try {
|
||||
const ticketRes = await authFetch(`${apiBase}/sse-ticket`, { method: 'POST' });
|
||||
if (ticketRes.ok) {
|
||||
const ticketData = await ticketRes.json();
|
||||
if (ticketData?.ticket) {
|
||||
sseUrl = `${apiBase}/jobs/${activeId}/stream?ticket=${encodeURIComponent(ticketData.ticket)}`;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
const token = getAuthToken();
|
||||
const sseUrl = `${apiBase}/jobs/${activeId}/stream${token ? `?token=${encodeURIComponent(token)}` : ''}`;
|
||||
if (token) sseUrl = `${apiBase}/jobs/${activeId}/stream?token=${encodeURIComponent(token)}`;
|
||||
}
|
||||
|
||||
const eventSource = new EventSource(sseUrl);
|
||||
eventSource.onmessage = (event) => {
|
||||
try {
|
||||
|
|
|
|||
|
|
@ -64,6 +64,10 @@ interface AdminOrderDetailResponse {
|
|||
billing_address_json: string | null;
|
||||
tracking_number: string | null;
|
||||
courier_name: string | null;
|
||||
shipping_label_url?: string | null;
|
||||
package_weight?: number | null;
|
||||
logistics_provider?: string | null;
|
||||
pickup_scheduled?: boolean;
|
||||
created_at: string;
|
||||
items: OrderItemDetail[];
|
||||
history: StatusHistorySchema[];
|
||||
|
|
@ -115,6 +119,7 @@ interface AdminOrderListResponse {
|
|||
coupon_code?: string | null;
|
||||
status: string;
|
||||
payment_status: string;
|
||||
shipping_label_url?: string | null;
|
||||
created_at: string;
|
||||
}
|
||||
|
||||
|
|
@ -134,7 +139,7 @@ function statusKey(status?: string) {
|
|||
|
||||
function isOpenStatus(status?: string) {
|
||||
const s = statusKey(status);
|
||||
return s === 'PENDING' || s === 'CONFIRMED' || s === 'PROCESSING';
|
||||
return s === 'PENDING' || s === 'CONFIRMED' || s === 'PROCESSING' || s === 'READY_FOR_DELIVERY';
|
||||
}
|
||||
|
||||
function isShippedStatus(status?: string) {
|
||||
|
|
@ -151,9 +156,13 @@ function isCancelledStatus(status?: string) {
|
|||
}
|
||||
|
||||
function statusBadgeClass(status?: string) {
|
||||
if (isDeliveredStatus(status)) return 'bg-success text-white';
|
||||
if (isCancelledStatus(status)) return 'bg-destructive text-white';
|
||||
if (isShippedStatus(status)) return 'bg-primary text-white';
|
||||
const k = statusKey(status);
|
||||
if (k === 'DELIVERED') return 'bg-emerald-600 text-white';
|
||||
if (k === 'CANCELLED') return 'bg-destructive text-white';
|
||||
if (k === 'SHIPPED') return 'bg-blue-600 text-white';
|
||||
if (k === 'OUT_FOR_DELIVERY') return 'bg-purple-600 text-white';
|
||||
if (k === 'READY_FOR_DELIVERY') return 'bg-amber-600 text-white';
|
||||
if (k === 'CONFIRMED' || k === 'ORDER_CONFIRMED') return 'bg-teal-600 text-white';
|
||||
return 'bg-warning text-white';
|
||||
}
|
||||
|
||||
|
|
@ -172,6 +181,8 @@ export default function OrderIntelligencePage() {
|
|||
const [verifiedItems, setVerifiedItems] = useState<string[]>([]);
|
||||
const [showStatusForm, setShowStatusForm] = useState(false);
|
||||
const [newStatus, setNewStatus] = useState('PROCESSING');
|
||||
const [logisticsProvider, setLogisticsProvider] = useState<'SHIPROCKET' | 'LOCAL_DELIVERY'>('SHIPROCKET');
|
||||
const [packageWeight, setPackageWeight] = useState<string>('0.5');
|
||||
const [trackingNo, setTrackingNo] = useState('');
|
||||
const [courier, setCourier] = useState('');
|
||||
const [changeReason, setChangeReason] = useState('');
|
||||
|
|
@ -742,9 +753,20 @@ export default function OrderIntelligencePage() {
|
|||
|
||||
const handleOpenStatusForm = () => {
|
||||
if (!detailData) return;
|
||||
const curKey = (detailData.status || '').toUpperCase();
|
||||
if (['PENDING', 'ORDER_CREATED', 'PAYMENT_PENDING'].includes(curKey)) {
|
||||
setNewStatus('PENDING');
|
||||
} else if (['CONFIRMED', 'ORDER_CONFIRMED', 'PROCESSING'].includes(curKey)) {
|
||||
setNewStatus('CONFIRMED');
|
||||
} else if (curKey === 'READY_FOR_DELIVERY') {
|
||||
setNewStatus('READY_FOR_DELIVERY');
|
||||
} else {
|
||||
setNewStatus(detailData.status);
|
||||
}
|
||||
setTrackingNo(detailData.tracking_number || '');
|
||||
setCourier(detailData.courier_name || '');
|
||||
setPackageWeight(detailData.package_weight ? String(detailData.package_weight) : '0.5');
|
||||
setLogisticsProvider((detailData.logistics_provider as any) || 'SHIPROCKET');
|
||||
setChangeReason('');
|
||||
|
||||
// Pre-fill partNumbersMap
|
||||
|
|
@ -762,17 +784,27 @@ export default function OrderIntelligencePage() {
|
|||
if (!detailData) return;
|
||||
setUpdating(true);
|
||||
try {
|
||||
await apiFetch(`/api/v1/admin/orders/${detailData.order_id}/status`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify({
|
||||
const payload: any = {
|
||||
status: newStatus,
|
||||
tracking_number: trackingNo || null,
|
||||
courier_name: courier || null,
|
||||
reason: changeReason || null,
|
||||
part_numbers_mapping: Object.keys(partNumbersMap).length > 0 ? partNumbersMap : undefined,
|
||||
}),
|
||||
};
|
||||
|
||||
if (newStatus === 'READY_FOR_DELIVERY') {
|
||||
payload.logistics_provider = logisticsProvider;
|
||||
payload.package_weight = parseFloat(packageWeight) || 0.5;
|
||||
}
|
||||
|
||||
const res = await apiFetch<any>(`/api/v1/admin/orders/${detailData.order_id}/status`, {
|
||||
method: 'PUT',
|
||||
body: JSON.stringify(payload),
|
||||
});
|
||||
toast.success('Order status saved');
|
||||
toast.success(res?.message || 'Order status saved');
|
||||
if (res?.shipping_label_url) {
|
||||
toast.info('Shipping slip generated! Click "Print Shipping Slip" to print.');
|
||||
}
|
||||
setShowStatusForm(false);
|
||||
handleInspectOrder(detailData.order_id);
|
||||
fetchOrders();
|
||||
|
|
@ -1067,12 +1099,52 @@ export default function OrderIntelligencePage() {
|
|||
</div>
|
||||
</div>
|
||||
{!showStatusForm && (
|
||||
<div className="flex items-center gap-2">
|
||||
{detailData.shipping_label_url && (
|
||||
<a
|
||||
href={detailData.shipping_label_url.startsWith('http') ? detailData.shipping_label_url : `http://localhost:8000${detailData.shipping_label_url}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex items-center gap-1.5 px-3 py-1.5 bg-primary text-white crm-radius-button text-[12px] font-semibold hover:opacity-90 shadow-sm"
|
||||
>
|
||||
<Printer className="w-3.5 h-3.5" />
|
||||
Print Shipping Slip
|
||||
</a>
|
||||
)}
|
||||
<button type="button" onClick={handleOpenStatusForm} className={primaryButton}>
|
||||
Update status
|
||||
</button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
|
||||
{detailData.tracking_number && (
|
||||
<div className="p-3 bg-muted/40 border border-border crm-radius-card flex items-center justify-between text-[12px]">
|
||||
<div className="flex items-center gap-2">
|
||||
<Barcode className="w-4 h-4 text-primary" />
|
||||
<div>
|
||||
<div className="font-semibold text-foreground">
|
||||
{detailData.courier_name || 'Carrier'}: <span className="font-mono">{detailData.tracking_number}</span>
|
||||
</div>
|
||||
<div className="text-[10px] text-muted-foreground">
|
||||
Status: {(detailData.status || '').replace('_', ' ')}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{detailData.shipping_label_url && (
|
||||
<a
|
||||
href={detailData.shipping_label_url.startsWith('http') ? detailData.shipping_label_url : `http://localhost:8000${detailData.shipping_label_url}`}
|
||||
target="_blank"
|
||||
rel="noopener noreferrer"
|
||||
className="inline-flex items-center gap-1.5 px-2.5 py-1 bg-primary/10 hover:bg-primary/20 text-primary text-[11px] font-semibold crm-radius-button"
|
||||
>
|
||||
<Printer className="w-3 h-3" />
|
||||
Print Slip
|
||||
</a>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
|
||||
<div className="space-y-2">
|
||||
<div className="flex items-center gap-2 text-[13px] font-semibold text-foreground">
|
||||
<User className="w-4 h-4 text-primary" />
|
||||
|
|
@ -1127,17 +1199,65 @@ export default function OrderIntelligencePage() {
|
|||
<CustomSelect
|
||||
value={newStatus}
|
||||
onChange={setNewStatus}
|
||||
options={[
|
||||
options={(() => {
|
||||
const curKey = (detailData.status || '').toUpperCase();
|
||||
const isPendingState = ['PENDING', 'ORDER_CREATED', 'PAYMENT_PENDING'].includes(curKey);
|
||||
if (isPendingState) {
|
||||
return [
|
||||
{ value: 'PENDING', label: 'Pending' },
|
||||
{ value: 'CONFIRMED', label: 'Confirmed' },
|
||||
{ value: 'PROCESSING', label: 'Processing' },
|
||||
{ value: 'SHIPPED', label: 'Shipped' },
|
||||
{ value: 'OUT_FOR_DELIVERY', label: 'Out for delivery' },
|
||||
{ value: 'DELIVERED', label: 'Delivered' },
|
||||
{ value: 'CANCELLED', label: 'Cancelled' },
|
||||
];
|
||||
}
|
||||
return [
|
||||
{ value: 'CONFIRMED', label: 'Confirmed' },
|
||||
{ value: 'READY_FOR_DELIVERY', label: 'Ready for delivery' },
|
||||
{ value: 'CANCELLED', label: 'Cancelled' },
|
||||
];
|
||||
})()}
|
||||
/>
|
||||
<p className="text-[10px] text-muted-foreground mt-1">
|
||||
Note: 'Shipped', 'Out for delivery', and 'Delivered' are automatically synced from Shiprocket upon courier pickup.
|
||||
</p>
|
||||
</div>
|
||||
|
||||
{newStatus === 'READY_FOR_DELIVERY' && (
|
||||
<div className="space-y-3 p-3 bg-primary/5 border border-primary/20 rounded-lg">
|
||||
<div>
|
||||
<label className={labelClass}>Delivery Provider</label>
|
||||
<CustomSelect
|
||||
value={logisticsProvider}
|
||||
onChange={(val) => setLogisticsProvider(val as any)}
|
||||
options={[
|
||||
{ value: 'SHIPROCKET', label: 'Shiprocket (Automated Pickup & Tracking)' },
|
||||
{ value: 'LOCAL_DELIVERY', label: 'Local / Store Delivery (Manual Handover)' },
|
||||
]}
|
||||
/>
|
||||
</div>
|
||||
<div>
|
||||
<label className={labelClass}>Package Weight (kg) *</label>
|
||||
<input
|
||||
type="number"
|
||||
step="0.05"
|
||||
min="0.05"
|
||||
max="50"
|
||||
placeholder="e.g. 0.50"
|
||||
value={packageWeight}
|
||||
onChange={(e) => setPackageWeight(e.target.value)}
|
||||
className={inputClass}
|
||||
required
|
||||
/>
|
||||
<p className="text-[11px] text-muted-foreground mt-1">
|
||||
Actual physical scale weight for courier billing and manifest verification.
|
||||
</p>
|
||||
</div>
|
||||
{logisticsProvider === 'SHIPROCKET' && (
|
||||
<div className="text-[11px] text-primary bg-primary/10 p-2.5 rounded border border-primary/20">
|
||||
⚡ <strong>Automated Pickup Trigger:</strong> Confirming will immediately create the order in Shiprocket, assign the AWB number, generate the printable shipping label slip, and schedule carrier pickup at the shop location.
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
)}
|
||||
{newStatus === 'SHIPPED' && (
|
||||
<>
|
||||
<div>
|
||||
|
|
|
|||
|
|
@ -339,7 +339,12 @@ export default function ProductsPage() {
|
|||
return () => clearTimeout(timer);
|
||||
}, [searchQuery]);
|
||||
|
||||
const isInitialSearchMount = useRef(true);
|
||||
useEffect(() => {
|
||||
if (isInitialSearchMount.current) {
|
||||
isInitialSearchMount.current = false;
|
||||
return;
|
||||
}
|
||||
setServerPage(1);
|
||||
fetchData(1);
|
||||
}, [debouncedSearch]);
|
||||
|
|
@ -1196,8 +1201,31 @@ export default function ProductsPage() {
|
|||
);
|
||||
};
|
||||
|
||||
const getProductDisplayImage = (p: ProductResponse): string => {
|
||||
if (p.images && p.images.length > 0) {
|
||||
const primary = p.images.find((img) => (img as any).is_primary);
|
||||
if (primary?.image_url) return primary.image_url;
|
||||
if (p.images[0]?.image_url) return p.images[0].image_url;
|
||||
}
|
||||
if ((p as any).thumbnail_url) return (p as any).thumbnail_url;
|
||||
if (p.variants && p.variants.length > 0) {
|
||||
for (const v of p.variants) {
|
||||
if (v.images && v.images.length > 0) {
|
||||
const primary = v.images.find((img) => img.is_primary);
|
||||
if (primary?.image_url) return primary.image_url;
|
||||
}
|
||||
}
|
||||
for (const v of p.variants) {
|
||||
if (v.images && v.images.length > 0 && v.images[0]?.image_url) {
|
||||
return v.images[0].image_url;
|
||||
}
|
||||
}
|
||||
}
|
||||
return '';
|
||||
};
|
||||
|
||||
const renderProductAvatar = (p: ProductResponse) => {
|
||||
const rawUrl = p.images?.[0]?.image_url || (p as any).thumbnail_url || p.variants?.[0]?.images?.[0]?.image_url;
|
||||
const rawUrl = getProductDisplayImage(p);
|
||||
const imgUrl = getMediaUrl(rawUrl);
|
||||
return (
|
||||
<div className="w-9 h-9 crm-radius-icon bg-primary/10 text-primary flex items-center justify-center shrink-0 overflow-hidden">
|
||||
|
|
@ -1960,9 +1988,9 @@ export default function ProductsPage() {
|
|||
</div>
|
||||
<div className="flex items-start gap-3 pr-8 min-w-0">
|
||||
<div className="w-10 h-10 crm-radius-icon bg-primary/10 text-primary flex items-center justify-center shrink-0 overflow-hidden">
|
||||
{(p.images?.[0]?.image_url || (p as any).thumbnail_url || p.variants?.[0]?.images?.[0]?.image_url) ? (
|
||||
{getProductDisplayImage(p) ? (
|
||||
<img
|
||||
src={getMediaUrl(p.images?.[0]?.image_url || (p as any).thumbnail_url || p.variants?.[0]?.images?.[0]?.image_url)}
|
||||
src={getMediaUrl(getProductDisplayImage(p))}
|
||||
alt={p.name}
|
||||
className="w-full h-full object-contain p-0.5"
|
||||
onError={(e) => { e.currentTarget.style.display = 'none'; }}
|
||||
|
|
|
|||
|
|
@ -46,7 +46,7 @@ export function useDashboardData() {
|
|||
const [statsRes, ordersRes, productsRes, categoriesRes, brandsRes, sessionsRes] = await Promise.allSettled([
|
||||
adminService.getDashboardStats().catch(() => null),
|
||||
apiFetch<any[]>('/api/v1/admin/orders').catch(() => []),
|
||||
apiFetch<any>('/api/v1/catalog/products/all?limit=200').catch(() => []),
|
||||
apiFetch<any>('/api/v1/catalog/products/all?limit=25').catch(() => []),
|
||||
apiFetch<any[]>('/api/v1/catalog/categories/all').catch(() => []),
|
||||
apiFetch<any[]>('/api/v1/catalog/brands/all').catch(() => []),
|
||||
adminService.listSessions(0, 50).catch(() => ({ sessions: [] })),
|
||||
|
|
|
|||
Loading…
Reference in a new issue