From 9956f5fc831f04ce94169c691f2c64815718f8e5 Mon Sep 17 00:00:00 2001 From: Yan Luiz Date: Wed, 13 Nov 2024 20:30:07 -0300 Subject: [PATCH] feat: add tag with course_id on cohort signup --- functions/active_campaign/active_campaign.js | 36 ++++++++++++++++++-- functions/active_campaign/course_tags.json | 10 ++++++ functions/index.js | 9 +++++ 3 files changed, 53 insertions(+), 2 deletions(-) create mode 100644 functions/active_campaign/course_tags.json diff --git a/functions/active_campaign/active_campaign.js b/functions/active_campaign/active_campaign.js index 1803656c..b7a146d4 100644 --- a/functions/active_campaign/active_campaign.js +++ b/functions/active_campaign/active_campaign.js @@ -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 @@ -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') @@ -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') @@ -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 + } +} diff --git a/functions/active_campaign/course_tags.json b/functions/active_campaign/course_tags.json new file mode 100644 index 00000000..d9e45f4e --- /dev/null +++ b/functions/active_campaign/course_tags.json @@ -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" +} diff --git a/functions/index.js b/functions/index.js index 249ebe0a..84527ac3 100644 --- a/functions/index.js +++ b/functions/index.js @@ -17,6 +17,7 @@ const { createActiveCampaignUser, fetchCustomFieldMeta, updateUserLessonProgress, + addCourseTagToUser, } = require('./active_campaign/active_campaign.js') exports.sendEmail = functions.https.onRequest(async (req, resp) => { @@ -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',