LibreChat / api /models /Balance.js
N.Achyuth Reddy
Upload 683 files
9705b6c
raw
history blame contribute delete
920 Bytes
const mongoose = require('mongoose');
const balanceSchema = require('./schema/balance');
const { getMultiplier } = require('./tx');
balanceSchema.statics.check = async function ({ user, model, valueKey, tokenType, amount, debug }) {
const multiplier = getMultiplier({ valueKey, tokenType, model });
const tokenCost = amount * multiplier;
const { tokenCredits: balance } = (await this.findOne({ user }, 'tokenCredits').lean()) ?? {};
if (debug) {
console.log('balance check', {
user,
model,
valueKey,
tokenType,
amount,
debug,
balance,
multiplier,
});
}
if (!balance) {
return {
canSpend: false,
balance: 0,
tokenCost,
};
}
if (debug) {
console.log('balance check', { tokenCost });
}
return { canSpend: balance >= tokenCost, balance, tokenCost };
};
module.exports = mongoose.model('Balance', balanceSchema);