worker_CLEAN_v90_bounces.js // ODH BOUNCE/DROPPED scanner // POST = primary API // GET = testing/health // 5 HubSpot portals + Gmail status

// workercleanv90bouncesjs-odh` // ODH BOUNCE/DROPPED scanner // POST = primary API // GET = testing/health // 5 HubSpot portals + Gmail status export default { async fetch(request, env, ctx) { const url = new URL(request.url); const pathname = url.pathname; const PORTALS = [ { id: "147656904", name: "PROD NA2 FREE", role: "DOWNGRADED FREE Sep25 Ticket40821205635", real_money: true, url: "app-na2.hubspot.com", env_keys: [ "HUBSPOT_TOKEN_147656904", "HUBSPOT_PRIVATE_APP_147656904" ] }, { id: "147530924", name: "PROD EU1 GOOD", role: "$28,671.50 intact deal 454151867621", real_money: true, url: "app-eu1.hubspot.com", env_keys: ["HUBSPOT_TOKEN_147530924"] }, { id: "147365869", name: "PROD EU1 TERMINATED", role: "73,838 contacts $39.68 outstanding", real_money: true, url: "app-eu1.hubspot.com", env_keys: ["HUBSPOT_TOKEN_147365869"] }, { id: "148000629", name: "DEV TEST NA1", role: "INV-1001 $500 + INV-1002 $999.95 simulated", real_money: false, url: "app-na1.hubspot.com", env_keys: ["HUBSPOT_TOKEN_148000629"] }, { id: "147751226", name: "DEV TEST EU1", role: "427303774394 / 426332731637 / 436948401391", real_money: false, url: "app-eu1.hubspot.com", env_keys: ["HUBSPOT_TOKEN_147751226"] } ]; async function getTokenForPortal(portal) { for (const key of portal.env_keys) { if (env[key]) return env[key]; } return env.HUBSPOT_TOKEN || null; } async function fetchEmailEventsForPortal( portal, token, eventType, limit ) { if (!token) { return { eventType, status: "NO_TOKEN", events: [], count: 0 }; } try { const apiUrl = `https://api.hubapi.com/email/public/v1/events` + `?eventType=${encodeURIComponent(eventType)}` + `&limit=${encodeURIComponent(limit)}`; const res = await fetch(apiUrl, { headers: { Authorization: `Bearer ${token}` } }); if (!res.ok) { const body = await res.text(); return { eventType, status: `API_ERROR ${res.status}`, detail: body.slice(0, 300), events: [], count: 0 }; } const data = await res.json(); const events = (data.events || []).map((e) => ({ id: e.id, email: e.recipient || e.email || "", eventType: e.type || eventType, bounceReason: e.bounceReason || e.dropReason || "", created: e.created ? new Date(e.created).toISOString() : null, portal_id: portal.id })); return { eventType, status: `LIVE ${events.length} ${eventType}`, events, count: events.length, live: true }; } catch (err) { return { eventType, status: `EXCEPTION ${err.message}`, events: [], count: 0 }; } } async function runBounceScan(options = {}) { const limit = Math.min( Math.max(Number(options.limit || 100), 1), 500 ); const includeDev = options.include_dev !== false; const selectedPortals = includeDev ? PORTALS : PORTALS.filter((p) => p.real_money); const results = []; let totalBounce = 0; let totalDropped = 0; let livePortals = 0; for (const portal of selectedPortals) { const token = await getTokenForPortal(portal); const result = { portal_id: portal.id, portal_name: portal.name, real_money: portal.real_money, token_present: !!token, bounces: [], dropped: [], counts: {}, statuses: [] }; if (token) livePortals++; const bounce = await fetchEmailEventsForPortal( portal, token, "BOUNCE", limit ); const dropped = await fetchEmailEventsForPortal( portal, token, "DROPPED", limit ); result.bounces = bounce.events; result.dropped = dropped.events; result.counts = { bounce: bounce.count, dropped: dropped.count, total: bounce.count + dropped.count }; result.statuses = [ bounce.status, dropped.status ]; totalBounce += bounce.count; totalDropped += dropped.count; results.push(result); } const gmail = env.GMAIL_ACCESS_TOKEN ? { status: "TOKEN_PRESENT — Gmail integration available" } : { status: "NO_TOKEN — Gmail live scan not executed", known_verp_examples: [ "1823144...@email.medium.com", "01000198...@email.amazonses.com" ], brian_ooo: "partners@activecampaign.com SENT — not classified as bounce" }; return { endpoint: "/api/email/bounces", version: "v90", method: "POST", timestamp: new Date().toISOString(), request: { limit, include_dev }, summary: { portals_scanned: selectedPortals.length, live_portals: livePortals, total_bounce: totalBounce, total_dropped: totalDropped, total_all: totalBounce + totalDropped }, results, gmail, contact: "ONLY +2347048858955" }; } /* * PRIMARY API * * POST /api/email/bounces */ if ( pathname === "/api/email/bounces" && request.method === "POST" ) { let body = {}; try { if (request.headers.get("content-type")?.includes("application/json")) { body = await request.json(); } } catch { return new Response( JSON.stringify({ error: "INVALID_JSON", message: "Request body must contain valid JSON." }, null, 2), { status: 400, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } } ); } const result = await runBounceScan(body); return new Response( JSON.stringify(result, null, 2), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "no-store", "Access-Control-Allow-Origin": "*" } } ); } /* * GET = TESTING / HEALTH * * GET /api/email/bounces * GET /api/email/bounces?limit=25&include_dev=true */ if ( pathname === "/api/email/bounces" && request.method === "GET" ) { const limit = Number(searchParams.get("limit") || 25); const includeDev = searchParams.get("include_dev") !== "false"; const result = await runBounceScan({ limit, include_dev: includeDev }); return new Response( JSON.stringify( { ...result, method: "GET_TEST" }, null, 2 ), { status: 200, headers: { "Content-Type": "application/json", "Cache-Control": "no-store" } } ); } /* * OPTIONS = CORS PREFLIGHT */ if ( pathname === "/api/email/bounces" && request.method === "OPTIONS" ) { return new Response(null, { status: 204, headers: { "Access-Control-Allow-Origin": "*", "Access-Control-Allow-Methods": "GET, POST, OPTIONS", "Access-Control-Allow-Headers": "Content-Type, Authorization" } }); } /* * Existing diagnostic endpoint */ if ( pathname === "/api/hubspot/deals/all-25k-scan" ) { return new Response( JSON.stringify({ scan: "25k deals", note: "Use v80 full logic for deals. v90 focuses on BOUNCE/DROPPED.", portals: PORTALS.length }), { headers: { "Content-Type": "application/json" } } ); } /* * Security/export review */ if ( pathname === "/api/security/export-review-47834085433" ) { return new Response( JSON.stringify({ ticket: "47834085433", downgrade_ticket: "40821205635", primary: "147656904 FREE", bounce_api: "/api/email/bounces", primary_method: "POST", testing_method: "GET", brian_OOO: "partners@activecampaign.com SENT" }), { headers: { "Content-Type": "application/json" } } ); } /* * Default health response */ return new Response( JSON.stringify({ service: "ODH BOUNCE/DROPPED SCANNER", version: "v90", status: "LIVE", primary_api: "POST /api/email/bounces", test_api: "GET /api/email/bounces?limit=25", portals: 5 }, null, 2), { headers: { "Content-Type": "application/json" } } ); } };

Comments

This is a really useful setup. The Blogger-to-Cloudflare Worker connection makes the email monitoring workflow much clearer, especially with BOUNCE and DROPPED events separated across the HubSpot portals. Great work making the technical side easier to understand.

Popular posts from this blog

OGHENEKUME DIGITAL HUB Building in public · Delta State, Nigeria

My Journey Into Building a Side Hustle Digital Business

ODH Business Suite