|
| 1 | +require('dotenv').config('../.env') |
| 2 | +const courseTags = require('./course_tags.json') |
| 3 | + |
| 4 | +const API_URL = `https://${process.env.ACTIVE_CAMPAIGN_API_URL}.api-us1.com/api/3` |
| 5 | +const API_TOKEN = process.env.ACTIVE_CAMPAIGN_API_KEY |
| 6 | + |
| 7 | +const headers = { |
| 8 | + Accept: 'application/json', |
| 9 | + 'Content-Type': 'application/json', |
| 10 | + 'Api-Token': API_TOKEN, |
| 11 | +} |
| 12 | + |
| 13 | +async function makeRequest(endpoint, method = 'GET', body = null) { |
| 14 | + const options = { |
| 15 | + method, |
| 16 | + headers, |
| 17 | + ...(body && { body: JSON.stringify(body) }), |
| 18 | + } |
| 19 | + |
| 20 | + const response = await fetch(`${API_URL}${endpoint}`, options) |
| 21 | + if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`) |
| 22 | + return response.json() |
| 23 | +} |
| 24 | + |
| 25 | +async function addContactToList(contactId, listId) { |
| 26 | + try { |
| 27 | + const response = await makeRequest('/contactLists', 'POST', { |
| 28 | + contactList: { |
| 29 | + list: listId, |
| 30 | + contact: contactId, |
| 31 | + status: 1, |
| 32 | + }, |
| 33 | + }) |
| 34 | + console.log(`Contact added to list ${listId} successfully`) |
| 35 | + return response |
| 36 | + } catch (error) { |
| 37 | + console.error(`Error adding contact to list ${listId}:`, error) |
| 38 | + throw error |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +async function searchContactByEmail(email) { |
| 43 | + try { |
| 44 | + const endpoint = `/contacts?search=${email}` |
| 45 | + const response = await makeRequest(endpoint) |
| 46 | + |
| 47 | + if (response.contacts && response.contacts.length > 0) { |
| 48 | + return response.contacts[0].id |
| 49 | + } |
| 50 | + throw new Error('Contact not found') |
| 51 | + } catch (error) { |
| 52 | + console.error('Error searching contact:', error) |
| 53 | + throw error |
| 54 | + } |
| 55 | +} |
| 56 | + |
| 57 | +exports.createActiveCampaignUser = async function (user) { |
| 58 | + try { |
| 59 | + // Create or update contact |
| 60 | + const { contact } = await makeRequest('/contacts', 'POST', { |
| 61 | + contact: { |
| 62 | + email: user.email, |
| 63 | + firstName: user.name || '', |
| 64 | + fieldValues: [ |
| 65 | + { |
| 66 | + field: '2', |
| 67 | + value: user.photoUrl, |
| 68 | + }, |
| 69 | + { |
| 70 | + field: '13', |
| 71 | + value: 'BUILD_PLATFORM_API', |
| 72 | + }, |
| 73 | + ], |
| 74 | + }, |
| 75 | + }) |
| 76 | + |
| 77 | + await addContactToList(contact.id, 1) // Add to master list |
| 78 | + |
| 79 | + console.log('User added to ActiveCampaign and lists successfully') |
| 80 | + return contact |
| 81 | + } catch (error) { |
| 82 | + console.error('Error in ActiveCampaign operations:', error) |
| 83 | + throw error |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +exports.fetchUSer = async function () { |
| 88 | + let userId = 2792 |
| 89 | + console.log('Fetching user from ActiveCampaign...', userId) |
| 90 | + try { |
| 91 | + const contact = await makeRequest(`/contacts/${userId}`) |
| 92 | + console.log('User retrieved from ActiveCampaign successfully') |
| 93 | + return contact |
| 94 | + } catch (error) { |
| 95 | + console.error('Error fetching user from ActiveCampaign:', error) |
| 96 | + throw error |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +exports.fetchCustomFieldMeta = async function () { |
| 101 | + console.log('Fetching custom field metadata from ActiveCampaign...') |
| 102 | + try { |
| 103 | + const endpoint = '/tags' |
| 104 | + const params = '?limit=100' |
| 105 | + const response = await makeRequest(endpoint + params) |
| 106 | + console.log('Custom field metadata retrieved successfully') |
| 107 | + return response |
| 108 | + } catch (error) { |
| 109 | + console.error('Error fetching custom field metadata:', error) |
| 110 | + throw error |
| 111 | + } |
| 112 | +} |
| 113 | + |
| 114 | +exports.updateUserLessonProgress = async function (user, lessonData, cohortData) { |
| 115 | + try { |
| 116 | + // First find the contact ID |
| 117 | + const contactId = await searchContactByEmail(user.email) |
| 118 | + |
| 119 | + // Update the contact with new field values |
| 120 | + const contactData = { |
| 121 | + contact: { |
| 122 | + fieldValues: [ |
| 123 | + { |
| 124 | + field: '9', // last_course field ID |
| 125 | + value: cohortData.course_id || '', |
| 126 | + }, |
| 127 | + { |
| 128 | + field: '10', // cohort_name field ID |
| 129 | + value: cohortData.name || '', |
| 130 | + }, |
| 131 | + { |
| 132 | + field: '15', // last_section field ID |
| 133 | + value: lessonData.section || '', |
| 134 | + }, |
| 135 | + ], |
| 136 | + }, |
| 137 | + } |
| 138 | + |
| 139 | + if (user.name) { |
| 140 | + contactData.contact.firstName = user.name |
| 141 | + } |
| 142 | + |
| 143 | + const response = await makeRequest(`/contacts/${contactId}`, 'PUT', contactData) |
| 144 | + |
| 145 | + return response |
| 146 | + } catch (error) { |
| 147 | + console.error('Error updating lesson progress in ActiveCampaign:', { |
| 148 | + error: error.message, |
| 149 | + userEmail: user.email, |
| 150 | + stack: error.stack, |
| 151 | + }) |
| 152 | + throw error |
| 153 | + } |
| 154 | +} |
| 155 | + |
| 156 | +exports.addCourseTagToUser = async function (email, courseId) { |
| 157 | + try { |
| 158 | + // Find contact ID by email |
| 159 | + const contactId = await searchContactByEmail(email) |
| 160 | + |
| 161 | + // Get tag ID from the mapping |
| 162 | + const tagId = courseTags[courseId] |
| 163 | + if (!tagId) { |
| 164 | + throw new Error(`Tag ID not found for course: ${courseId}`) |
| 165 | + } |
| 166 | + |
| 167 | + // Create tag and add to contact |
| 168 | + const response = await makeRequest('/contactTags', 'POST', { |
| 169 | + contactTag: { |
| 170 | + contact: contactId, |
| 171 | + tag: tagId, |
| 172 | + }, |
| 173 | + }) |
| 174 | + |
| 175 | + console.log(`Tag ${courseId} (ID: ${tagId}) added to contact successfully`) |
| 176 | + return response |
| 177 | + } catch (error) { |
| 178 | + console.error('Error adding course tag in ActiveCampaign:', { |
| 179 | + error: error.message, |
| 180 | + userEmail: email, |
| 181 | + tagName, |
| 182 | + stack: error.stack, |
| 183 | + }) |
| 184 | + throw error |
| 185 | + } |
| 186 | +} |
| 187 | + |
| 188 | +exports.addTagToUserCertificate = async function (email, courseId) { |
| 189 | + try { |
| 190 | + const contactId = await searchContactByEmail(email) |
| 191 | + courseId |
| 192 | + |
| 193 | + console.log('courseId'); |
| 194 | + console.log(courseId); |
| 195 | + |
| 196 | + |
| 197 | + const tagId = courseTags[courseId] |
| 198 | + if (!tagId) { |
| 199 | + throw new Error(`Tag ID not found for course: ${courseId}`) |
| 200 | + } |
| 201 | + |
| 202 | + const response = await makeRequest('/contactTags', 'POST', { |
| 203 | + contactTag: { |
| 204 | + contact: contactId, |
| 205 | + tag: tagId, |
| 206 | + }, |
| 207 | + }) |
| 208 | + |
| 209 | + console.log(`Tag ${courseId} (ID: ${tagId}) added to contact successfully`) |
| 210 | + return response |
| 211 | + } catch (error) { |
| 212 | + console.error('Error adding certificate tag to user in ActiveCampaign:', { |
| 213 | + error: error.message, |
| 214 | + userEmail: email, |
| 215 | + stack: error.stack, |
| 216 | + }) |
| 217 | + throw error |
| 218 | + } |
| 219 | +} |
0 commit comments