| const mongoose = require('mongoose'); |
| const { isEnabled } = require('../server/utils/handleText'); |
| const transactionSchema = require('./schema/transaction'); |
| const { getMultiplier } = require('./tx'); |
| const { logger } = require('~/config'); |
| const Balance = require('./Balance'); |
| const cancelRate = 1.15; |
|
|
| |
| transactionSchema.methods.calculateTokenValue = function () { |
| if (!this.valueKey || !this.tokenType) { |
| this.tokenValue = this.rawAmount; |
| } |
| const { valueKey, tokenType, model, endpointTokenConfig } = this; |
| const multiplier = Math.abs(getMultiplier({ valueKey, tokenType, model, endpointTokenConfig })); |
| this.rate = multiplier; |
| this.tokenValue = this.rawAmount * multiplier; |
| if (this.context && this.tokenType === 'completion' && this.context === 'incomplete') { |
| this.tokenValue = Math.ceil(this.tokenValue * cancelRate); |
| this.rate *= cancelRate; |
| } |
| }; |
|
|
| |
| transactionSchema.statics.create = async function (transactionData) { |
| const Transaction = this; |
|
|
| const transaction = new Transaction(transactionData); |
| transaction.endpointTokenConfig = transactionData.endpointTokenConfig; |
| transaction.calculateTokenValue(); |
|
|
| |
| await transaction.save(); |
|
|
| if (!isEnabled(process.env.CHECK_BALANCE)) { |
| return; |
| } |
|
|
| let balance = await Balance.findOne({ user: transaction.user }).lean(); |
| let incrementValue = transaction.tokenValue; |
|
|
| if (balance && balance?.tokenCredits + incrementValue < 0) { |
| incrementValue = -balance.tokenCredits; |
| } |
|
|
| balance = await Balance.findOneAndUpdate( |
| { user: transaction.user }, |
| { $inc: { tokenCredits: incrementValue } }, |
| { upsert: true, new: true }, |
| ).lean(); |
|
|
| return { |
| rate: transaction.rate, |
| user: transaction.user.toString(), |
| balance: balance.tokenCredits, |
| [transaction.tokenType]: incrementValue, |
| }; |
| }; |
|
|
| const Transaction = mongoose.model('Transaction', transactionSchema); |
|
|
| |
| |
| |
| |
| |
| |
| |
| |
| async function getTransactions(filter) { |
| try { |
| return await Transaction.find(filter).lean(); |
| } catch (error) { |
| logger.error('Error querying transactions:', error); |
| throw error; |
| } |
| } |
|
|
| module.exports = { Transaction, getTransactions }; |
|
|