mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-09-22 06:00:56 +02:00
39 lines
920 B
JavaScript
39 lines
920 B
JavaScript
![]() |
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);
|