diff --git a/src/http.ts b/src/http.ts index 69ce27b..70c5227 100644 --- a/src/http.ts +++ b/src/http.ts @@ -236,6 +236,207 @@ app.post("/webhooks/paperfly", (req: Request, res: Response) => { }); }); +/** + * Live Consumer & Merchant Cyber-Dark Tracking Hub + * GET /track/:code + */ +app.get("/track/:code", async (req: Request, res: Response) => { + const code = req.params.code; + const format = req.query.format as string | undefined; + + let courier = (req.query.courier as string) || ""; + if (!courier) { + if (code.toUpperCase().startsWith("DS") || code.toUpperCase().startsWith("DB")) courier = "pathao"; + else if (code.toUpperCase().startsWith("SF") || /^\d{8,12}$/.test(code)) courier = "steadfast"; + else if (code.toUpperCase().startsWith("REDX") || code.toUpperCase().startsWith("RX")) courier = "redx"; + else if (code.toUpperCase().startsWith("PF")) courier = "paperfly"; + else courier = "pathao"; + } + + const registry = new CourierRegistry(loadConfig(keysFromRequest(req))); + let trackResult: any = null; + let errorMsg: string | null = null; + + try { + trackResult = await registry.trackParcel(code, courier as any); + } catch (err: any) { + errorMsg = err?.message || String(err); + } + + if (format === "json" || (!req.headers.accept?.includes("text/html") && format !== "html")) { + return res.json(trackResult || { success: false, tracking_code: code, error: errorMsg }); + } + + const statusText = trackResult?.status || (trackResult?.raw_response?.data?.order_status) || "In Transit"; + const raw = trackResult?.raw_response?.data || trackResult?.raw_response || {}; + const recipientName = raw.recipient_name || raw.customer_name || raw.name || "Verified Customer"; + const recipientAddress = raw.recipient_address || raw.address || "Bangladesh"; + const codAmount = raw.order_amount || raw.amount_to_collect || raw.cod_amount || raw.collectable_amount || 0; + const carrierName = courier === "pathao" ? "Pathao Express" : (courier === "steadfast" ? "Steadfast Courier" : courier === "redx" ? "RedX Delivery" : courier === "paperfly" ? "Paperfly Go" : courier.toUpperCase()); + const shortLink = raw.short_link || (courier === "pathao" ? `https://merchant.pathao.com/tracking?consignment_id=${code}` : (courier === "steadfast" ? `https://steadfast.com.bd/t/${code}` : "")); + const updatedAt = raw.order_status_updated_at || trackResult?.updated_at || new Date().toLocaleString(); + + const isDelivered = statusText.toLowerCase().includes("deliv"); + const isPickedUp = isDelivered || statusText.toLowerCase().includes("transit") || statusText.toLowerCase().includes("hub") || statusText.toLowerCase().includes("way") || statusText.toLowerCase().includes("pick"); + + res.type("html").send(` + +
+ + +${carrierName} • Direct Live Telemetry
+