-
Notifications
You must be signed in to change notification settings - Fork 0
/
yswsToLoops.js
163 lines (132 loc) · 5.41 KB
/
yswsToLoops.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
import fs from 'fs';
import Airtable from 'airtable';
import { LoopsClient } from 'loops';
import dotenv from 'dotenv';
// Load environment variables from .env file
dotenv.config();
async function fetchAllRecords(apiKey, baseId, tableName) {
console.log(`\n📊 Fetching records from Airtable...`);
console.log(`Base ID: ${baseId}`);
console.log(`Table Name: ${tableName}`);
// Configure Airtable
Airtable.configure({ apiKey });
const base = Airtable.base(baseId);
let records = [];
try {
const query = await base(tableName).select();
const pageRecords = await query.all();
records = records.concat(pageRecords);
console.log(`✅ Successfully fetched ${records.length} records from Airtable`);
return records;
} catch (error) {
console.error('❌ Failed to fetch records:', error);
throw error;
}
}
async function updateLoopsContact(loops, email, data) {
try {
// Truncate referral reason to 490 characters if it exists
const truncatedReferralReason = data.firstStatedReferralReason
? data.firstStatedReferralReason.slice(0, 490)
: '';
console.log(`\n📝 Updating contact: ${email}`);
console.log('Data to update:');
console.log('- Weighted Grant Contribution:', data.weightedGrantContribution);
console.log('- First Stated Referral Reason:', truncatedReferralReason || '(none)');
if (data.firstStatedReferralReason && data.firstStatedReferralReason.length > 490) {
console.log(`⚠️ Referral reason truncated from ${data.firstStatedReferralReason.length} to 490 characters`);
}
console.log('- First Stated Referral Category:', data.firstReferralReasonCategory || '(none)');
await loops.updateContact(
email,
{
calculatedYswsWeightedGrantContribution: data.weightedGrantContribution,
calculatedYswsFirstStatedReferralReason: truncatedReferralReason,
calculatedYswsFirstStatedReferralCategory: data.firstReferralReasonCategory || '',
calculatedYswsLastUpdatedAt: new Date().toISOString()
}
);
console.log(`✅ Successfully updated contact: ${email}`);
return true;
} catch (error) {
console.error(`❌ Error updating contact ${email}:`, error);
throw error;
}
}
// Validate environment variables
console.log('\n🔑 Validating environment variables...');
const apiKey = process.env.AIRTABLE_API_KEY;
const baseId = process.env.AIRTABLE_BASE_ID || 'app3A5kJwYqxMLOgh';
const tableName = process.env.AIRTABLE_TABLE_NAME || 'tblzWWGUYHVH7Zyqf';
const loopsApiKey = process.env.LOOPS_API_KEY;
if (!apiKey) throw new Error('AIRTABLE_API_KEY is required');
if (!baseId) throw new Error('AIRTABLE_BASE_ID is required');
if (!loopsApiKey) throw new Error('LOOPS_API_KEY is required');
console.log('✅ Environment variables validated');
// Initialize Loops client
console.log('\n🔄 Initializing Loops client...');
const loops = new LoopsClient(loopsApiKey);
console.log('✅ Loops client initialized');
// Fetch and process records
const records = await fetchAllRecords(apiKey, baseId, tableName);
const yswsProjects = records.map(r => r.fields);
console.log('\n📊 Processing records...');
const emailData = yswsProjects.reduce((acc, project) => {
let email = project['Email'];
// Trim whitespace from the email
if (typeof email === 'string') {
email = email.trim();
}
if (!email) {
console.warn('⚠️ Found record without email, skipping...');
return acc;
}
// Enhanced regex to validate email format more strictly
const emailRegex = /^[^\s@]+@[^\s@]+\.[^\s@]+$/;
if (!emailRegex.test(email)) {
console.warn(`⚠️ Invalid email format: ${email}, skipping...`);
return acc;
}
// Properly round to the nearest 0.1 to avoid floating-point issues
const contribution = parseFloat((project['YSWS–Weighted Grant Contribution'] || 0).toFixed(1));
const referralReason = project['How did you hear about this?'];
const referralCategory = project['Referral Reason'];
if (!acc[email]) {
acc[email] = {
weightedGrantContribution: 0,
firstStatedReferralReason: null,
firstReferralReasonCategory: null
};
}
acc[email].weightedGrantContribution += contribution;
// Only set referral info if we don't have it yet and referralReason exists
if (!acc[email].firstStatedReferralReason && referralReason) {
acc[email].firstStatedReferralReason = referralReason;
acc[email].firstReferralReasonCategory = referralCategory;
}
// Round the total contribution to the nearest 0.1 after accumulation
acc[email].weightedGrantContribution = parseFloat(acc[email].weightedGrantContribution.toFixed(1));
return acc;
}, {});
// Log processing results
console.log(`\n📊 Processing complete:`);
console.log(`- Total unique emails: ${Object.keys(emailData).length}`);
console.log(`- Total records processed: ${yswsProjects.length}`);
// Update Loops.so contacts
console.log(`\n🔄 Starting Loops.so contact updates...`);
let successCount = 0;
let failureCount = 0;
for (const [email, data] of Object.entries(emailData)) {
try {
await updateLoopsContact(loops, email, data);
successCount++;
} catch (error) {
failureCount++;
// Error already logged in updateLoopsContact
}
}
// Final summary
console.log('\n📊 Final Summary:');
console.log(`✅ Successfully updated: ${successCount} contacts`);
console.log(`❌ Failed updates: ${failureCount} contacts`);
console.log(`🏁 Process completed at: ${new Date().toISOString()}`);