| 20 | |
| 21 | // Standalone Kit Add Subscriber Function (Tag Based) |
| 22 | async function addSubscriberToKit(email, firstName) { |
| 23 | const TAG_ID = "15119471"; // OpenStock Users |
| 24 | const url = `https://api.convertkit.com/v3/tags/${TAG_ID}/subscribe`; |
| 25 | |
| 26 | // Auto-detect first name if missing |
| 27 | if (!firstName) firstName = "Subscriber"; |
| 28 | |
| 29 | try { |
| 30 | const response = await fetch(url, { |
| 31 | method: 'POST', |
| 32 | headers: { 'Content-Type': 'application/json' }, |
| 33 | body: JSON.stringify({ |
| 34 | api_key: KIT_API_KEY, |
| 35 | email: email, |
| 36 | first_name: firstName, |
| 37 | }), |
| 38 | }); |
| 39 | |
| 40 | if (!response.ok) { |
| 41 | const err = await response.text(); |
| 42 | |
| 43 | // Rate Limit Handling |
| 44 | if (response.status === 429 || err.includes('Retry later')) { |
| 45 | console.log("⚠️ Rate Limit Hit. Cooling down for 10s..."); |
| 46 | await new Promise(r => setTimeout(r, 10000)); |
| 47 | return false; // Will be retried next run since we don't update DB |
| 48 | } |
| 49 | throw new Error(`Kit API Error: ${err}`); |
| 50 | } |
| 51 | return true; |
| 52 | } catch (e) { |
| 53 | // If "already subscribed", treat as success |
| 54 | if (e.message && e.message.includes('already')) return true; |
| 55 | |
| 56 | // Log valid errors but don't crash |
| 57 | // console.error(`❌ Failed to add ${email}:`, e.message); |
| 58 | process.stdout.write("x"); |
| 59 | return false; |
| 60 | } |
| 61 | } |
| 62 | |
| 63 | async function runMigration() { |
| 64 | try { |