mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge branch 'master' into query-transaction-link
This commit is contained in:
commit
92f02f9f39
@ -20,6 +20,7 @@ export enum RIGHTS {
|
|||||||
HAS_ELOPAGE = 'HAS_ELOPAGE',
|
HAS_ELOPAGE = 'HAS_ELOPAGE',
|
||||||
CREATE_TRANSACTION_LINK = 'CREATE_TRANSACTION_LINK',
|
CREATE_TRANSACTION_LINK = 'CREATE_TRANSACTION_LINK',
|
||||||
QUERY_TRANSACTION_LINK = 'QUERY_TRANSACTION_LINK',
|
QUERY_TRANSACTION_LINK = 'QUERY_TRANSACTION_LINK',
|
||||||
|
|
||||||
// Admin
|
// Admin
|
||||||
SEARCH_USERS = 'SEARCH_USERS',
|
SEARCH_USERS = 'SEARCH_USERS',
|
||||||
CREATE_PENDING_CREATION = 'CREATE_PENDING_CREATION',
|
CREATE_PENDING_CREATION = 'CREATE_PENDING_CREATION',
|
||||||
|
|||||||
@ -5,17 +5,19 @@ import { User } from './User'
|
|||||||
|
|
||||||
@ObjectType()
|
@ObjectType()
|
||||||
export class TransactionLink {
|
export class TransactionLink {
|
||||||
constructor(transactionLink: dbTransactionLink, user: User) {
|
constructor(transactionLink: dbTransactionLink, user: User, redeemedBy: User | null = null) {
|
||||||
this.id = transactionLink.id
|
this.id = transactionLink.id
|
||||||
this.user = user
|
this.user = user
|
||||||
this.amount = transactionLink.amount
|
this.amount = transactionLink.amount
|
||||||
|
this.holdAvailableAmount = transactionLink.holdAvailableAmount
|
||||||
this.memo = transactionLink.memo
|
this.memo = transactionLink.memo
|
||||||
this.code = transactionLink.code
|
this.code = transactionLink.code
|
||||||
this.createdAt = transactionLink.createdAt
|
this.createdAt = transactionLink.createdAt
|
||||||
this.validUntil = transactionLink.validUntil
|
this.validUntil = transactionLink.validUntil
|
||||||
this.showEmail = transactionLink.showEmail
|
this.showEmail = transactionLink.showEmail
|
||||||
this.redeemedAt = null
|
this.deletedAt = transactionLink.deletedAt
|
||||||
this.redeemedBy = null
|
this.redeemedAt = transactionLink.redeemedAt
|
||||||
|
this.redeemedBy = redeemedBy
|
||||||
}
|
}
|
||||||
|
|
||||||
@Field(() => Number)
|
@Field(() => Number)
|
||||||
|
|||||||
@ -3,8 +3,8 @@ import { transactionLinkCode } from './TransactionLinkResolver'
|
|||||||
describe('transactionLinkCode', () => {
|
describe('transactionLinkCode', () => {
|
||||||
const date = new Date()
|
const date = new Date()
|
||||||
|
|
||||||
it('returns a string of length 96', () => {
|
it('returns a string of length 24', () => {
|
||||||
expect(transactionLinkCode(date)).toHaveLength(96)
|
expect(transactionLinkCode(date)).toHaveLength(24)
|
||||||
})
|
})
|
||||||
|
|
||||||
it('returns a string that ends with the hex value of date', () => {
|
it('returns a string that ends with the hex value of date', () => {
|
||||||
|
|||||||
@ -11,20 +11,23 @@ import { calculateBalance } from '@/util/validate'
|
|||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
import { randomBytes } from 'crypto'
|
import { randomBytes } from 'crypto'
|
||||||
import { User } from '@model/User'
|
import { User } from '@model/User'
|
||||||
|
import { calculateDecay } from '@/util/decay'
|
||||||
|
^
|
||||||
// TODO: do not export, test it inside the resolver
|
// TODO: do not export, test it inside the resolver
|
||||||
export const transactionLinkCode = (date: Date): string => {
|
export const transactionLinkCode = (date: Date): string => {
|
||||||
const time = date.getTime().toString(16)
|
const time = date.getTime().toString(16)
|
||||||
return (
|
return (
|
||||||
randomBytes(48)
|
randomBytes(12)
|
||||||
.toString('hex')
|
.toString('hex')
|
||||||
.substring(0, 96 - time.length) + time
|
.substring(0, 24 - time.length) + time
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CODE_VALID_DAYS_DURATION = 14
|
||||||
|
|
||||||
const transactionLinkExpireDate = (date: Date): Date => {
|
const transactionLinkExpireDate = (date: Date): Date => {
|
||||||
// valid for 14 days
|
const validUntil = new Date(date)
|
||||||
return new Date(date.setDate(date.getDate() + 14))
|
return new Date(validUntil.setDate(date.getDate() + CODE_VALID_DAYS_DURATION))
|
||||||
}
|
}
|
||||||
|
|
||||||
@Resolver()
|
@Resolver()
|
||||||
@ -38,26 +41,28 @@ export class TransactionLinkResolver {
|
|||||||
const userRepository = getCustomRepository(UserRepository)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
const user = await userRepository.findByPubkeyHex(context.pubKey)
|
const user = await userRepository.findByPubkeyHex(context.pubKey)
|
||||||
|
|
||||||
// validate amount
|
|
||||||
// TODO taken from transaction resolver, duplicate code
|
|
||||||
const createdDate = new Date()
|
const createdDate = new Date()
|
||||||
const sendBalance = await calculateBalance(user.id, amount.mul(-1), createdDate)
|
const validUntil = transactionLinkExpireDate(createdDate)
|
||||||
|
|
||||||
|
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
||||||
|
|
||||||
|
// validate amount
|
||||||
|
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
||||||
if (!sendBalance) {
|
if (!sendBalance) {
|
||||||
throw new Error("user hasn't enough GDD or amount is < 0")
|
throw new Error("user hasn't enough GDD or amount is < 0")
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO!!!! Test balance for pending transaction links
|
|
||||||
|
|
||||||
const transactionLink = dbTransactionLink.create()
|
const transactionLink = dbTransactionLink.create()
|
||||||
transactionLink.userId = user.id
|
transactionLink.userId = user.id
|
||||||
transactionLink.amount = amount
|
transactionLink.amount = amount
|
||||||
transactionLink.memo = memo
|
transactionLink.memo = memo
|
||||||
|
transactionLink.holdAvailableAmount = holdAvailableAmount
|
||||||
transactionLink.code = transactionLinkCode(createdDate)
|
transactionLink.code = transactionLinkCode(createdDate)
|
||||||
transactionLink.createdAt = createdDate
|
transactionLink.createdAt = createdDate
|
||||||
transactionLink.validUntil = transactionLinkExpireDate(createdDate)
|
transactionLink.validUntil = validUntil
|
||||||
transactionLink.showEmail = showEmail
|
transactionLink.showEmail = showEmail
|
||||||
await dbTransactionLink.save(transactionLink).catch((error) => {
|
await dbTransactionLink.save(transactionLink).catch(() => {
|
||||||
throw error
|
throw new Error('Unable to save transaction link')
|
||||||
})
|
})
|
||||||
|
|
||||||
return new TransactionLink(transactionLink, new User(user))
|
return new TransactionLink(transactionLink, new User(user))
|
||||||
|
|||||||
@ -19,6 +19,7 @@ import { Order } from '@enum/Order'
|
|||||||
|
|
||||||
import { UserRepository } from '@repository/User'
|
import { UserRepository } from '@repository/User'
|
||||||
import { TransactionRepository } from '@repository/Transaction'
|
import { TransactionRepository } from '@repository/Transaction'
|
||||||
|
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
||||||
|
|
||||||
import { User as dbUser } from '@entity/User'
|
import { User as dbUser } from '@entity/User'
|
||||||
import { Transaction as dbTransaction } from '@entity/Transaction'
|
import { Transaction as dbTransaction } from '@entity/Transaction'
|
||||||
@ -127,9 +128,14 @@ export class TransactionResolver {
|
|||||||
transactions.push(new Transaction(userTransaction, self, linkedUser))
|
transactions.push(new Transaction(userTransaction, self, linkedUser))
|
||||||
})
|
})
|
||||||
|
|
||||||
|
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
||||||
|
const toHoldAvailable = await transactionLinkRepository.sumAmountToHoldAvailable(user.id, now)
|
||||||
|
|
||||||
// Construct Result
|
// Construct Result
|
||||||
return new TransactionList(
|
return new TransactionList(
|
||||||
calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, now).balance,
|
calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, now).balance.minus(
|
||||||
|
toHoldAvailable.toString(),
|
||||||
|
),
|
||||||
transactions,
|
transactions,
|
||||||
userTransactionsCount,
|
userTransactionsCount,
|
||||||
balanceGDT,
|
balanceGDT,
|
||||||
|
|||||||
16
backend/src/typeorm/repository/TransactionLink.ts
Normal file
16
backend/src/typeorm/repository/TransactionLink.ts
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
import { Repository, EntityRepository } from '@dbTools/typeorm'
|
||||||
|
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||||
|
import Decimal from 'decimal.js-light'
|
||||||
|
|
||||||
|
@EntityRepository(dbTransactionLink)
|
||||||
|
export class TransactionLinkRepository extends Repository<dbTransactionLink> {
|
||||||
|
async sumAmountToHoldAvailable(userId: number, date: Date): Promise<Decimal> {
|
||||||
|
const { sum } = await this.createQueryBuilder('transactionLinks')
|
||||||
|
.select('SUM(transactionLinks.holdAvailableAmount)', 'sum')
|
||||||
|
.where('transactionLinks.userId = :userId', { userId })
|
||||||
|
.andWhere('transactionLinks.redeemedAt is NULL')
|
||||||
|
.andWhere('transactionLinks.validUntil > :date', { date })
|
||||||
|
.getRawOne()
|
||||||
|
return sum ? new Decimal(sum) : new Decimal(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
@ -2,6 +2,8 @@ import { calculateDecay } from './decay'
|
|||||||
import Decimal from 'decimal.js-light'
|
import Decimal from 'decimal.js-light'
|
||||||
import { Transaction } from '@entity/Transaction'
|
import { Transaction } from '@entity/Transaction'
|
||||||
import { Decay } from '@model/Decay'
|
import { Decay } from '@model/Decay'
|
||||||
|
import { getCustomRepository } from '@dbTools/typeorm'
|
||||||
|
import { TransactionLinkRepository } from '@repository/TransactionLink'
|
||||||
|
|
||||||
function isStringBoolean(value: string): boolean {
|
function isStringBoolean(value: string): boolean {
|
||||||
const lowerValue = value.toLowerCase()
|
const lowerValue = value.toLowerCase()
|
||||||
@ -24,9 +26,13 @@ async function calculateBalance(
|
|||||||
if (!lastTransaction) return null
|
if (!lastTransaction) return null
|
||||||
|
|
||||||
const decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, time)
|
const decay = calculateDecay(lastTransaction.balance, lastTransaction.balanceDate, time)
|
||||||
|
|
||||||
// TODO why we have to use toString() here?
|
// TODO why we have to use toString() here?
|
||||||
const balance = decay.balance.add(amount.toString())
|
const balance = decay.balance.add(amount.toString())
|
||||||
if (balance.lessThan(0)) {
|
const transactionLinkRepository = getCustomRepository(TransactionLinkRepository)
|
||||||
|
const toHoldAvailable = await transactionLinkRepository.sumAmountToHoldAvailable(userId, time)
|
||||||
|
|
||||||
|
if (balance.minus(toHoldAvailable.toString()).lessThan(0)) {
|
||||||
return null
|
return null
|
||||||
}
|
}
|
||||||
return { balance, lastTransactionId: lastTransaction.id, decay }
|
return { balance, lastTransactionId: lastTransaction.id, decay }
|
||||||
|
|||||||
@ -42,7 +42,7 @@ export class TransactionLink extends BaseEntity {
|
|||||||
createdAt: Date
|
createdAt: Date
|
||||||
|
|
||||||
@DeleteDateColumn()
|
@DeleteDateColumn()
|
||||||
deletedAt?: Date | null
|
deletedAt: Date | null
|
||||||
|
|
||||||
@Column({
|
@Column({
|
||||||
type: 'datetime',
|
type: 'datetime',
|
||||||
@ -61,8 +61,8 @@ export class TransactionLink extends BaseEntity {
|
|||||||
type: 'datetime',
|
type: 'datetime',
|
||||||
nullable: true,
|
nullable: true,
|
||||||
})
|
})
|
||||||
redeemedAt?: Date | null
|
redeemedAt: Date | null
|
||||||
|
|
||||||
@Column({ type: 'int', unsigned: true, nullable: true })
|
@Column({ type: 'int', unsigned: true, nullable: true })
|
||||||
redeemedBy?: number | null
|
redeemedBy: number | null
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user