// Code by Zapier Input Data: response = MCP Content Text (the JSON object). const data = JSON.parse(inputData.response); if (data.serviceUnknown || !data.service?.domain || !Array.isArray(data.companies)) { throw new Error("The response has no recognized service and company list."); } const now = new Date(); const cutoff = now.getTime() - 30 * 24 * 60 * 60 * 1000; const selected = new Map(); for (const company of data.companies) { const seen = Date.parse(company.lastDetectedAt || ""); const eligible = ["detected", "purchased"].includes(company.match) && company.recentlyNotDetected === false && Number.isFinite(seen) && seen >= cutoff && seen <= now.getTime(); if (!eligible || !company.domain) continue; const pairKey = `${data.service.domain}|${company.domain}`; const row = { pair_key: pairKey, company: company.name || company.domain, domain: company.domain, service: data.service.domain, location: company.location || "Not available", finding: company.match, last_detected: company.lastDetectedAt, last_checked: company.lastCheckedAt || "Not available", figures_locked: company.locked ? "Yes" : "No", exported_at: now.toISOString() }; const previous = selected.get(pairKey); if (!previous || Date.parse(previous.last_detected) < seen) selected.set(pairKey, row); } const rows = [...selected.values()]; if (rows.length > 500) { throw new Error("Split this page into batches of at most 500 before exporting."); } return { count: rows.length, next_page: JSON.stringify(data.next ?? null), line_items: rows };