🪙 feat: automatically add start balance (#4486)

* automatically add  start balance

https://github.com/danny-avila/LibreChat/issues/2687

* chore: imports order in userMethods.js

* Information about START_BALANCE has been added

---------

Co-authored-by: Danny Avila <danny@librechat.ai>
This commit is contained in:
MSITE.TOP 2024-11-16 19:17:17 +04:00 committed by GitHub
parent 493e64e6db
commit d9ed161104
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 13 additions and 0 deletions

View file

@ -352,6 +352,7 @@ ILLEGAL_MODEL_REQ_SCORE=5
#========================# #========================#
CHECK_BALANCE=false CHECK_BALANCE=false
# START_BALANCE=20000 # note: the number of tokens that will be credited after registration.
#========================# #========================#
# Registration and Login # # Registration and Login #

View file

@ -1,5 +1,7 @@
const bcrypt = require('bcryptjs'); const bcrypt = require('bcryptjs');
const signPayload = require('~/server/services/signPayload'); const signPayload = require('~/server/services/signPayload');
const { isEnabled } = require('~/server/utils/handleText');
const Balance = require('./Balance');
const User = require('./User'); const User = require('./User');
/** /**
@ -71,6 +73,16 @@ const createUser = async (data, disableTTL = true, returnUser = false) => {
} }
const user = await User.create(userData); const user = await User.create(userData);
if (isEnabled(process.env.CHECK_BALANCE) && process.env.START_BALANCE) {
let incrementValue = parseInt(process.env.START_BALANCE);
await Balance.findOneAndUpdate(
{ user: user._id },
{ $inc: { tokenCredits: incrementValue } },
{ upsert: true, new: true },
).lean();
}
if (returnUser) { if (returnUser) {
return user.toObject(); return user.toObject();
} }