diff --git a/backend/src/federation/client/1_0/model/SendCoinsArgs.ts b/backend/src/federation/client/1_0/model/SendCoinsArgs.ts index fb97da925..73e166308 100644 --- a/backend/src/federation/client/1_0/model/SendCoinsArgs.ts +++ b/backend/src/federation/client/1_0/model/SendCoinsArgs.ts @@ -26,4 +26,7 @@ export class SendCoinsArgs { @Field(() => String) senderUserName: string + + @Field(() => String) + senderAlias: string } diff --git a/backend/src/graphql/resolver/util/processXComSendCoins.ts b/backend/src/graphql/resolver/util/processXComSendCoins.ts index af6826bd1..1afa7a1c0 100644 --- a/backend/src/graphql/resolver/util/processXComSendCoins.ts +++ b/backend/src/graphql/resolver/util/processXComSendCoins.ts @@ -85,6 +85,7 @@ export async function processXComPendingSendCoins( } args.senderUserUuid = sender.gradidoID args.senderUserName = fullName(sender.firstName, sender.lastName) + args.senderAlias = sender.alias logger.debug(`X-Com: ready for voteForSendCoins with args=`, args) voteResult = await client.voteForSendCoins(args) logger.debug(`X-Com: returned from voteForSendCoins:`, voteResult) @@ -212,6 +213,7 @@ export async function processXComCommittingSendCoins( if (pendingTx.userName) { args.senderUserName = pendingTx.userName } + args.senderAlias = sender.alias logger.debug(`X-Com: ready for settleSendCoins with args=`, args) const acknowledge = await client.settleSendCoins(args) logger.debug(`X-Com: returnd from settleSendCoins:`, acknowledge) diff --git a/federation/src/graphql/api/1_0/model/SendCoinsArgs.ts b/federation/src/graphql/api/1_0/model/SendCoinsArgs.ts index e658209ca..1337bbc6c 100644 --- a/federation/src/graphql/api/1_0/model/SendCoinsArgs.ts +++ b/federation/src/graphql/api/1_0/model/SendCoinsArgs.ts @@ -26,4 +26,7 @@ export class SendCoinsArgs { @Field(() => String) senderUserName: string + + @Field(() => String) + senderAlias: string } diff --git a/federation/src/graphql/api/1_0/resolver/SendCoinsResolver.ts b/federation/src/graphql/api/1_0/resolver/SendCoinsResolver.ts index ef37c0d00..a05ab2c7d 100644 --- a/federation/src/graphql/api/1_0/resolver/SendCoinsResolver.ts +++ b/federation/src/graphql/api/1_0/resolver/SendCoinsResolver.ts @@ -15,6 +15,7 @@ import { revertSettledReceiveTransaction } from '../util/revertSettledReceiveTra import { findUserByIdentifier } from '@/graphql/util/findUserByIdentifier' import { SendCoinsResult } from '../model/SendCoinsResult' import Decimal from 'decimal.js-light' +import { storeForeignUser } from '../util/storeForeignUser' @Resolver() // eslint-disable-next-line @typescript-eslint/no-unused-vars @@ -236,6 +237,16 @@ export class SendCoinsResolver { logger.debug('XCom: settleSendCoins matching pendingTX for settlement...') await settlePendingReceiveTransaction(homeCom, receiverUser, pendingTx) + // after successful x-com-tx store the recipient as foreign user + logger.debug('store recipient as foreign user...') + if (await storeForeignUser(args)) { + logger.info( + 'X-Com: new foreign user inserted successfully...', + args.senderCommunityUuid, + args.senderUserUuid, + ) + } + logger.debug(`XCom: settlePendingReceiveTransaction()-1_0... successfull`) return true } else { diff --git a/federation/src/graphql/api/1_0/util/storeForeignUser.ts b/federation/src/graphql/api/1_0/util/storeForeignUser.ts new file mode 100644 index 000000000..d8f6e3748 --- /dev/null +++ b/federation/src/graphql/api/1_0/util/storeForeignUser.ts @@ -0,0 +1,75 @@ +import { User as DbUser } from '@entity/User' + +import { federationLogger as logger } from '@/server/logger' +import { SendCoinsArgs } from '../model/SendCoinsArgs' + +export async function storeForeignUser(args: SendCoinsArgs): Promise { + if (args.senderCommunityUuid !== null && args.senderUserUuid !== null) { + try { + const user = await DbUser.findOne({ + where: { + foreign: true, + communityUuid: args.senderCommunityUuid, + gradidoID: args.senderUserUuid, + }, + }) + if (!user) { + logger.debug( + 'X-Com: no foreignUser found for:', + args.senderCommunityUuid, + args.senderUserUuid, + ) + console.log( + 'X-Com: no foreignUser found for:', + args.senderCommunityUuid, + args.senderUserUuid, + ) + let foreignUser = DbUser.create() + foreignUser.foreign = true + if (args.senderAlias !== null) { + foreignUser.alias = args.senderAlias + } + foreignUser.communityUuid = args.senderCommunityUuid + if (args.senderUserName !== null) { + foreignUser.firstName = args.senderUserName.slice(0, args.senderUserName.indexOf(' ')) + foreignUser.firstName = args.senderUserName.slice( + args.senderUserName.indexOf(' '), + args.senderUserName.length, + ) + } + foreignUser.gradidoID = args.senderUserUuid + foreignUser = await DbUser.save(foreignUser) + logger.debug('X-Com: new foreignUser inserted:', foreignUser) + console.log('X-Com: new foreignUser inserted:', foreignUser) + + return true + } else if ( + user.firstName !== args.senderUserName.slice(0, args.senderUserName.indexOf(' ')) || + user.lastName !== + args.senderUserName.slice(args.senderUserName.indexOf(' '), args.senderUserName.length) || + user.alias !== args.senderAlias + ) { + logger.warn( + 'X-Com: foreignUser still exists, but with different name or alias:', + user, + args, + ) + console.log( + 'X-Com: foreignUser still exists, but with different name or alias:', + user, + args, + ) + return false + } else { + logger.debug('X-Com: foreignUser still exists...:', user) + console.log('X-Com: foreignUser still exists...:', user) + return true + } + } catch (err) { + logger.error('X-Com: error in storeForeignUser;', err) + console.log('X-Com: error in storeForeignUser;', err) + return false + } + } + return false +}