-
Notifications
You must be signed in to change notification settings - Fork 24
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
progress: auth and user creation with basic info
- Loading branch information
1 parent
32f5e98
commit c5af3d2
Showing
2 changed files
with
96 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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: '[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 }) | ||
} | ||
}) |