Skip to content

Commit c91ad3b

Browse files
committed
getOrCreateRecommenderWithTelegramId
Signed-off-by: MarcoMandar <malicemandar@gmail.com>
1 parent 1b29373 commit c91ad3b

File tree

2 files changed

+66
-4
lines changed

2 files changed

+66
-4
lines changed

packages/plugin-solana/src/adapters/trustScoreDatabase.ts

+64-2
Original file line numberDiff line numberDiff line change
@@ -403,8 +403,6 @@ export class TrustScoreDatabase {
403403
}
404404
}
405405

406-
// get Or Create Recommender with discord id
407-
408406
/**
409407
* Retrieves an existing recommender or creates a new one if not found.
410408
* Also initializes metrics for the recommender if they haven't been initialized yet.
@@ -469,6 +467,70 @@ export class TrustScoreDatabase {
469467
}
470468
}
471469

470+
/**
471+
* Retrieves an existing recommender or creates a new one if not found.
472+
* Also initializes metrics for the recommender if they haven't been initialized yet.
473+
* @param telegramId Telegram ID of the recommender
474+
* @returns Recommender object with all details, or null if failed
475+
*/
476+
477+
async getOrCreateRecommenderWithTelegramId(
478+
telegramId: string
479+
): Promise<Recommender | null> {
480+
try {
481+
// Begin a transaction
482+
const transaction = this.db.transaction(() => {
483+
// Attempt to retrieve the recommender
484+
const existingRecommender = this.getRecommender(telegramId);
485+
if (existingRecommender) {
486+
// Recommender exists, ensure metrics are initialized
487+
this.initializeRecommenderMetrics(existingRecommender.id!);
488+
return existingRecommender;
489+
}
490+
491+
// Recommender does not exist, create a new one
492+
const newRecommender = {
493+
id: uuidv4(),
494+
address: telegramId,
495+
telegramId: telegramId,
496+
};
497+
const newRecommenderId = this.addRecommender(newRecommender);
498+
if (!newRecommenderId) {
499+
throw new Error("Failed to add new recommender.");
500+
}
501+
502+
// Initialize metrics for the new recommender
503+
const metricsInitialized =
504+
this.initializeRecommenderMetrics(newRecommenderId);
505+
if (!metricsInitialized) {
506+
throw new Error(
507+
"Failed to initialize recommender metrics."
508+
);
509+
}
510+
511+
// Retrieve and return the newly created recommender
512+
const recommender = this.getRecommender(newRecommenderId);
513+
if (!recommender) {
514+
throw new Error(
515+
"Failed to retrieve the newly created recommender."
516+
);
517+
}
518+
519+
return recommender;
520+
});
521+
522+
// Execute the transaction and return the recommender
523+
const recommenderResult = transaction();
524+
return recommenderResult;
525+
} catch (error) {
526+
console.error(
527+
"Error in getOrCreateRecommenderWithTelegramId:",
528+
error
529+
);
530+
return null;
531+
}
532+
}
533+
472534
/**
473535
* Initializes metrics for a recommender if not present.
474536
* @param recommenderId Recommender's UUID

packages/plugin-solana/src/providers/trustScoreProvider.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -333,7 +333,7 @@ export class TrustScoreManager {
333333
data: TradeData
334334
): Promise<TradePerformance> {
335335
const recommender =
336-
await this.trustScoreDb.getOrCreateRecommenderWithDiscordId(
336+
await this.trustScoreDb.getOrCreateRecommenderWithTelegramId(
337337
recommenderId
338338
);
339339
const processedData: ProcessedTokenData =
@@ -444,7 +444,7 @@ export class TrustScoreManager {
444444
isSimulation: boolean
445445
) {
446446
const recommender =
447-
await this.trustScoreDb.getOrCreateRecommenderWithDiscordId(
447+
await this.trustScoreDb.getOrCreateRecommenderWithTelegramId(
448448
recommenderId
449449
);
450450
const processedData: ProcessedTokenData =

0 commit comments

Comments
 (0)