From c91ad3b09e3145f493b4ab6d182a2e5f9fbdb6ab Mon Sep 17 00:00:00 2001 From: MarcoMandar Date: Sat, 16 Nov 2024 03:38:43 +0200 Subject: [PATCH] getOrCreateRecommenderWithTelegramId Signed-off-by: MarcoMandar --- .../src/adapters/trustScoreDatabase.ts | 66 ++++++++++++++++++- .../src/providers/trustScoreProvider.ts | 4 +- 2 files changed, 66 insertions(+), 4 deletions(-) diff --git a/packages/plugin-solana/src/adapters/trustScoreDatabase.ts b/packages/plugin-solana/src/adapters/trustScoreDatabase.ts index 417bdf76f9..6d2439b714 100644 --- a/packages/plugin-solana/src/adapters/trustScoreDatabase.ts +++ b/packages/plugin-solana/src/adapters/trustScoreDatabase.ts @@ -403,8 +403,6 @@ export class TrustScoreDatabase { } } - // get Or Create Recommender with discord id - /** * Retrieves an existing recommender or creates a new one if not found. * Also initializes metrics for the recommender if they haven't been initialized yet. @@ -469,6 +467,70 @@ export class TrustScoreDatabase { } } + /** + * Retrieves an existing recommender or creates a new one if not found. + * Also initializes metrics for the recommender if they haven't been initialized yet. + * @param telegramId Telegram ID of the recommender + * @returns Recommender object with all details, or null if failed + */ + + async getOrCreateRecommenderWithTelegramId( + telegramId: string + ): Promise { + try { + // Begin a transaction + const transaction = this.db.transaction(() => { + // Attempt to retrieve the recommender + const existingRecommender = this.getRecommender(telegramId); + if (existingRecommender) { + // Recommender exists, ensure metrics are initialized + this.initializeRecommenderMetrics(existingRecommender.id!); + return existingRecommender; + } + + // Recommender does not exist, create a new one + const newRecommender = { + id: uuidv4(), + address: telegramId, + telegramId: telegramId, + }; + const newRecommenderId = this.addRecommender(newRecommender); + if (!newRecommenderId) { + throw new Error("Failed to add new recommender."); + } + + // Initialize metrics for the new recommender + const metricsInitialized = + this.initializeRecommenderMetrics(newRecommenderId); + if (!metricsInitialized) { + throw new Error( + "Failed to initialize recommender metrics." + ); + } + + // Retrieve and return the newly created recommender + const recommender = this.getRecommender(newRecommenderId); + if (!recommender) { + throw new Error( + "Failed to retrieve the newly created recommender." + ); + } + + return recommender; + }); + + // Execute the transaction and return the recommender + const recommenderResult = transaction(); + return recommenderResult; + } catch (error) { + console.error( + "Error in getOrCreateRecommenderWithTelegramId:", + error + ); + return null; + } + } + /** * Initializes metrics for a recommender if not present. * @param recommenderId Recommender's UUID diff --git a/packages/plugin-solana/src/providers/trustScoreProvider.ts b/packages/plugin-solana/src/providers/trustScoreProvider.ts index 8243879f35..06a90e7e04 100644 --- a/packages/plugin-solana/src/providers/trustScoreProvider.ts +++ b/packages/plugin-solana/src/providers/trustScoreProvider.ts @@ -333,7 +333,7 @@ export class TrustScoreManager { data: TradeData ): Promise { const recommender = - await this.trustScoreDb.getOrCreateRecommenderWithDiscordId( + await this.trustScoreDb.getOrCreateRecommenderWithTelegramId( recommenderId ); const processedData: ProcessedTokenData = @@ -444,7 +444,7 @@ export class TrustScoreManager { isSimulation: boolean ) { const recommender = - await this.trustScoreDb.getOrCreateRecommenderWithDiscordId( + await this.trustScoreDb.getOrCreateRecommenderWithTelegramId( recommenderId ); const processedData: ProcessedTokenData =