Created
February 2, 2024 05:06
-
-
Save ranfysvalle02/a51342278d3b10671c25d6c12bbf6909 to your computer and use it in GitHub Desktop.
MongoDB Atlas App Services Auth + OSO Cloud
This file contains hidden or 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
exports = async function(arg){ | |
const axios = require('axios').default; | |
const OSO_authToken = String(context.values.get("Value-OSO_API")); // Replace with your actual authorization token | |
const OSO_userId = context.user.data.email; | |
let checkOSO = async function(){ | |
const apiUrl = 'https://cloud.osohq.com/api/list'; | |
const requestData = { | |
actor_type: "User", | |
actor_id: OSO_userId, | |
action: "view", | |
resource_type: "Account", | |
context_facts: [], | |
}; | |
try { | |
const response = await axios.post(apiUrl, requestData, { | |
headers: { | |
Authorization: `Bearer ${OSO_authToken}`, | |
Accept: 'application/json', | |
'Content-Type': 'application/json', | |
}, | |
}); | |
console.log('Response:', response.data); | |
return (response.data.results); | |
} catch (error) { | |
console.error('Error:', error); | |
return error; | |
} | |
} | |
let myAccounts = await checkOSO(); | |
var serviceName = "mongodb-atlas"; | |
var dbName = "eventlab-dev"; | |
var collName = "events"; | |
var collection = context.services.get(serviceName).db(dbName).collection(collName); | |
return await collection.aggregate([ | |
{"$match":{ | |
"account_id":{"$in":myAccounts} | |
}}, | |
{ | |
"$group": { | |
"_id": "$account_name", | |
"count": { "$sum": 1 } // Count the number of documents in each group | |
} | |
} | |
]) | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
OSO Config: