|
const connectDb = require('@librechat/backend/lib/db/connectDb'); |
|
const { askQuestion, silentExit } = require('./helpers'); |
|
const User = require('@librechat/backend/models/User'); |
|
const Transaction = require('@librechat/backend/models/Transaction'); |
|
|
|
(async () => { |
|
|
|
|
|
|
|
|
|
|
|
let timeout = setTimeout(() => { |
|
console.orange( |
|
'This is taking a while... You may need to check your connection if this fails.', |
|
); |
|
timeout = setTimeout(() => { |
|
console.orange('Still going... Might as well assume the connection failed...'); |
|
timeout = setTimeout(() => { |
|
console.orange('Error incoming in 3... 2... 1...'); |
|
}, 13000); |
|
}, 10000); |
|
}, 5000); |
|
|
|
try { |
|
console.orange('Warming up the engines...'); |
|
await connectDb(); |
|
clearTimeout(timeout); |
|
} catch (e) { |
|
console.error(e); |
|
silentExit(1); |
|
} |
|
|
|
|
|
|
|
|
|
console.purple('--------------------------'); |
|
console.purple('Add balance to a user account!'); |
|
console.purple('--------------------------'); |
|
|
|
|
|
|
|
let email = ''; |
|
let amount = ''; |
|
|
|
if (process.argv.length >= 3) { |
|
email = process.argv[2]; |
|
amount = process.argv[3]; |
|
} else { |
|
console.orange('Usage: npm run add-balance <email> <amount>'); |
|
console.orange('Note: if you do not pass in the arguments, you will be prompted for them.'); |
|
console.purple('--------------------------'); |
|
|
|
} |
|
|
|
|
|
|
|
|
|
if (!email) { |
|
email = await askQuestion('Email:'); |
|
} |
|
|
|
if (!email.includes('@')) { |
|
console.red('Error: Invalid email address!'); |
|
silentExit(1); |
|
} |
|
|
|
if (!amount) { |
|
amount = await askQuestion('amount: (default is 1000 tokens if empty or 0)'); |
|
} |
|
|
|
if (!amount) { |
|
amount = 1000; |
|
} |
|
|
|
|
|
const user = await User.findOne({ email }).lean(); |
|
if (!user) { |
|
console.red('Error: No user with that email was found!'); |
|
silentExit(1); |
|
} else { |
|
console.purple(`Found user: ${user.email}`); |
|
} |
|
|
|
|
|
|
|
|
|
let result; |
|
try { |
|
result = await Transaction.create({ |
|
user: user._id, |
|
tokenType: 'credits', |
|
context: 'admin', |
|
rawAmount: +amount, |
|
}); |
|
} catch (error) { |
|
console.red('Error: ' + error.message); |
|
console.error(error); |
|
silentExit(1); |
|
} |
|
|
|
|
|
if (!result.tokenCredits) { |
|
console.red('Error: Something went wrong while updating the balance!'); |
|
console.error(result); |
|
silentExit(1); |
|
} |
|
|
|
|
|
console.green('Transaction created successfully!'); |
|
console.purple(`Amount: ${amount} |
|
New Balance: ${result.tokenCredits}`); |
|
silentExit(0); |
|
})(); |
|
|
|
process.on('uncaughtException', (err) => { |
|
if (!err.message.includes('fetch failed')) { |
|
console.error('There was an uncaught error:'); |
|
console.error(err); |
|
} |
|
|
|
if (err.message.includes('fetch failed')) { |
|
return; |
|
} else { |
|
process.exit(1); |
|
} |
|
}); |
|
|