Created
May 7, 2024 00:32
-
-
Save LuFC-SSD/066cfa79f6e5e2fcc2b220361eddc390 to your computer and use it in GitHub Desktop.
Script used to retrieve posts data from specific user in Retool
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
console.log("HELLO MATE"); | |
const database = db.firestore(); | |
const userId = localStorage.values.userDetails._id; | |
const dateString = DateInput_User_Posts_Report.value; | |
const dateRange = DateRange_User_Posts_Report.value; | |
const postRef = database.collection('posts'); | |
async function queryPostsByDate(date){ | |
const startTime = new Date(date + "T00:00:00Z").getTime(); | |
const endTime = new Date(date + "T23:59:59Z").getTime(); | |
try{ | |
const snapshot = await postRef | |
.where('userId', "==", userId) | |
.where('createdAtTimestamp', '>=', startTime) | |
.where('createdAtTimestamp', '<=', endTime) | |
.get(); | |
if (snapshot.empty) { | |
console.log('No matching documents.'); | |
return; | |
} | |
return snapshot.docs.map(doc => doc.data()); | |
} catch (error){ | |
console.log(`Error getting posts for this date ${date}`, error); | |
return [] | |
} | |
} | |
async function queryPostsByDateRange(dateRange) { | |
const { start, end } = dateRange; | |
const startDate = new Date(start + 'T00:00:00Z'); | |
const endDate = new Date(end + 'T23:59:59Z'); | |
try{ | |
// Query the 'posts' collection | |
const snapshot = await postRef | |
.where('userId', "==", userId) | |
.where('createdAtTimestamp', '>=', startDate) | |
.where('createdAtTimestamp', '<=', endDate) | |
.get() | |
if (snapshot.empty) { | |
console.log('No matching documents.'); | |
return; | |
} | |
return snapshot.docs.map(doc => doc.data()); | |
} catch (error){ | |
console.error('Error getting documents', error); | |
return [] | |
} | |
} | |
let results; | |
if(Tabs_User_Posts_Report.value.includes("Day") && dateString){ | |
const postsByDate = await queryPostsByDate(dateString); | |
console.log(`Posts by date: ${JSON.stringify(postsByDate, null, 2)}`) | |
results = postsByDate; | |
} else { | |
const postsByDateRange = await queryPostsByDateRange(dateRange); | |
console.log(`Posts by date range: ${JSON.stringify(postsByDateRange, null, 2)}`) | |
results = postsByDateRange; | |
} | |
return results; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment