mirror of
https://github.com/danny-avila/LibreChat.git
synced 2025-12-17 08:50:15 +01:00
🪙 feat: Sync Balance Config on Login (#6671)
* chore: Add deprecation warnings for environment variables in checks * chore: Change deprecatedVariables to a const declaration in checks.js * fix: Add date validation in checkBalanceRecord to prevent invalid date errors * feat: Add setBalanceConfig middleware to synchronize user balance settings * chore: Reorder middleware imports in oauth.js for better readability
This commit is contained in:
parent
57faae8d96
commit
0865bc4a72
7 changed files with 145 additions and 20 deletions
|
|
@ -5,6 +5,10 @@ const { getMultiplier } = require('./tx');
|
|||
const { logger } = require('~/config');
|
||||
const Balance = require('./Balance');
|
||||
|
||||
function isInvalidDate(date) {
|
||||
return isNaN(date);
|
||||
}
|
||||
|
||||
/**
|
||||
* Simple check method that calculates token cost and returns balance info.
|
||||
* The auto-refill logic has been moved to balanceMethods.js to prevent circular dependencies.
|
||||
|
|
@ -48,13 +52,12 @@ const checkBalanceRecord = async function ({
|
|||
// Only perform auto-refill if spending would bring the balance to 0 or below
|
||||
if (balance - tokenCost <= 0 && record.autoRefillEnabled && record.refillAmount > 0) {
|
||||
const lastRefillDate = new Date(record.lastRefill);
|
||||
const nextRefillDate = addIntervalToDate(
|
||||
lastRefillDate,
|
||||
record.refillIntervalValue,
|
||||
record.refillIntervalUnit,
|
||||
);
|
||||
const now = new Date();
|
||||
if (now >= nextRefillDate) {
|
||||
if (
|
||||
isInvalidDate(lastRefillDate) ||
|
||||
now >=
|
||||
addIntervalToDate(lastRefillDate, record.refillIntervalValue, record.refillIntervalUnit)
|
||||
) {
|
||||
try {
|
||||
/** @type {{ rate: number, user: string, balance: number, transaction: import('@librechat/data-schemas').ITransaction}} */
|
||||
const result = await Transaction.createAutoRefillTransaction({
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue