Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

getOrCreateRecommenderWithTelegramId #345

Merged
merged 2 commits into from
Nov 16, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
66 changes: 64 additions & 2 deletions packages/plugin-solana/src/adapters/trustScoreDatabase.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down Expand Up @@ -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<Recommender | null> {
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
Expand Down
4 changes: 2 additions & 2 deletions packages/plugin-solana/src/providers/trustScoreProvider.ts
Original file line number Diff line number Diff line change
Expand Up @@ -333,7 +333,7 @@ export class TrustScoreManager {
data: TradeData
): Promise<TradePerformance> {
const recommender =
await this.trustScoreDb.getOrCreateRecommenderWithDiscordId(
await this.trustScoreDb.getOrCreateRecommenderWithTelegramId(
recommenderId
);
const processedData: ProcessedTokenData =
Expand Down Expand Up @@ -444,7 +444,7 @@ export class TrustScoreManager {
isSimulation: boolean
) {
const recommender =
await this.trustScoreDb.getOrCreateRecommenderWithDiscordId(
await this.trustScoreDb.getOrCreateRecommenderWithTelegramId(
recommenderId
);
const processedData: ProcessedTokenData =
Expand Down
Loading