Skip to content

Commit

Permalink
🪙 feat: automatically add start balance (danny-avila#4486)
Browse files Browse the repository at this point in the history
* automatically add  start balance

danny-avila#2687

* chore: imports order in userMethods.js

* Information about START_BALANCE has been added

---------

Co-authored-by: Danny Avila <[email protected]>
  • Loading branch information
MSITETOP and danny-avila authored Nov 16, 2024
1 parent ee18c32 commit 06a198a
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions .env.example
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,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) {
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

0 comments on commit 06a198a

Please sign in to comment.