|
| 1 | +const path = require('path'); |
| 2 | +require('module-alias')({ base: path.resolve(__dirname, '..', 'api') }); |
| 3 | +const { askQuestion, silentExit } = require('./helpers'); |
| 4 | +const { isEnabled } = require('~/server/utils/handleText'); |
| 5 | +const User = require('~/models/User'); |
| 6 | +const connect = require('./connect'); |
| 7 | +const Balance = require('~/models/Balance'); |
| 8 | + |
| 9 | +(async () => { |
| 10 | + await connect(); |
| 11 | + |
| 12 | + /** |
| 13 | + * Show the welcome / help menu |
| 14 | + */ |
| 15 | + console.purple('--------------------------'); |
| 16 | + console.purple('Set balance to a user account!'); |
| 17 | + console.purple('--------------------------'); |
| 18 | + /** |
| 19 | + * Set up the variables we need and get the arguments if they were passed in |
| 20 | + */ |
| 21 | + let email = ''; |
| 22 | + let amount = ''; |
| 23 | + // If we have the right number of arguments, lets use them |
| 24 | + if (process.argv.length >= 3) { |
| 25 | + email = process.argv[2]; |
| 26 | + amount = process.argv[3]; |
| 27 | + } else { |
| 28 | + console.orange('Usage: npm run set-balance <email> <amount>'); |
| 29 | + console.orange('Note: if you do not pass in the arguments, you will be prompted for them.'); |
| 30 | + console.purple('--------------------------'); |
| 31 | + // console.purple(`[DEBUG] Args Length: ${process.argv.length}`); |
| 32 | + } |
| 33 | + |
| 34 | + if (!process.env.CHECK_BALANCE) { |
| 35 | + console.red( |
| 36 | + 'Error: CHECK_BALANCE environment variable is not set! Configure it to use it: `CHECK_BALANCE=true`', |
| 37 | + ); |
| 38 | + silentExit(1); |
| 39 | + } |
| 40 | + if (isEnabled(process.env.CHECK_BALANCE) === false) { |
| 41 | + console.red( |
| 42 | + 'Error: CHECK_BALANCE environment variable is set to `false`! Please configure: `CHECK_BALANCE=true`', |
| 43 | + ); |
| 44 | + silentExit(1); |
| 45 | + } |
| 46 | + |
| 47 | + /** |
| 48 | + * If we don't have the right number of arguments, lets prompt the user for them |
| 49 | + */ |
| 50 | + if (!email) { |
| 51 | + email = await askQuestion('Email:'); |
| 52 | + } |
| 53 | + // Validate the email |
| 54 | + if (!email.includes('@')) { |
| 55 | + console.red('Error: Invalid email address!'); |
| 56 | + silentExit(1); |
| 57 | + } |
| 58 | + |
| 59 | + if (!amount) { |
| 60 | + amount = await askQuestion('amount:'); |
| 61 | + } |
| 62 | + // Validate the amount |
| 63 | + if (!amount) { |
| 64 | + console.red('Error: Please specify an amount!'); |
| 65 | + silentExit(1); |
| 66 | + } |
| 67 | + |
| 68 | + // Validate the user |
| 69 | + const user = await User.findOne({ email }).lean(); |
| 70 | + if (!user) { |
| 71 | + console.red('Error: No user with that email was found!'); |
| 72 | + silentExit(1); |
| 73 | + } else { |
| 74 | + console.purple(`Found user: ${user.email}`); |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Now that we have all the variables we need, lets set the balance |
| 79 | + */ |
| 80 | + let result; |
| 81 | + try { |
| 82 | + result = await Balance.findOneAndUpdate( |
| 83 | + { user: user._id }, |
| 84 | + { tokenCredits: amount }, |
| 85 | + { upsert: true, new: true }, |
| 86 | + ).lean(); |
| 87 | + } catch (error) { |
| 88 | + console.red('Error: ' + error.message); |
| 89 | + console.error(error); |
| 90 | + silentExit(1); |
| 91 | + } |
| 92 | + |
| 93 | + // Check the result |
| 94 | + if (!result?.tokenCredits) { |
| 95 | + console.red('Error: Something went wrong while updating the balance!'); |
| 96 | + console.error(result); |
| 97 | + silentExit(1); |
| 98 | + } |
| 99 | + |
| 100 | + // Done! |
| 101 | + console.green('Balance set successfully!'); |
| 102 | + console.purple(`New Balance: ${result.tokenCredits}`); |
| 103 | + silentExit(0); |
| 104 | +})(); |
| 105 | + |
| 106 | +process.on('uncaughtException', (err) => { |
| 107 | + if (!err.message.includes('fetch failed')) { |
| 108 | + console.error('There was an uncaught error:'); |
| 109 | + console.error(err); |
| 110 | + } |
| 111 | + |
| 112 | + if (err.message.includes('fetch failed')) { |
| 113 | + return; |
| 114 | + } else { |
| 115 | + process.exit(1); |
| 116 | + } |
| 117 | +}); |
0 commit comments