|
| 1 | +const fs = require('fs'); |
| 2 | +const path = require('path'); |
| 3 | +const fb = require('firebase-admin'); |
| 4 | +const downloadUsers = require('./utils/downloadUsers'); |
| 5 | + |
| 6 | + |
| 7 | +// --------------------- |
| 8 | +const EMAIL = 'foobar@email.com' |
| 9 | +const PG = '7-31-testing-tankards'; |
| 10 | +const POINTS = 16; |
| 11 | +// --------------------- |
| 12 | + |
| 13 | + |
| 14 | + |
| 15 | + |
| 16 | + |
| 17 | +const userDb = fb.firestore().collection('users'); |
| 18 | +async function updatePoints() { |
| 19 | + await downloadUsers(); |
| 20 | + |
| 21 | + console.log('\n\n\n\n'); |
| 22 | + |
| 23 | + const file = path.join(__dirname, `../private/tmp-users.json`); |
| 24 | + const accounts = JSON.parse(fs.readFileSync(file)).users; |
| 25 | + |
| 26 | + const userId = accounts.find(a => a.email.toLowerCase() === EMAIL.toLowerCase()).localId; |
| 27 | + |
| 28 | + if(!userId) { |
| 29 | + console.log('ERROR: User not found by email'); |
| 30 | + process.exit(); |
| 31 | + } |
| 32 | + |
| 33 | + const liveDoc = (await userDb.doc(userId).get()).data(); |
| 34 | + |
| 35 | + if(!liveDoc) { |
| 36 | + console.log('ERROR: User not found by id'); |
| 37 | + process.exit(); |
| 38 | + } |
| 39 | + |
| 40 | + const answers = liveDoc.answers?.[PG]; |
| 41 | + |
| 42 | + if (!answers || !answers.submitted) { |
| 43 | + console.log('ERROR: User has not submitted answers for this pg'); |
| 44 | + process.exit(); |
| 45 | + } |
| 46 | + |
| 47 | + if (!answers.total || answers.total < POINTS) { |
| 48 | + console.log('ERROR: Number of points to set is greater than total possible points'); |
| 49 | + process.exit(); |
| 50 | + } |
| 51 | + |
| 52 | + const score = Math.round(100 * POINTS / answers.total); |
| 53 | + |
| 54 | + console.log(`Setting points for ${liveDoc.first} ${liveDoc.last} to ${POINTS} (${score}%)\n\n\n\n`); |
| 55 | + |
| 56 | + await userDb.doc(userId).set({answers: {[PG]: { points: POINTS, score }}}, {merge: true}); |
| 57 | +} |
| 58 | + |
| 59 | +updatePoints().then(() => process.exit()); |
0 commit comments