Skip to content

Commit

Permalink
feat: add user to active campaign with custom fields
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin committed Nov 13, 2024
1 parent c5af3d2 commit c2fc163
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 20 deletions.
16 changes: 12 additions & 4 deletions functions/active_campaign/active_campaign.js
Original file line number Diff line number Diff line change
Expand Up @@ -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',
},
],
},
})

Expand All @@ -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'
Expand Down
31 changes: 15 additions & 16 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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'
Expand Down Expand Up @@ -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)
Expand Down Expand Up @@ -416,21 +427,9 @@ exports.scheduledFetchAndStoreIssues = functions.pubsub
}
})

exports.testCreateActiveCampaignUser = functions.https.onRequest(async (req, resp) => {
const fakeUser = {
email: '[email protected]',
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)
Expand Down

0 comments on commit c2fc163

Please sign in to comment.