next try for graphql api definition

This commit is contained in:
Claus-Peter Huebner 2023-09-20 17:48:24 +02:00
parent 2fe9b4e55e
commit d5ea0b7724
2 changed files with 103 additions and 77 deletions

View File

@ -23,7 +23,7 @@ export class SendCoinsClient {
dbCom.apiVersion
}/`
this.client = new GraphQLClient(this.endpoint, {
method: 'GET',
method: 'POST',
jsonSerializer: {
parse: JSON.parse,
stringify: JSON.stringify,

View File

@ -1,5 +1,5 @@
// eslint-disable-next-line @typescript-eslint/no-unused-vars
import { Args, Mutation, Resolver } from 'type-graphql'
import { Arg, Args, Mutation, Resolver } from 'type-graphql'
import { federationLogger as logger } from '@/server/logger'
import { Community as DbCommunity } from '@entity/Community'
import { PendingTransaction as DbPendingTransaction } from '@entity/PendingTransaction'
@ -21,7 +21,9 @@ import { SendCoinsResult } from '../model/SendCoinsResult'
export class SendCoinsResolver {
@Mutation(() => SendCoinsResult)
async voteForSendCoins(
@Args()
@Arg('data')
args: SendCoinsArgs,
/*
{
recipientCommunityUuid,
recipientUserIdentifier,
@ -31,34 +33,35 @@ export class SendCoinsResolver {
senderCommunityUuid,
senderUserUuid,
senderUserName,
}: SendCoinsArgs,
}
*/
): Promise<SendCoinsResult> {
logger.debug(
`voteForSendCoins() via apiVersion=1_0 ...`,
recipientCommunityUuid,
recipientUserIdentifier,
creationDate,
amount.toString(),
memo,
senderCommunityUuid,
senderUserUuid,
senderUserName,
args.recipientCommunityUuid,
args.recipientUserIdentifier,
args.creationDate,
args.amount.toString(),
args.memo,
args.senderCommunityUuid,
args.senderUserUuid,
args.senderUserName,
)
const result = new SendCoinsResult()
// first check if receiver community is correct
const homeCom = await DbCommunity.findOneBy({
communityUuid: recipientCommunityUuid,
communityUuid: args.recipientCommunityUuid,
})
if (!homeCom) {
throw new LogError(
`voteForSendCoins with wrong recipientCommunityUuid`,
recipientCommunityUuid,
args.recipientCommunityUuid,
)
}
let receiverUser
try {
// second check if receiver user exists in this community
receiverUser = await findUserByIdentifier(recipientUserIdentifier)
receiverUser = await findUserByIdentifier(args.recipientUserIdentifier)
} catch (err) {
logger.error('Error in findUserByIdentifier:', err)
throw new LogError(
@ -67,24 +70,24 @@ export class SendCoinsResolver {
)
}
try {
const txDate = new Date(creationDate)
const receiveBalance = await calculateRecipientBalance(receiverUser.id, amount, txDate)
const txDate = new Date(args.creationDate)
const receiveBalance = await calculateRecipientBalance(receiverUser.id, args.amount, txDate)
const pendingTx = DbPendingTransaction.create()
pendingTx.amount = amount
pendingTx.balance = receiveBalance ? receiveBalance.balance : amount
pendingTx.amount = args.amount
pendingTx.balance = receiveBalance ? receiveBalance.balance : args.amount
pendingTx.balanceDate = txDate
pendingTx.decay = receiveBalance ? receiveBalance.decay.decay : new Decimal(0)
pendingTx.decayStart = receiveBalance ? receiveBalance.decay.start : null
pendingTx.creationDate = new Date()
pendingTx.linkedUserCommunityUuid = senderCommunityUuid
pendingTx.linkedUserGradidoID = senderUserUuid
pendingTx.linkedUserName = senderUserName
pendingTx.memo = memo
pendingTx.linkedUserCommunityUuid = args.senderCommunityUuid
pendingTx.linkedUserGradidoID = args.senderUserUuid
pendingTx.linkedUserName = args.senderUserName
pendingTx.memo = args.memo
pendingTx.previous = receiveBalance ? receiveBalance.lastTransactionId : null
pendingTx.state = PendingTransactionState.NEW
pendingTx.typeId = TransactionTypeId.RECEIVE
pendingTx.userId = receiverUser.id
pendingTx.userCommunityUuid = recipientCommunityUuid
pendingTx.userCommunityUuid = args.recipientCommunityUuid
pendingTx.userGradidoID = receiverUser.gradidoID
pendingTx.userName = fullName(receiverUser.firstName, receiverUser.lastName)
@ -101,7 +104,9 @@ export class SendCoinsResolver {
@Mutation(() => Boolean)
async revertSendCoins(
@Args()
@Arg('data')
args: SendCoinsArgs,
/*
{
recipientCommunityUuid,
recipientUserIdentifier,
@ -112,22 +117,23 @@ export class SendCoinsResolver {
senderUserUuid,
senderUserName,
}: SendCoinsArgs,
*/
): Promise<boolean> {
logger.debug(`revertSendCoins() via apiVersion=1_0 ...`)
// first check if receiver community is correct
const homeCom = await DbCommunity.findOneBy({
communityUuid: recipientCommunityUuid,
communityUuid: args.recipientCommunityUuid,
})
if (!homeCom) {
throw new LogError(
`revertSendCoins with wrong recipientCommunityUuid`,
recipientCommunityUuid,
args.recipientCommunityUuid,
)
}
let receiverUser
try {
// second check if receiver user exists in this community
receiverUser = await findUserByIdentifier(recipientUserIdentifier)
receiverUser = await findUserByIdentifier(args.recipientUserIdentifier)
} catch (err) {
logger.error('Error in findUserByIdentifier:', err)
throw new LogError(
@ -137,16 +143,16 @@ export class SendCoinsResolver {
}
try {
const pendingTx = await DbPendingTransaction.findOneBy({
userCommunityUuid: recipientCommunityUuid,
userCommunityUuid: args.recipientCommunityUuid,
userGradidoID: receiverUser.gradidoID,
state: PendingTransactionState.NEW,
typeId: TransactionTypeId.RECEIVE,
balanceDate: new Date(creationDate),
linkedUserCommunityUuid: senderCommunityUuid,
linkedUserGradidoID: senderUserUuid,
balanceDate: new Date(args.creationDate),
linkedUserCommunityUuid: args.senderCommunityUuid,
linkedUserGradidoID: args.senderUserUuid,
})
logger.debug('XCom: revertSendCoins found pendingTX=', pendingTx)
if (pendingTx && pendingTx.amount.toString() === amount.toString()) {
if (pendingTx && pendingTx.amount.toString() === args.amount.toString()) {
logger.debug('XCom: revertSendCoins matching pendingTX for remove...')
try {
await pendingTx.remove()
@ -158,20 +164,20 @@ export class SendCoinsResolver {
logger.debug(
'XCom: revertSendCoins NOT matching pendingTX for remove:',
pendingTx?.amount,
amount,
args.amount,
)
throw new LogError(
`Can't find in revertSendCoins the pending receiver TX for args=`,
recipientCommunityUuid,
recipientUserIdentifier,
args.recipientCommunityUuid,
args.recipientUserIdentifier,
PendingTransactionState.NEW,
TransactionTypeId.RECEIVE,
creationDate,
amount,
memo,
senderCommunityUuid,
senderUserUuid,
senderUserName,
args.creationDate,
args.amount,
args.memo,
args.senderCommunityUuid,
args.senderUserUuid,
args.senderUserName,
)
}
logger.debug(`revertSendCoins()-1_0... successfull`)
@ -183,7 +189,9 @@ export class SendCoinsResolver {
@Mutation(() => Boolean)
async settleSendCoins(
@Args()
@Arg('data')
args: SendCoinsArgs,
/*
{
recipientCommunityUuid,
recipientUserIdentifier,
@ -194,24 +202,31 @@ export class SendCoinsResolver {
senderUserUuid,
senderUserName,
}: SendCoinsArgs,
*/
): Promise<boolean> {
logger.debug(
`settleSendCoins() via apiVersion=1_0 ...userCommunityUuid=${recipientCommunityUuid}, userGradidoID=${recipientUserIdentifier}, balanceDate=${creationDate},amount=${amount.valueOf()}, memo=${memo}, linkedUserCommunityUuid = ${senderCommunityUuid}, userSenderIdentifier=${senderUserUuid}, userSenderName=${senderUserName}`,
`settleSendCoins() via apiVersion=1_0 ...userCommunityUuid=${
args.recipientCommunityUuid
}, userGradidoID=${args.recipientUserIdentifier}, balanceDate=${
args.creationDate
},amount=${args.amount.valueOf()}, memo=${args.memo}, linkedUserCommunityUuid = ${
args.senderCommunityUuid
}, userSenderIdentifier=${args.senderUserUuid}, userSenderName=${args.senderUserName}`,
)
// first check if receiver community is correct
const homeCom = await DbCommunity.findOneBy({
communityUuid: recipientCommunityUuid,
communityUuid: args.recipientCommunityUuid,
})
if (!homeCom) {
throw new LogError(
`settleSendCoins with wrong recipientCommunityUuid`,
recipientCommunityUuid,
args.recipientCommunityUuid,
)
}
let receiverUser
try {
// second check if receiver user exists in this community
receiverUser = await findUserByIdentifier(recipientUserIdentifier)
receiverUser = await findUserByIdentifier(args.recipientUserIdentifier)
} catch (err) {
logger.error('Error in findUserByIdentifier:', err)
throw new LogError(
@ -220,16 +235,20 @@ export class SendCoinsResolver {
)
}
const pendingTx = await DbPendingTransaction.findOneBy({
userCommunityUuid: recipientCommunityUuid,
userCommunityUuid: args.recipientCommunityUuid,
userGradidoID: receiverUser.gradidoID,
state: PendingTransactionState.NEW,
typeId: TransactionTypeId.RECEIVE,
balanceDate: new Date(creationDate),
linkedUserCommunityUuid: senderCommunityUuid,
linkedUserGradidoID: senderUserUuid,
balanceDate: new Date(args.creationDate),
linkedUserCommunityUuid: args.senderCommunityUuid,
linkedUserGradidoID: args.senderUserUuid,
})
logger.debug('XCom: settleSendCoins found pendingTX=', pendingTx?.toString())
if (pendingTx && pendingTx.amount.toString() === amount.toString() && pendingTx.memo === memo) {
if (
pendingTx &&
pendingTx.amount.toString() === args.amount.toString() &&
pendingTx.memo === args.memo
) {
logger.debug('XCom: settleSendCoins matching pendingTX for settlement...')
await settlePendingReceiveTransaction(homeCom, receiverUser, pendingTx)
@ -239,23 +258,25 @@ export class SendCoinsResolver {
logger.debug('XCom: settlePendingReceiveTransaction NOT matching pendingTX for settlement...')
throw new LogError(
`Can't find in settlePendingReceiveTransaction the pending receiver TX for args=`,
recipientCommunityUuid,
recipientUserIdentifier,
args.recipientCommunityUuid,
args.recipientUserIdentifier,
PendingTransactionState.NEW,
TransactionTypeId.RECEIVE,
creationDate,
amount,
memo,
senderCommunityUuid,
senderUserUuid,
senderUserName,
args.creationDate,
args.amount,
args.memo,
args.senderCommunityUuid,
args.senderUserUuid,
args.senderUserName,
)
}
}
@Mutation(() => Boolean)
async revertSettledSendCoins(
@Args()
@Arg('data')
args: SendCoinsArgs,
/*
{
recipientCommunityUuid,
recipientUserIdentifier,
@ -266,22 +287,23 @@ export class SendCoinsResolver {
senderUserUuid,
senderUserName,
}: SendCoinsArgs,
*/
): Promise<boolean> {
logger.debug(`revertSettledSendCoins() via apiVersion=1_0 ...`)
// first check if receiver community is correct
const homeCom = await DbCommunity.findOneBy({
communityUuid: recipientCommunityUuid,
communityUuid: args.recipientCommunityUuid,
})
if (!homeCom) {
throw new LogError(
`revertSettledSendCoins with wrong recipientCommunityUuid`,
recipientCommunityUuid,
args.recipientCommunityUuid,
)
}
let receiverUser
try {
// second check if receiver user exists in this community
receiverUser = await findUserByIdentifier(recipientUserIdentifier)
receiverUser = await findUserByIdentifier(args.recipientUserIdentifier)
} catch (err) {
logger.error('Error in findUserByIdentifier:', err)
throw new LogError(
@ -290,16 +312,20 @@ export class SendCoinsResolver {
)
}
const pendingTx = await DbPendingTransaction.findOneBy({
userCommunityUuid: recipientCommunityUuid,
userCommunityUuid: args.recipientCommunityUuid,
userGradidoID: receiverUser.gradidoID,
state: PendingTransactionState.SETTLED,
typeId: TransactionTypeId.RECEIVE,
balanceDate: new Date(creationDate),
linkedUserCommunityUuid: senderCommunityUuid,
linkedUserGradidoID: senderUserUuid,
balanceDate: new Date(args.creationDate),
linkedUserCommunityUuid: args.senderCommunityUuid,
linkedUserGradidoID: args.senderUserUuid,
})
logger.debug('XCom: revertSettledSendCoins found pendingTX=', pendingTx)
if (pendingTx && pendingTx.amount.toString() === amount.toString() && pendingTx.memo === memo) {
if (
pendingTx &&
pendingTx.amount.toString() === args.amount.toString() &&
pendingTx.memo === args.memo
) {
logger.debug('XCom: revertSettledSendCoins matching pendingTX for remove...')
try {
await revertSettledReceiveTransaction(homeCom, receiverUser, pendingTx)
@ -311,16 +337,16 @@ export class SendCoinsResolver {
logger.debug('XCom: revertSettledSendCoins NOT matching pendingTX...')
throw new LogError(
`Can't find in revertSettledSendCoins the pending receiver TX for args=`,
recipientCommunityUuid,
recipientUserIdentifier,
args.recipientCommunityUuid,
args.recipientUserIdentifier,
PendingTransactionState.SETTLED,
TransactionTypeId.RECEIVE,
creationDate,
amount,
memo,
senderCommunityUuid,
senderUserUuid,
senderUserName,
args.creationDate,
args.amount,
args.memo,
args.senderCommunityUuid,
args.senderUserUuid,
args.senderUserName,
)
}
logger.debug(`revertSendCoins()-1_0... successfull`)