mirror of
https://github.com/IT4Change/gradido.git
synced 2025-12-13 07:45:54 +00:00
call transaction resolver to redeem transactoin link. Optional senderId parameter for sendCoins to redeem links
This commit is contained in:
parent
277d0a4b38
commit
81cd858eb4
@ -1,4 +1,4 @@
|
||||
import { ArgsType, Field } from 'type-graphql'
|
||||
import { ArgsType, Field, Int } from 'type-graphql'
|
||||
import Decimal from 'decimal.js-light'
|
||||
|
||||
@ArgsType()
|
||||
@ -11,4 +11,7 @@ export default class TransactionSendArgs {
|
||||
|
||||
@Field(() => String)
|
||||
memo: string
|
||||
|
||||
@Field(() => Int, { nullable: true })
|
||||
senderId?: number
|
||||
}
|
||||
|
||||
@ -13,6 +13,7 @@ import { RIGHTS } from '@/auth/RIGHTS'
|
||||
import { randomBytes } from 'crypto'
|
||||
import { User } from '@model/User'
|
||||
import { calculateDecay } from '@/util/decay'
|
||||
import { TransactionResolver } from './TransactionResolver'
|
||||
|
||||
// TODO: do not export, test it inside the resolver
|
||||
export const transactionLinkCode = (date: Date): string => {
|
||||
@ -123,9 +124,43 @@ export class TransactionLinkResolver {
|
||||
return new TransactionLink(transactionLink, new User(user), userRedeem)
|
||||
}
|
||||
|
||||
/*
|
||||
@Authorized([RIGHTS.REDEEM_TRANSACTION_LINK])
|
||||
@Mutation(() => Boolean)
|
||||
async redeemTransactionLink(@Arg('id') id: number, @Ctx() context: any)
|
||||
*/
|
||||
async redeemTransactionLink(@Arg('id') id: number, @Ctx() context: any): Promise<boolean> {
|
||||
const userRepository = getCustomRepository(UserRepository)
|
||||
const user = await userRepository.findByPubkeyHex(context.pubKey)
|
||||
const transactionLink = await dbTransactionLink.findOneOrFail({ id })
|
||||
|
||||
const now = new Date()
|
||||
|
||||
if (user.id === transactionLink.userId) {
|
||||
throw new Error('Cannot redeem own transaction link.')
|
||||
}
|
||||
|
||||
if (transactionLink.validUntil.getTime() < now.getTime()) {
|
||||
throw new Error('Transaction Link is not valid anymore.')
|
||||
}
|
||||
|
||||
if (transactionLink.redeemedBy) {
|
||||
throw new Error('Transaction Link already redeemed.')
|
||||
}
|
||||
|
||||
const transactionResolver = new TransactionResolver()
|
||||
transactionResolver.sendCoins(
|
||||
{
|
||||
email: user.email,
|
||||
amount: transactionLink.amount,
|
||||
memo: transactionLink.memo,
|
||||
senderId: transactionLink.userId,
|
||||
},
|
||||
context,
|
||||
)
|
||||
transactionLink.redeemedAt = now
|
||||
transactionLink.redeemedBy = user.id
|
||||
transactionLink.save().catch(() => {
|
||||
throw new Error('Could not update transaction link.')
|
||||
})
|
||||
|
||||
return true
|
||||
}
|
||||
}
|
||||
|
||||
@ -145,12 +145,14 @@ export class TransactionResolver {
|
||||
@Authorized([RIGHTS.SEND_COINS])
|
||||
@Mutation(() => String)
|
||||
async sendCoins(
|
||||
@Args() { email, amount, memo }: TransactionSendArgs,
|
||||
@Args() { email, amount, memo, senderId = 0 }: TransactionSendArgs,
|
||||
@Ctx() context: any,
|
||||
): Promise<boolean> {
|
||||
// TODO this is subject to replay attacks
|
||||
const userRepository = getCustomRepository(UserRepository)
|
||||
const senderUser = await userRepository.findByPubkeyHex(context.pubKey)
|
||||
const senderUser = senderId
|
||||
? await dbUser.findOneOrFail({ id: senderId })
|
||||
: await userRepository.findByPubkeyHex(context.pubKey)
|
||||
if (senderUser.pubKey.length !== 32) {
|
||||
throw new Error('invalid sender public key')
|
||||
}
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user