From c5af3d2a56c025ebf189e096c511d2ac06c1624c Mon Sep 17 00:00:00 2001 From: Yan Luiz Date: Tue, 12 Nov 2024 22:26:03 -0300 Subject: [PATCH] progress: auth and user creation with basic info --- functions/active_campaign/active_campaign.js | 69 ++++++++++++++++++++ functions/index.js | 27 ++++++++ 2 files changed, 96 insertions(+) create mode 100644 functions/active_campaign/active_campaign.js diff --git a/functions/active_campaign/active_campaign.js b/functions/active_campaign/active_campaign.js new file mode 100644 index 00000000..a178c6b8 --- /dev/null +++ b/functions/active_campaign/active_campaign.js @@ -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 + } +} diff --git a/functions/index.js b/functions/index.js index 2109a6dc..d2399063 100644 --- a/functions/index.js +++ b/functions/index.js @@ -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 } } @@ -410,3 +415,25 @@ exports.scheduledFetchAndStoreIssues = functions.pubsub return false } }) + +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', + } + + 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 }) + } +})