diff --git a/src/controllers/platform.controller.ts b/src/controllers/platform.controller.ts index 15ba5fd..5c11067 100644 --- a/src/controllers/platform.controller.ts +++ b/src/controllers/platform.controller.ts @@ -386,7 +386,7 @@ 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 569e3c1..6d7ccb6 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 3b47b7f..f9e40f5 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 a763366..ddc3594 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; }