mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
Merge branch 'master' into frontend-generate-link-for-send-gdd
This commit is contained in:
commit
bc4450dc8f
@ -7,4 +7,5 @@ export const INALIENABLE_RIGHTS = [
|
|||||||
RIGHTS.CREATE_USER,
|
RIGHTS.CREATE_USER,
|
||||||
RIGHTS.SEND_RESET_PASSWORD_EMAIL,
|
RIGHTS.SEND_RESET_PASSWORD_EMAIL,
|
||||||
RIGHTS.SET_PASSWORD,
|
RIGHTS.SET_PASSWORD,
|
||||||
|
RIGHTS.QUERY_TRANSACTION_LINK,
|
||||||
]
|
]
|
||||||
|
|||||||
@ -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',
|
||||||
|
|||||||
10
backend/src/graphql/arg/QueryTransactionLinkArgs.ts
Normal file
10
backend/src/graphql/arg/QueryTransactionLinkArgs.ts
Normal file
@ -0,0 +1,10 @@
|
|||||||
|
import { ArgsType, Field, Int } from 'type-graphql'
|
||||||
|
|
||||||
|
@ArgsType()
|
||||||
|
export default class QueryTransactionLinkArgs {
|
||||||
|
@Field(() => String)
|
||||||
|
code: string
|
||||||
|
|
||||||
|
@Field(() => Int, { nullable: true })
|
||||||
|
redeemUserId?: number
|
||||||
|
}
|
||||||
@ -5,7 +5,7 @@ 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
|
||||||
@ -15,8 +15,9 @@ export class TransactionLink {
|
|||||||
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)
|
||||||
|
|||||||
@ -1,11 +1,12 @@
|
|||||||
/* eslint-disable @typescript-eslint/no-explicit-any */
|
/* eslint-disable @typescript-eslint/no-explicit-any */
|
||||||
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
/* eslint-disable @typescript-eslint/explicit-module-boundary-types */
|
||||||
|
|
||||||
import { Resolver, Args, Authorized, Ctx, Mutation, Query, Arg } from 'type-graphql'
|
import { Resolver, Args, Authorized, Ctx, Mutation, Query } from 'type-graphql'
|
||||||
import { getCustomRepository, MoreThan } from '@dbTools/typeorm'
|
import { getCustomRepository } from '@dbTools/typeorm'
|
||||||
import { TransactionLink } from '@model/TransactionLink'
|
import { TransactionLink } from '@model/TransactionLink'
|
||||||
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
import { TransactionLink as dbTransactionLink } from '@entity/TransactionLink'
|
||||||
import TransactionLinkArgs from '@arg/TransactionLinkArgs'
|
import TransactionLinkArgs from '@arg/TransactionLinkArgs'
|
||||||
|
import QueryTransactionLinkArgs from '@arg/QueryTransactionLinkArgs'
|
||||||
import { UserRepository } from '@repository/User'
|
import { UserRepository } from '@repository/User'
|
||||||
import { calculateBalance } from '@/util/validate'
|
import { calculateBalance } from '@/util/validate'
|
||||||
import { RIGHTS } from '@/auth/RIGHTS'
|
import { RIGHTS } from '@/auth/RIGHTS'
|
||||||
@ -23,10 +24,11 @@ export const transactionLinkCode = (date: Date): string => {
|
|||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
const CODE_VALID_DAYS_DURATION = 14
|
||||||
|
|
||||||
const transactionLinkExpireDate = (date: Date): Date => {
|
const transactionLinkExpireDate = (date: Date): Date => {
|
||||||
const validUntil = new Date(date)
|
const validUntil = new Date(date)
|
||||||
// valid for 14 days
|
return new Date(validUntil.setDate(date.getDate() + CODE_VALID_DAYS_DURATION))
|
||||||
return new Date(validUntil.setDate(date.getDate() + 14))
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Resolver()
|
@Resolver()
|
||||||
@ -43,24 +45,10 @@ export class TransactionLinkResolver {
|
|||||||
const createdDate = new Date()
|
const createdDate = new Date()
|
||||||
const validUntil = transactionLinkExpireDate(createdDate)
|
const validUntil = transactionLinkExpireDate(createdDate)
|
||||||
|
|
||||||
const holdAvailableAmount = amount.add(
|
const holdAvailableAmount = amount.minus(calculateDecay(amount, createdDate, validUntil).decay)
|
||||||
calculateDecay(amount, createdDate, validUntil).decay.mul(-1),
|
|
||||||
)
|
|
||||||
|
|
||||||
const openTransactionLinks = await dbTransactionLink.find({
|
|
||||||
select: ['holdAvailableAmount'],
|
|
||||||
where: { userId: user.id, redeemedAt: null, validUntil: MoreThan(createdDate) },
|
|
||||||
})
|
|
||||||
|
|
||||||
const holdAvailable = openTransactionLinks.reduce(
|
|
||||||
(previousValue, currentValue) =>
|
|
||||||
previousValue.add(currentValue.holdAvailableAmount.toString()),
|
|
||||||
holdAvailableAmount,
|
|
||||||
)
|
|
||||||
|
|
||||||
// validate amount
|
// validate amount
|
||||||
// TODO taken from transaction resolver, duplicate code
|
const sendBalance = await calculateBalance(user.id, holdAvailableAmount.mul(-1), createdDate)
|
||||||
const sendBalance = await calculateBalance(user.id, holdAvailable.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")
|
||||||
}
|
}
|
||||||
@ -74,8 +62,8 @@ export class TransactionLinkResolver {
|
|||||||
transactionLink.createdAt = createdDate
|
transactionLink.createdAt = createdDate
|
||||||
transactionLink.validUntil = validUntil
|
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))
|
||||||
@ -83,11 +71,30 @@ export class TransactionLinkResolver {
|
|||||||
|
|
||||||
@Authorized([RIGHTS.QUERY_TRANSACTION_LINK])
|
@Authorized([RIGHTS.QUERY_TRANSACTION_LINK])
|
||||||
@Query(() => TransactionLink)
|
@Query(() => TransactionLink)
|
||||||
async queryTransactionLink(@Arg('code') code: string): Promise<TransactionLink> {
|
async queryTransactionLink(
|
||||||
console.log(code)
|
@Args() { code, redeemUserId }: QueryTransactionLinkArgs,
|
||||||
const transactionLink = await dbTransactionLink.findOneOrFail({ code: code })
|
): Promise<TransactionLink> {
|
||||||
|
const transactionLink = await dbTransactionLink.findOneOrFail({ code })
|
||||||
const userRepository = getCustomRepository(UserRepository)
|
const userRepository = getCustomRepository(UserRepository)
|
||||||
const user = await userRepository.findOneOrFail({ id: transactionLink.userId })
|
const user = await userRepository.findOneOrFail({ id: transactionLink.userId })
|
||||||
return new TransactionLink(transactionLink, new User(user))
|
let userRedeem = null
|
||||||
|
if (redeemUserId && !transactionLink.redeemedBy) {
|
||||||
|
const redeemedByUser = await userRepository.findOne({ id: redeemUserId })
|
||||||
|
if (!redeemedByUser) {
|
||||||
|
throw new Error('Unable to find user that redeem the link')
|
||||||
|
}
|
||||||
|
userRedeem = new User(redeemedByUser)
|
||||||
|
transactionLink.redeemedBy = userRedeem.id
|
||||||
|
await dbTransactionLink.save(transactionLink).catch(() => {
|
||||||
|
throw new Error('Unable to save transaction link')
|
||||||
|
})
|
||||||
|
} else if (transactionLink.redeemedBy) {
|
||||||
|
const redeemedByUser = await userRepository.findOne({ id: redeemUserId })
|
||||||
|
if (!redeemedByUser) {
|
||||||
|
throw new Error('Unable to find user that has redeemed the link')
|
||||||
|
}
|
||||||
|
userRedeem = new User(redeemedByUser)
|
||||||
|
}
|
||||||
|
return new TransactionLink(transactionLink, new User(user), userRedeem)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@ -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