From 74d8a7b5e812627a748b9828f7ee3b8bac16ea0f Mon Sep 17 00:00:00 2001 From: Behzad-rabiei Date: Wed, 19 Feb 2025 11:17:19 +0100 Subject: [PATCH 1/2] fix: fix the issue with identity id of the user --- src/controllers/platform.controller.ts | 18 +++++++----------- src/index.ts | 12 +++++++----- src/services/platform.service.ts | 10 +++++++--- src/services/reputationScore.service.ts | 16 +++++----------- 4 files changed, 26 insertions(+), 30 deletions(-) diff --git a/src/controllers/platform.controller.ts b/src/controllers/platform.controller.ts index 15ba5fdc..7245d371 100644 --- a/src/controllers/platform.controller.ts +++ b/src/controllers/platform.controller.ts @@ -6,19 +6,14 @@ import { DatabaseManager, PlatformNames } from '@togethercrew.dev/db'; import config from '../config'; import parentLogger from '../config/logger'; -import { discord, generateCodeChallenge, generateCodeVerifier, generateState, google, twitter } from '../config/oAtuh2'; +import { + discord, generateCodeChallenge, generateCodeVerifier, generateState, google, twitter +} from '../config/oAtuh2'; import { IAuthAndPlatform, ISessionRequest } from '../interfaces'; import { IAuthRequest } from '../interfaces/Request.interface'; import { - discordServices, - discourseService, - githubService, - googleService, - notionService, - platformService, - tokenService, - twitterService, - userService, + discordServices, discourseService, githubService, googleService, notionService, platformService, + tokenService, twitterService, userService } from '../services'; import { catchAsync, pick } from '../utils'; @@ -386,7 +381,8 @@ const requestAccess = catchAsync(async function (req: ISessionRequest, res: Resp }); const getReputationScore = catchAsync(async function (req: IAuthAndPlatform, res: Response) { - const reputationScore = await platformService.getReputationScore(req.platform, req.user.id); + + const reputationScore = await platformService.getReputationScore(req.platform, req.user); res.send(reputationScore); }); diff --git a/src/index.ts b/src/index.ts index 569e3c1c..6d7ccb6a 100644 --- a/src/index.ts +++ b/src/index.ts @@ -1,19 +1,21 @@ import mongoose from 'mongoose'; + +import { announcementEmitter } from '@togethercrew.dev/db'; +import RabbitMQ, { MBConnection } from '@togethercrew.dev/tc-messagebroker'; + import app from './app'; import config from './config'; -import RabbitMQ, { MBConnection, Queue, Event } from '@togethercrew.dev/tc-messagebroker'; import logger from './config/logger'; -import { announcementEmitter } from '@togethercrew.dev/db'; -import { announcementService } from './services'; import rabbitMQClient from './rabbitmq/'; -import initializeHandlers from './rabbitmq/handlers'; +import { announcementService } from './services'; + mongoose.set('strictQuery', false); // Connect to RabbitMQ const setupRabbitMq = async () => { // Establish connection await rabbitMQClient.connect(); // Initialize all event handlers - initializeHandlers(); + // initializeHandlers(); }; // Connect to Message Broker DB diff --git a/src/services/platform.service.ts b/src/services/platform.service.ts index 3b47b7f5..f9e40f5b 100644 --- a/src/services/platform.service.ts +++ b/src/services/platform.service.ts @@ -2,7 +2,7 @@ import { Snowflake } from 'discord.js'; import httpStatus from 'http-status'; import { FilterQuery, HydratedDocument, ObjectId, Types } from 'mongoose'; -import { IPlatform, Platform, PlatformNames } from '@togethercrew.dev/db'; +import { IPlatform, IUser, Platform, PlatformNames } from '@togethercrew.dev/db'; import { analyzerAction, analyzerWindow } from '../config/analyzer.statics'; import parentLogger from '../config/logger'; @@ -309,9 +309,13 @@ const validatePlatformUpdate = (platform: IAuthAndPlatform['platform'], body: IA * @param {IPlatform} platform * @param {IUser} user */ -const getReputationScore = async (platform: HydratedDocument, userId: ObjectId) => { +const getReputationScore = async (platform: HydratedDocument, user: HydratedDocument) => { + const identity = user.identities.find((id) => id.provider === platform.name); + if (!identity) { + throw new ApiError(httpStatus.BAD_REQUEST, `User need to login with the ${platform.name} account`); + } return { - reputationScore: (await reputationScoreService.calculateReputationScoreForUser(platform, userId)) * 100, + reputationScore: (await reputationScoreService.calculateReputationScoreForUser(platform, identity.id)) * 100, }; }; export default { diff --git a/src/services/reputationScore.service.ts b/src/services/reputationScore.service.ts index a7633669..ddc35948 100644 --- a/src/services/reputationScore.service.ts +++ b/src/services/reputationScore.service.ts @@ -1,4 +1,4 @@ -import { HydratedDocument, ObjectId } from 'mongoose'; +import { HydratedDocument } from 'mongoose'; import { IPlatform } from '@togethercrew.dev/db'; @@ -11,21 +11,20 @@ const logger = parentLogger.child({ module: 'ReputationScoreService' }); async function calculateReputationScoreForUser( platform: HydratedDocument, - userId: ObjectId, + identity: string, ): Promise { const platformName = platform.name as SupportedNeo4jPlatforms; const memberLabel = NEO4J_PLATFORM_INFO[platformName].member; const platformId = platform.id; - const reputationScoreQuery = buildReputationScoreQuery(userId, platformId, memberLabel); + const reputationScoreQuery = buildReputationScoreQuery(identity, platformId, memberLabel); const neo4jData = await Neo4j.read(reputationScoreQuery); - return extractReputationScoreFromNeo4jData(neo4jData); } -function buildReputationScoreQuery(userId: ObjectId, platformId: string, memberLabel: string): string { +function buildReputationScoreQuery(identity: string, platformId: string, memberLabel: string): string { return ` - MATCH (:${memberLabel} {id: "${userId}"})-[r:HAVE_METRICS {platformId: "${platformId}"}]->(a) + MATCH (:${memberLabel} {id: "${identity}"})-[r:HAVE_METRICS {platformId: "${platformId}"}]->(a) WITH r.date as metrics_date, r.closenessCentrality as memberScore ORDER BY metrics_date DESC LIMIT 1 @@ -34,18 +33,13 @@ function buildReputationScoreQuery(userId: ObjectId, platformId: string, memberL RETURN memberScore / maxScore AS reputation_score `; } - function extractReputationScoreFromNeo4jData(neo4jData: any): number { const { records } = neo4jData; - logger.debug(`Neo4j Records: ${JSON.stringify(records)}`); - if (records.length === 0) { return 0; } - const reputationScoreResponse = records[0]; const reputationScore = reputationScoreResponse.get('reputation_score'); - return reputationScore || 0; } From c1c70a1b74f0c46e9d0b1706dff81834be3bf0af Mon Sep 17 00:00:00 2001 From: Behzad-rabiei Date: Wed, 19 Feb 2025 11:18:39 +0100 Subject: [PATCH 2/2] style: format the code --- src/controllers/platform.controller.ts | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/controllers/platform.controller.ts b/src/controllers/platform.controller.ts index 7245d371..5c11067f 100644 --- a/src/controllers/platform.controller.ts +++ b/src/controllers/platform.controller.ts @@ -6,14 +6,19 @@ import { DatabaseManager, PlatformNames } from '@togethercrew.dev/db'; import config from '../config'; import parentLogger from '../config/logger'; -import { - discord, generateCodeChallenge, generateCodeVerifier, generateState, google, twitter -} from '../config/oAtuh2'; +import { discord, generateCodeChallenge, generateCodeVerifier, generateState, google, twitter } from '../config/oAtuh2'; import { IAuthAndPlatform, ISessionRequest } from '../interfaces'; import { IAuthRequest } from '../interfaces/Request.interface'; import { - discordServices, discourseService, githubService, googleService, notionService, platformService, - tokenService, twitterService, userService + discordServices, + discourseService, + githubService, + googleService, + notionService, + platformService, + tokenService, + twitterService, + userService, } from '../services'; import { catchAsync, pick } from '../utils'; @@ -381,7 +386,6 @@ const requestAccess = catchAsync(async function (req: ISessionRequest, res: Resp }); const getReputationScore = catchAsync(async function (req: IAuthAndPlatform, res: Response) { - const reputationScore = await platformService.getReputationScore(req.platform, req.user); res.send(reputationScore); });