Skip to content

Commit

Permalink
progress: auth and user creation with basic info
Browse files Browse the repository at this point in the history
  • Loading branch information
nomadbitcoin committed Nov 13, 2024
1 parent 32f5e98 commit c5af3d2
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 0 deletions.
69 changes: 69 additions & 0 deletions functions/active_campaign/active_campaign.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
require('dotenv').config('../.env')

const API_URL = `https://${process.env.ACTIVE_CAMPAIGN_API_URL}.api-us1.com/api/3`
const API_TOKEN = process.env.ACTIVE_CAMPAIGN_API_KEY

const headers = {
Accept: 'application/json',
'Content-Type': 'application/json',
'Api-Token': API_TOKEN,
}

async function makeRequest(endpoint, method = 'GET', body = null) {
const options = {
method,
headers,
...(body && { body: JSON.stringify(body) }),
}

const response = await fetch(`${API_URL}${endpoint}`, options)
if (!response.ok) throw new Error(`HTTP error! status: ${response.status}`)
return response.json()
}

exports.createACUser = async function (user) {
// return fetchCustomFieldMeta()
console.log('Saving user on ActiveCampaign...', user)
try {
// Create or update contact
const { contact } = await makeRequest('/contacts', 'POST', {
contact: {
email: user.email,
firstName: user.name || '',
},
})

console.log('User added to ActiveCampaign successfully')
return contact
} catch (error) {
console.error('Error creating user in ActiveCampaign:', error)
throw error
}
}

exports.fetchUSer = async function () {
let userId = 2792
console.log('Fetching user from ActiveCampaign...', userId)
try {
const contact = await makeRequest(`/contacts/${userId}`)
console.log('User retrieved from ActiveCampaign successfully')
return contact
} catch (error) {
console.error('Error fetching user from ActiveCampaign:', error)
throw error
}
}

fetchCustomFieldMeta = async function () {
console.log('Fetching custom field metadata from ActiveCampaign...')
try {
const endpoint = '/fields'
const params = '?limit=100'
const response = await makeRequest(endpoint + params)
console.log('Custom field metadata retrieved successfully')
return response
} catch (error) {
console.error('Error fetching custom field metadata:', error)
throw error
}
}
27 changes: 27 additions & 0 deletions functions/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,17 @@ 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')

exports.sendEmail = functions.https.onRequest(async (req, resp) => {
const subject = req.query.subject || '🏕️ Seu primeiro Smart Contract na Ethereum'
resp.send(await sendEmail(req.query.template, subject, req.query.to))
})

exports.returnTrue = functions.https.onRequest(async (req, resp) => {
return resp.send('true')
})

async function docData(collection, doc_id) {
return { ...(await db.collection(collection).doc(doc_id).get()).data(), id: doc_id }
}
Expand Down Expand Up @@ -410,3 +415,25 @@ exports.scheduledFetchAndStoreIssues = functions.pubsub
return false
}
})

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',
}

try {
const result = await createACUser(fakeUser)
resp.json({ success: true, result })
} catch (error) {
console.error('Error creating test user:', error)
resp.status(500).json({ success: false, error: error.message })
}
})

0 comments on commit c5af3d2

Please sign in to comment.