mirror of
https://github.com/IT4Change/gradido.git
synced 2026-02-06 09:56:05 +00:00
adjust backend to moving logic into dlt-connector
This commit is contained in:
parent
6505e1f0cb
commit
75a7481a1b
@ -1,6 +1,4 @@
|
||||
import { Contribution } from '@entity/Contribution'
|
||||
import { Transaction as DbTransaction } from '@entity/Transaction'
|
||||
import { User as DbUser } from '@entity/User'
|
||||
import { gql, GraphQLClient } from 'graphql-request'
|
||||
|
||||
import { CONFIG } from '@/config'
|
||||
@ -79,98 +77,24 @@ export class DltConnectorClient {
|
||||
return DltConnectorClient.instance
|
||||
}
|
||||
|
||||
protected async getCorrectUserUUID(
|
||||
transaction: DbTransaction,
|
||||
type: 'sender' | 'recipient',
|
||||
): Promise<string> {
|
||||
let confirmingUserId: number | undefined
|
||||
logger.info('confirming user id', confirmingUserId)
|
||||
switch (transaction.typeId) {
|
||||
case TransactionTypeId.CREATION:
|
||||
confirmingUserId = (
|
||||
await Contribution.findOneOrFail({ where: { transactionId: transaction.id } })
|
||||
).confirmedBy
|
||||
if (!confirmingUserId) {
|
||||
throw new LogError(
|
||||
"couldn't find id of confirming moderator for contribution transaction!",
|
||||
)
|
||||
}
|
||||
if (type === 'sender') {
|
||||
return (await DbUser.findOneOrFail({ where: { id: confirmingUserId } })).gradidoID
|
||||
} else if (type === 'recipient') {
|
||||
return transaction.userGradidoID
|
||||
}
|
||||
break
|
||||
case TransactionTypeId.SEND:
|
||||
if (type === 'sender') {
|
||||
return transaction.userGradidoID
|
||||
} else if (type === 'recipient') {
|
||||
if (!transaction.linkedUserGradidoID) {
|
||||
throw new LogError('missing linked user gradido id')
|
||||
}
|
||||
return transaction.linkedUserGradidoID
|
||||
}
|
||||
break
|
||||
case TransactionTypeId.RECEIVE:
|
||||
if (type === 'sender') {
|
||||
if (!transaction.linkedUserGradidoID) {
|
||||
throw new LogError('missing linked user gradido id')
|
||||
}
|
||||
return transaction.linkedUserGradidoID
|
||||
} else if (type === 'recipient') {
|
||||
return transaction.userGradidoID
|
||||
}
|
||||
}
|
||||
throw new LogError('unhandled case')
|
||||
}
|
||||
|
||||
protected async getCorrectUserIdentifier(
|
||||
transaction: DbTransaction,
|
||||
senderCommunityUuid: string,
|
||||
type: 'sender' | 'recipient',
|
||||
recipientCommunityUuid?: string,
|
||||
): Promise<UserIdentifier> {
|
||||
// sender and receiver user on creation transaction
|
||||
// sender user on send transaction (SEND and RECEIVE)
|
||||
if (type === 'sender' || transaction.typeId === TransactionTypeId.CREATION) {
|
||||
return {
|
||||
uuid: await this.getCorrectUserUUID(transaction, type),
|
||||
communityUuid: senderCommunityUuid,
|
||||
}
|
||||
}
|
||||
// recipient user on SEND and RECEIVE transactions
|
||||
return {
|
||||
uuid: await this.getCorrectUserUUID(transaction, type),
|
||||
communityUuid: recipientCommunityUuid ?? senderCommunityUuid,
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* transmit transaction via dlt-connector to iota
|
||||
* and update dltTransactionId of transaction in db with iota message id
|
||||
*/
|
||||
public async transmitTransaction(
|
||||
transaction: DbTransaction,
|
||||
senderCommunityUuid: string,
|
||||
recipientCommunityUuid?: string,
|
||||
): Promise<boolean> {
|
||||
public async transmitTransaction(transaction: DbTransaction): Promise<boolean> {
|
||||
const typeString = getTransactionTypeString(transaction.typeId)
|
||||
// no negative values in dlt connector, gradido concept don't use negative values so the code don't use it too
|
||||
const amountString = transaction.amount.abs().toString()
|
||||
const params = {
|
||||
input: {
|
||||
senderUser: await this.getCorrectUserIdentifier(
|
||||
transaction,
|
||||
senderCommunityUuid,
|
||||
'sender',
|
||||
recipientCommunityUuid,
|
||||
),
|
||||
recipientUser: await this.getCorrectUserIdentifier(
|
||||
transaction,
|
||||
senderCommunityUuid,
|
||||
'recipient',
|
||||
recipientCommunityUuid,
|
||||
),
|
||||
user: {
|
||||
uuid: transaction.userGradidoID,
|
||||
communityUuid: transaction.userCommunityUuid,
|
||||
} as UserIdentifier,
|
||||
linkedUser: {
|
||||
uuid: transaction.linkedUserGradidoID,
|
||||
communityUuid: transaction.linkedUserCommunityUuid,
|
||||
} as UserIdentifier,
|
||||
amount: amountString,
|
||||
type: typeString,
|
||||
createdAt: transaction.balanceDate.toISOString(),
|
||||
|
||||
@ -1,5 +1,4 @@
|
||||
import { IsNull } from '@dbTools/typeorm'
|
||||
import { Community } from '@entity/Community'
|
||||
import { DltTransaction } from '@entity/DltTransaction'
|
||||
import { Transaction } from '@entity/Transaction'
|
||||
|
||||
@ -18,12 +17,6 @@ export async function sendTransactionsToDltConnector(): Promise<void> {
|
||||
try {
|
||||
await createDltTransactions()
|
||||
const dltConnector = DltConnectorClient.getInstance()
|
||||
// TODO: get actual communities from users
|
||||
const homeCommunity = await Community.findOneOrFail({ where: { foreign: false } })
|
||||
const senderCommunityUuid = homeCommunity.communityUuid
|
||||
if (!senderCommunityUuid) {
|
||||
throw new Error('Cannot find community uuid of home community')
|
||||
}
|
||||
if (dltConnector) {
|
||||
logger.debug('with sending to DltConnector...')
|
||||
const dltTransactions = await DltTransaction.find({
|
||||
@ -37,10 +30,7 @@ export async function sendTransactionsToDltConnector(): Promise<void> {
|
||||
continue
|
||||
}
|
||||
try {
|
||||
const result = await dltConnector.transmitTransaction(
|
||||
dltTx.transaction,
|
||||
senderCommunityUuid,
|
||||
)
|
||||
const result = await dltConnector.transmitTransaction(dltTx.transaction)
|
||||
// message id isn't known at this point of time, because transaction will not direct sended to iota,
|
||||
// it will first go to db and then sended, if no transaction is in db before
|
||||
if (result) {
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user