Skip to content
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
2 changes: 1 addition & 1 deletion src/controllers/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});

Expand Down
12 changes: 7 additions & 5 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -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
Expand Down
10 changes: 7 additions & 3 deletions src/services/platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';
Expand Down Expand Up @@ -309,9 +309,13 @@ const validatePlatformUpdate = (platform: IAuthAndPlatform['platform'], body: IA
* @param {IPlatform} platform
* @param {IUser} user
*/
const getReputationScore = async (platform: HydratedDocument<IPlatform>, userId: ObjectId) => {
const getReputationScore = async (platform: HydratedDocument<IPlatform>, user: HydratedDocument<IUser>) => {
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 {
Expand Down
16 changes: 5 additions & 11 deletions src/services/reputationScore.service.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { HydratedDocument, ObjectId } from 'mongoose';
import { HydratedDocument } from 'mongoose';

import { IPlatform } from '@togethercrew.dev/db';

Expand All @@ -11,21 +11,20 @@ const logger = parentLogger.child({ module: 'ReputationScoreService' });

async function calculateReputationScoreForUser(
platform: HydratedDocument<IPlatform>,
userId: ObjectId,
identity: string,
): Promise<number> {
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
Expand All @@ -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;
}

Expand Down