From c2fc1632305afd8006c77b428899770aec53970a Mon Sep 17 00:00:00 2001 From: Yan Luiz Date: Wed, 13 Nov 2024 07:16:22 -0300 Subject: [PATCH] feat: add user to active campaign with custom fields --- functions/active_campaign/active_campaign.js | 16 +++++++--- functions/index.js | 31 ++++++++++---------- 2 files changed, 27 insertions(+), 20 deletions(-) diff --git a/functions/active_campaign/active_campaign.js b/functions/active_campaign/active_campaign.js index a178c6b8..e12a64fe 100644 --- a/functions/active_campaign/active_campaign.js +++ b/functions/active_campaign/active_campaign.js @@ -21,15 +21,23 @@ async function makeRequest(endpoint, method = 'GET', body = null) { return response.json() } -exports.createACUser = async function (user) { - // return fetchCustomFieldMeta() - console.log('Saving user on ActiveCampaign...', user) +exports.createActiveCampaignUser = async function (user) { try { // Create or update contact const { contact } = await makeRequest('/contacts', 'POST', { contact: { email: user.email, firstName: user.name || '', + fieldValues: [ + { + field: '2', + value: user.photoUrl, + }, + { + field: '13', + value: 'BUILD_PLATFORM_API', + }, + ], }, }) @@ -54,7 +62,7 @@ exports.fetchUSer = async function () { } } -fetchCustomFieldMeta = async function () { +exports.fetchCustomFieldMeta = async function () { console.log('Fetching custom field metadata from ActiveCampaign...') try { const endpoint = '/fields' diff --git a/functions/index.js b/functions/index.js index d2399063..58f57137 100644 --- a/functions/index.js +++ b/functions/index.js @@ -13,7 +13,10 @@ const { log_study_group } = require('./lib/log_study_group') const { db, firebase } = require('./lib/initDb') const { usersByStudyGroup, storeUsersPerStudyGroup } = require('./study_group_analytics') const { fetchAndStoreIssues } = require('./fetchKanban') -const { createACUser } = require('./active_campaign/active_campaign.js') +const { + createActiveCampaignUser, + fetchCustomFieldMeta, +} = require('./active_campaign/active_campaign.js') exports.sendEmail = functions.https.onRequest(async (req, resp) => { const subject = req.query.subject || '🏕️ Seu primeiro Smart Contract na Ethereum' @@ -323,11 +326,19 @@ exports.router = functions.pubsub.topic('router-pubsub').onPublish(async (messag exports.sendUserToMailchimpOnUserCreation = functions.pubsub .topic('user_created') - .onPublish((message) => { + .onPublish(async (message) => { const data = JSON.parse(Buffer.from(message.data, 'base64')) + console.log('sending user to AC on user creation', data.user) return createUser(data.user) }) +exports.sendUserToActiveCampaignOnUserCreation = functions.pubsub + .topic('user_created') + .onPublish(async (message) => { + const data = JSON.parse(Buffer.from(message.data, 'base64')) + return createActiveCampaignUser(data.user) + }) + exports.onCohortSignupMail = functions.pubsub.topic('cohort_signup').onPublish((message) => { const data = JSON.parse(Buffer.from(message.data, 'base64')) console.log(data) @@ -416,21 +427,9 @@ exports.scheduledFetchAndStoreIssues = functions.pubsub } }) -exports.testCreateActiveCampaignUser = functions.https.onRequest(async (req, resp) => { - const fakeUser = { - email: 'test@example.com', - name: 'Test User', - discord: { - username: 'testuser#1234', - id: '123456789', - }, - wallet: '0x1234567890abcdef', - createdAt: new Date(), - uid: 'test-uid-123', - } - +exports.fetchACCustomFieldsMeta = functions.https.onRequest(async (req, resp) => { try { - const result = await createACUser(fakeUser) + const result = await fetchCustomFieldMeta() resp.json({ success: true, result }) } catch (error) { console.error('Error creating test user:', error)