Skip to content

Commit

Permalink
feat: add tag with course_id on cohort signup
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin committed Nov 13, 2024
1 parent eba7a6d commit 9956f5f
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 2 deletions.
36 changes: 34 additions & 2 deletions functions/active_campaign/active_campaign.js
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
require('dotenv').config('../.env')
const courseTags = require('./course_tags.json')

const API_URL = `https://${process.env.ACTIVE_CAMPAIGN_API_URL}.api-us1.com/api/3`
const API_TOKEN = process.env.ACTIVE_CAMPAIGN_API_KEY
Expand Down Expand Up @@ -44,7 +45,6 @@ async function searchContactByEmail(email) {
const response = await makeRequest(endpoint)

if (response.contacts && response.contacts.length > 0) {
console.log('Contact found:', response.contacts[0])
return response.contacts[0].id
}
throw new Error('Contact not found')
Expand Down Expand Up @@ -100,7 +100,7 @@ exports.fetchUSer = async function () {
exports.fetchCustomFieldMeta = async function () {
console.log('Fetching custom field metadata from ActiveCampaign...')
try {
const endpoint = '/fields'
const endpoint = '/tags'
const params = '?limit=100'
const response = await makeRequest(endpoint + params)
console.log('Custom field metadata retrieved successfully')
Expand Down Expand Up @@ -152,3 +152,35 @@ exports.updateUserLessonProgress = async function (user, lessonData, cohortData)
throw error
}
}

exports.addCourseTagToUser = async function (email, courseId) {
try {
// Find contact ID by email
const contactId = await searchContactByEmail(email)

// Get tag ID from the mapping
const tagId = courseTags[courseId]
if (!tagId) {
throw new Error(`Tag ID not found for course: ${courseId}`)
}

// Create tag and add to contact
const response = await makeRequest('/contactTags', 'POST', {
contactTag: {
contact: contactId,
tag: tagId,
},
})

console.log(`Tag ${courseId} (ID: ${tagId}) added to contact successfully`)
return response
} catch (error) {
console.error('Error adding course tag in ActiveCampaign:', {
error: error.message,
userEmail: email,
tagName,
stack: error.stack,
})
throw error
}
}
10 changes: 10 additions & 0 deletions functions/active_campaign/course_tags.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
{
"Solidity_And_Smart_Contracts": "7",
"JS_DAO": "11",
"Solana_And_Web3": "12",
"Solana_NFTs": "13",
"Solana_Pay_Store": "14",
"Rust_State_Machine": "16",
"NFT_Game": "20",
"NFT_Collection": "25"
}
9 changes: 9 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ const {
createActiveCampaignUser,
fetchCustomFieldMeta,
updateUserLessonProgress,
addCourseTagToUser,
} = require('./active_campaign/active_campaign.js')

exports.sendEmail = functions.https.onRequest(async (req, resp) => {
Expand Down Expand Up @@ -58,6 +59,14 @@ exports.onCohortSignup = functions.firestore

for (let cohortSnapshot of userNewCohorts) {
const params = await emailParams(cohortSnapshot)

// Add course tag to Active Campaign
try {
await addCourseTagToUser(user.email, params.cohort.course_id)
} catch (error) {
console.error('Failed to add course tag:', error)
}

//todo essas funções deveriam ser enfileiradas num pubsub para evitar falhas
const emailRawData = {
incoming_topic: 'cohort_signup',
Expand Down

0 comments on commit 9956f5f

Please sign in to comment.