mirror of
https://github.com/IT4Change/gradido.git
synced 2026-03-01 12:44:43 +00:00
55 lines
1.8 KiB
TypeScript
55 lines
1.8 KiB
TypeScript
import Decimal from 'decimal.js-light'
|
|
import { User, Transaction } from '../../entity'
|
|
import { TransactionTypeId } from '../../enum'
|
|
import { fullName } from 'shared'
|
|
import { getLastTransaction } from '../../queries'
|
|
import { calculateDecay, Decay } from 'shared'
|
|
|
|
export async function createTransaction(
|
|
amount: Decimal,
|
|
memo: string,
|
|
user: User,
|
|
linkedUser: User,
|
|
type: TransactionTypeId,
|
|
balanceDate: Date,
|
|
creationDate?: Date,
|
|
store: boolean = true,
|
|
): Promise<Transaction> {
|
|
|
|
const lastTransaction = await getLastTransaction(user.id)
|
|
// balance and decay calculation
|
|
let newBalance = new Decimal(0)
|
|
let decay: Decay | null = null
|
|
if (lastTransaction) {
|
|
decay = calculateDecay(
|
|
lastTransaction.balance,
|
|
lastTransaction.balanceDate,
|
|
balanceDate,
|
|
)
|
|
newBalance = decay.balance
|
|
}
|
|
newBalance = newBalance.add(amount.toString())
|
|
|
|
const transaction = new Transaction()
|
|
transaction.typeId = type
|
|
transaction.memo = memo
|
|
transaction.userId = user.id
|
|
transaction.userGradidoID = user.gradidoID
|
|
transaction.userName = fullName(user.firstName, user.lastName)
|
|
transaction.userCommunityUuid = user.communityUuid
|
|
transaction.linkedUserId = linkedUser.id
|
|
transaction.linkedUserGradidoID = linkedUser.gradidoID
|
|
transaction.linkedUserName = fullName(linkedUser.firstName, linkedUser.lastName)
|
|
transaction.linkedUserCommunityUuid = linkedUser.communityUuid
|
|
transaction.previous = lastTransaction ? lastTransaction.id : null
|
|
transaction.amount = amount
|
|
if (creationDate) {
|
|
transaction.creationDate = creationDate
|
|
}
|
|
transaction.balance = newBalance
|
|
transaction.balanceDate = balanceDate
|
|
transaction.decay = decay ? decay.decay : new Decimal(0)
|
|
transaction.decayStart = decay ? decay.start : null
|
|
|
|
return store ? transaction.save() : transaction
|
|
} |