mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
save hold available amount
This commit is contained in:
parent
0f3d2719d5
commit
6d6f58a932
@ -9,6 +9,7 @@ export class TransactionLink {
|
||||
this.id = transactionLink.id
|
||||
this.user = user
|
||||
this.amount = transactionLink.amount
|
||||
this.holdAvailableAmount = transactionLink.holdAvailableAmount
|
||||
this.memo = transactionLink.memo
|
||||
this.code = transactionLink.code
|
||||
this.createdAt = transactionLink.createdAt
|
||||
|
||||
@ -3,8 +3,8 @@ import { transactionLinkCode } from './TransactionLinkResolver'
|
||||
describe('transactionLinkCode', () => {
|
||||
const date = new Date()
|
||||
|
||||
it('returns a string of length 96', () => {
|
||||
expect(transactionLinkCode(date)).toHaveLength(96)
|
||||
it('returns a string of length 24', () => {
|
||||
expect(transactionLinkCode(date)).toHaveLength(24)
|
||||
})
|
||||
|
||||
it('returns a string that ends with the hex value of date', () => {
|
||||
|
||||
@ -11,20 +11,21 @@ import { calculateBalance } from '@/util/validate'
|
||||
import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { User } from '@model/User'
|
||||
import { calculateDecay } from '@/util/decay'
|
||||
|
||||
// TODO: do not export, test it inside the resolver
|
||||
export const transactionLinkCode = (date: Date): string => {
|
||||
const time = date.getTime().toString(16)
|
||||
return (
|
||||
randomBytes(48)
|
||||
randomBytes(12)
|
||||
.toString('hex')
|
||||
.substring(0, 96 - time.length) + time
|
||||
.substring(0, 24 - time.length) + time
|
||||
)
|
||||
}
|
||||
|
||||
const transactionLinkExpireDate = (date: Date): Date => {
|
||||
// valid for 14 days
|
||||
const validUntil = new Date(date)
|
||||
// valid for 14 days
|
||||
return new Date(validUntil.setDate(date.getDate() + 14))
|
||||
}
|
||||
|
||||
@ -42,7 +43,13 @@ export class TransactionLinkResolver {
|
||||
// validate amount
|
||||
// TODO taken from transaction resolver, duplicate code
|
||||
const createdDate = new Date()
|
||||
const sendBalance = await calculateBalance(user.id, amount.mul(-1), createdDate)
|
||||
const validUntil = transactionLinkExpireDate(createdDate)
|
||||
|
||||
const holdAvailableAmount = amount.add(
|
||||
calculateDecay(amount, createdDate, validUntil).decay.mul(-1),
|
||||
)
|
||||
|
||||
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
||||
if (!sendBalance) {
|
||||
throw new Error("user hasn't enough GDD or amount is < 0")
|
||||
}
|
||||
@ -53,9 +60,10 @@ export class TransactionLinkResolver {
|
||||
transactionLink.userId = user.id
|
||||
transactionLink.amount = amount
|
||||
transactionLink.memo = memo
|
||||
transactionLink.holdAvailableAmount = holdAvailableAmount
|
||||
transactionLink.code = transactionLinkCode(createdDate)
|
||||
transactionLink.createdAt = createdDate
|
||||
transactionLink.validUntil = transactionLinkExpireDate(createdDate)
|
||||
transactionLink.validUntil = validUntil
|
||||
transactionLink.showEmail = showEmail
|
||||
await dbTransactionLink.save(transactionLink).catch((error) => {
|
||||
throw error
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user