Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
✨ sort by newest data for proposals list (#523)
- Loading branch information
Showing
10 changed files
with
1,475 additions
and
15 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 |
---|---|---|
|
@@ -29,6 +29,7 @@ firebase-debug.log* | |
.firebaserc | ||
.runtimeconfig.json | ||
.firebase/ | ||
serviceAccount.json | ||
|
||
# docz | ||
.docz/ | ||
|
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
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
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
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 @@ | ||
8 |
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,118 @@ | ||
/* eslint-disable no-underscore-dangle */ | ||
const admin = require('firebase-admin') | ||
|
||
// ==================================== | ||
// Configuration | ||
// ==================================== | ||
|
||
// service account credentials (need to be downloaded from firebase console) | ||
const serviceAccount = require('./serviceAccount.json') | ||
|
||
// initialize app credentials | ||
admin.initializeApp({ | ||
credential: admin.credential.cert(serviceAccount), | ||
}) | ||
|
||
// initialize firestore database | ||
const db = admin.firestore() | ||
|
||
// ==================================== | ||
// Utils | ||
// ==================================== | ||
|
||
const isTimestamp = date => !!date && date instanceof admin.firestore.Timestamp | ||
|
||
// ==================================== | ||
// Firebase update functions | ||
// ==================================== | ||
|
||
const getEvent = async (id) => { | ||
const doc = await db.collection('events').doc(id).get() | ||
return { id: doc.id, data: doc.data() } | ||
} | ||
|
||
const getAllEvents = async () => { | ||
const events = await db.collection('events').get() | ||
return events.docs.map(doc => ({ id: doc.id, data: doc.data() })) | ||
} | ||
|
||
const getProposals = async (eventId) => { | ||
const eventProposals = await db | ||
.collection('events') | ||
.doc(eventId) | ||
.collection('proposals') | ||
.get() | ||
|
||
const proposals = eventProposals.docs.map(doc => doc.data()) | ||
|
||
return { eventId, proposals } | ||
} | ||
|
||
/** | ||
* Function used to update all proposals for all events or by event id | ||
* @param function callback proposal updater | ||
* @param string eventId event id (optional) | ||
*/ | ||
const updateProposals = async (callback, eventId) => { | ||
let events = [] | ||
if (eventId) { | ||
events = [await getEvent(eventId)] | ||
} else { | ||
events = await getAllEvents() | ||
} | ||
const eventsProposalsList = await Promise.all(events.map(e => getProposals(e.id))) | ||
|
||
return Promise.all( | ||
eventsProposalsList.map(async (eventProposals) => { | ||
console.log(`Update proposals for event ${eventProposals.eventId}:`) | ||
console.log(` > ${eventProposals.proposals.length} proposals`) | ||
|
||
return Promise.all( | ||
eventProposals.proposals.map((oldProposal) => { | ||
const updatedProposal = callback(oldProposal) || {} | ||
console.log(` - update proposal ${oldProposal.title} (${oldProposal.id})`) | ||
return db | ||
.collection('events') | ||
.doc(eventProposals.eventId) | ||
.collection('proposals') | ||
.doc(oldProposal.id) | ||
.update(updatedProposal) | ||
}), | ||
) | ||
}), | ||
) | ||
} | ||
|
||
const main = async () => { | ||
await updateProposals((proposal) => { | ||
let { updateTimestamp } = proposal | ||
|
||
if (isTimestamp(updateTimestamp)) { | ||
updateTimestamp = new admin.firestore.Timestamp( | ||
updateTimestamp._seconds, | ||
updateTimestamp._nanoseconds, | ||
) | ||
} else if (updateTimestamp._seconds && updateTimestamp._nanoseconds) { | ||
updateTimestamp = new admin.firestore.Timestamp( | ||
updateTimestamp._seconds, | ||
updateTimestamp._nanoseconds, | ||
) | ||
} else if (updateTimestamp.seconds && updateTimestamp.nanoseconds) { | ||
updateTimestamp = new admin.firestore.Timestamp( | ||
updateTimestamp.seconds, | ||
updateTimestamp.nanoseconds, | ||
) | ||
} else if (!isTimestamp(updateTimestamp) && updateTimestamp instanceof Date) { | ||
updateTimestamp = admin.firestore.Timestamp.fromDate(updateTimestamp) | ||
} | ||
|
||
const createTimestamp = updateTimestamp | ||
return { | ||
updateTimestamp, | ||
createTimestamp, | ||
} | ||
}) | ||
console.log('🎉 Finished') | ||
} | ||
|
||
main() |
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,9 @@ | ||
{ | ||
"name": "conference-hall-scripts", | ||
"version": "0.1.0", | ||
"main": "index.js", | ||
"license": "MIT", | ||
"dependencies": { | ||
"firebase-admin": "^7.1.0" | ||
} | ||
} |
Oops, something went wrong.