Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

🪙 feat: automatically add start balance #4486

Merged
merged 3 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -350,6 +350,7 @@ ILLEGAL_MODEL_REQ_SCORE=5
#========================#

CHECK_BALANCE=false
# START_BALANCE=20000 # note: the number of tokens that will be credited after registration.

#========================#
# Registration and Login #
Expand Down
12 changes: 12 additions & 0 deletions api/models/userMethods.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
const bcrypt = require('bcryptjs');
const signPayload = require('~/server/services/signPayload');
const { isEnabled } = require('~/server/utils/handleText');
const Balance = require('./Balance');
const User = require('./User');

/**
Expand Down Expand Up @@ -71,6 +73,16 @@ const createUser = async (data, disableTTL = true, returnUser = false) => {
}

const user = await User.create(userData);

if (isEnabled(process.env.CHECK_BALANCE) && process.env.START_BALANCE) {
danny-avila marked this conversation as resolved.
Show resolved Hide resolved
let incrementValue = parseInt(process.env.START_BALANCE);
await Balance.findOneAndUpdate(
{ user: user._id },
{ $inc: { tokenCredits: incrementValue } },
{ upsert: true, new: true },
).lean();
}

if (returnUser) {
return user.toObject();
}
Expand Down
Loading