diff --git a/backend/src/graphql/model/TransactionLink.ts b/backend/src/graphql/model/TransactionLink.ts index c518f0f6c..1670e9a23 100644 --- a/backend/src/graphql/model/TransactionLink.ts +++ b/backend/src/graphql/model/TransactionLink.ts @@ -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 diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts index 51790502d..5a1a39dca 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.test.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.test.ts @@ -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', () => { diff --git a/backend/src/graphql/resolver/TransactionLinkResolver.ts b/backend/src/graphql/resolver/TransactionLinkResolver.ts index 46cdf5071..657337d74 100644 --- a/backend/src/graphql/resolver/TransactionLinkResolver.ts +++ b/backend/src/graphql/resolver/TransactionLinkResolver.ts @@ -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