From c4704964d82fbb8d9d73bc11a31bdd7a70d5f736 Mon Sep 17 00:00:00 2001 From: "maciek.nabialek" Date: Thu, 14 Mar 2024 16:58:51 +0100 Subject: [PATCH 1/5] Alignment of naming convention and endpoint structure --- .../apps/human-app/server/src/app.module.ts | 4 +-- .../exchange-oracle-api.gateway.ts} | 6 ++--- .../exchange-oracle-api.mapper.ts} | 2 +- .../exchange-oracle-api.module.ts | 11 ++++++++ .../spec/exchange-oracle-api.gateway.spec.ts} | 20 +++++++------- .../external-api/external-api.module.ts | 11 -------- .../job-assignment/job-assignment.module.ts | 4 +-- .../job-assignment/job-assignment.service.ts | 4 +-- .../jobs-discovery.controller.ts | 4 +-- .../jobs-discovery/jobs-discovery.module.ts | 4 +-- .../jobs-discovery/jobs-discovery.service.ts | 4 +-- .../spec/jobs-discovery.controller.spec.ts | 2 +- .../oracle-discovery.controller.ts | 4 +-- .../spec/oracle-discovery.controller.spec.ts | 2 +- .../interfaces/oracle-statistics.interface.ts | 2 +- .../interfaces/user-statistics.interface.ts | 2 +- .../statistics/spec/statistics.fixtures.ts | 12 ++++----- .../spec/statistisc.controller.spec.ts | 27 ++++++++++++------- .../statistics/statistics.controller.ts | 4 +-- .../modules/statistics/statistics.module.ts | 4 +-- .../modules/statistics/statistics.service.ts | 8 +++--- 21 files changed, 75 insertions(+), 66 deletions(-) rename packages/apps/human-app/server/src/integrations/{external-api/external-api.gateway.ts => exchange-oracle-api/exchange-oracle-api.gateway.ts} (95%) rename packages/apps/human-app/server/src/integrations/{external-api/external-api.mapper.ts => exchange-oracle-api/exchange-oracle-api.mapper.ts} (96%) create mode 100644 packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts rename packages/apps/human-app/server/src/integrations/{external-api/spec/external-api.gateway.spec.ts => exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts} (87%) delete mode 100644 packages/apps/human-app/server/src/integrations/external-api/external-api.module.ts diff --git a/packages/apps/human-app/server/src/app.module.ts b/packages/apps/human-app/server/src/app.module.ts index 4ec9d16d5b..5d662159d3 100644 --- a/packages/apps/human-app/server/src/app.module.ts +++ b/packages/apps/human-app/server/src/app.module.ts @@ -21,7 +21,7 @@ import { JobAssignmentController } from './modules/job-assignment/job-assignment import { JobAssignmentModule } from './modules/job-assignment/job-assignment.module'; import { StatisticsModule } from './modules/statistics/statistics.module'; import { StatisticsController } from './modules/statistics/statistics.controller'; -import { ExternalApiModule } from './integrations/external-api/external-api.module'; +import { ExchangeOracleApiModule } from './integrations/exchange-oracle-api/exchange-oracle-api.module'; @Module({ imports: [ @@ -40,7 +40,7 @@ import { ExternalApiModule } from './integrations/external-api/external-api.modu JobsDiscoveryModule, JobAssignmentModule, ReputationOracleModule, - ExternalApiModule, + ExchangeOracleApiModule, CommonConfigModule, OracleDiscoveryModule, StatisticsModule, diff --git a/packages/apps/human-app/server/src/integrations/external-api/external-api.gateway.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts similarity index 95% rename from packages/apps/human-app/server/src/integrations/external-api/external-api.gateway.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts index f6a2f23805..f13f03f4fb 100644 --- a/packages/apps/human-app/server/src/integrations/external-api/external-api.gateway.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts @@ -30,7 +30,7 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; @Injectable() -export class ExternalApiGateway { +export class ExchangeOracleApiGateway { constructor( private httpService: HttpService, @InjectMapper() private mapper: Mapper, @@ -46,7 +46,7 @@ export class ExternalApiGateway { ): Promise { const options: AxiosRequestConfig = { method: 'GET', - url: `${command.oracleUrl}/stats/assignment`, + url: `${command.exchangeOracleUrl}/stats/assignment`, headers: { Authorization: `Bearer ${command.token}`, }, @@ -58,7 +58,7 @@ export class ExternalApiGateway { ): Promise { const options: AxiosRequestConfig = { method: 'GET', - url: `${command.oracleUrl}/stats`, + url: `${command.exchangeOracleUrl}/stats`, }; return this.callExternalHttpUtilRequest(options); } diff --git a/packages/apps/human-app/server/src/integrations/external-api/external-api.mapper.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts similarity index 96% rename from packages/apps/human-app/server/src/integrations/external-api/external-api.mapper.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts index 1bebc5009f..9774fb50e8 100644 --- a/packages/apps/human-app/server/src/integrations/external-api/external-api.mapper.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts @@ -21,7 +21,7 @@ import { } from '../../modules/jobs-discovery/interfaces/jobs-discovery.interface'; @Injectable() -export class ExternalApiProfile extends AutomapperProfile { +export class ExchangeOralceApiProfile extends AutomapperProfile { constructor(@InjectMapper() mapper: Mapper) { super(mapper); } diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts new file mode 100644 index 0000000000..0cb3834433 --- /dev/null +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { HttpModule } from '@nestjs/axios'; +import { ExchangeOracleApiGateway } from './exchange-oracle-api.gateway'; +import { ExchangeOralceApiProfile } from './exchange-oracle-api.mapper'; + +@Module({ + imports: [HttpModule], + providers: [ExchangeOracleApiGateway, ExchangeOralceApiProfile], + exports: [ExchangeOracleApiGateway], +}) +export class ExchangeOracleApiModule {} diff --git a/packages/apps/human-app/server/src/integrations/external-api/spec/external-api.gateway.spec.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts similarity index 87% rename from packages/apps/human-app/server/src/integrations/external-api/spec/external-api.gateway.spec.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts index 6eaa089011..cfa673af1c 100644 --- a/packages/apps/human-app/server/src/integrations/external-api/spec/external-api.gateway.spec.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts @@ -1,9 +1,9 @@ import { Test, TestingModule } from '@nestjs/testing'; import { HttpService } from '@nestjs/axios'; -import { ExternalApiGateway } from '../external-api.gateway'; +import { ExchangeOracleApiGateway } from '../exchange-oracle-api.gateway'; import { oracleStatsCommandFixture, - statisticsOracleUrl, + statisticsExchangeOracleUrl, userStatsCommandFixture, } from '../../../modules/statistics/spec/statistics.fixtures'; import { AutomapperModule } from '@automapper/nestjs'; @@ -17,14 +17,14 @@ import { jobsFetchParamsCommandFixture, jobsFetchParamsDataFixtureAsString, } from '../../../modules/job-assignment/spec/job-assignment.fixtures'; -import { ExternalApiProfile } from '../external-api.mapper'; +import { ExchangeOralceApiProfile } from '../exchange-oracle-api.mapper'; import { jobsDiscoveryParamsCommandFixture, paramsDataFixtureAsString, } from '../../../modules/jobs-discovery/spec/jobs-discovery.fixtures'; -describe('ExternalApiGateway', () => { - let gateway: ExternalApiGateway; +describe('ExchangeOracleApiGateway', () => { + let gateway: ExchangeOracleApiGateway; let httpService: HttpService; beforeEach(async () => { @@ -35,8 +35,8 @@ describe('ExternalApiGateway', () => { }), ], providers: [ - ExternalApiProfile, - ExternalApiGateway, + ExchangeOralceApiProfile, + ExchangeOracleApiGateway, { provide: HttpService, useValue: { @@ -46,7 +46,7 @@ describe('ExternalApiGateway', () => { ], }).compile(); - gateway = module.get(ExternalApiGateway); + gateway = module.get(ExchangeOracleApiGateway); httpService = module.get(HttpService); }); @@ -57,7 +57,7 @@ describe('ExternalApiGateway', () => { describe('fetchUserStatistics', () => { it('should successfully call the requested url for user statistics', async () => { const command = userStatsCommandFixture; - nock(statisticsOracleUrl) + nock(statisticsExchangeOracleUrl) .get('/stats/assignment') .matchHeader('Authorization', `Bearer ${command.token}`) .reply(200); @@ -68,7 +68,7 @@ describe('ExternalApiGateway', () => { describe('fetchOracleStatistics', () => { it('should successfully call the requested url for oracle statistics', async () => { const command = oracleStatsCommandFixture; - nock(statisticsOracleUrl).get('/stats').reply(200); + nock(statisticsExchangeOracleUrl).get('/stats').reply(200); await gateway.fetchOracleStatistics(command); expect(httpService.request).toHaveBeenCalled(); }); diff --git a/packages/apps/human-app/server/src/integrations/external-api/external-api.module.ts b/packages/apps/human-app/server/src/integrations/external-api/external-api.module.ts deleted file mode 100644 index 579148f244..0000000000 --- a/packages/apps/human-app/server/src/integrations/external-api/external-api.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Module } from '@nestjs/common'; -import { HttpModule } from '@nestjs/axios'; -import { ExternalApiGateway } from './external-api.gateway'; -import { ExternalApiProfile } from './external-api.mapper'; - -@Module({ - imports: [HttpModule], - providers: [ExternalApiGateway, ExternalApiProfile], - exports: [ExternalApiGateway], -}) -export class ExternalApiModule {} diff --git a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts index 4710912784..9845e6f87b 100644 --- a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts +++ b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts @@ -1,10 +1,10 @@ import { JobAssignmentService } from './job-assignment.service'; import { JobAssignmentProfile } from './job-assignment.mapper'; import { Module } from '@nestjs/common'; -import { ExternalApiModule } from '../../integrations/external-api/external-api.module'; +import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; @Module({ - imports: [ExternalApiModule], + imports: [ExchangeOracleApiModule], providers: [JobAssignmentService, JobAssignmentProfile], exports: [JobAssignmentService], }) diff --git a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts index 1acdedf008..940bc19fb7 100644 --- a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts +++ b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts @@ -5,10 +5,10 @@ import { JobAssignmentCommand, JobsFetchResponse, } from './interfaces/job-assignment.interface'; -import { ExternalApiGateway } from '../../integrations/external-api/external-api.gateway'; +import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; @Injectable() export class JobAssignmentService { - constructor(private readonly externalApiGateway: ExternalApiGateway) {} + constructor(private readonly externalApiGateway: ExchangeOracleApiGateway) {} async processJobAssignment( command: JobAssignmentCommand, diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.controller.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.controller.ts index 05a8e7875e..21625beef5 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.controller.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.controller.ts @@ -17,12 +17,12 @@ export class JobsDiscoveryController { ) {} @ApiTags('Jobs-Discovery') - @Get('/discovery/jobs') + @Get('/jobs') @ApiOperation({ summary: 'Retrieve a list of filtered available jobs for passed Exchange Oracle url', }) - public async discoverJobs( + public async getJobs( @Query() jobsDiscoveryParamsDto: JobsDiscoveryParamsDto, @Headers('authorization') token: string, ): Promise { diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts index 3d1ab5f4fc..0baf64cd7a 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts @@ -1,10 +1,10 @@ import { JobsDiscoveryService } from './jobs-discovery.service'; import { JobsDiscoveryProfile } from './jobs-discovery.mapper'; import { Module } from '@nestjs/common'; -import { ExternalApiModule } from '../../integrations/external-api/external-api.module'; +import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; @Module({ - imports: [ExternalApiModule], + imports: [ExchangeOracleApiModule], providers: [JobsDiscoveryService, JobsDiscoveryProfile], exports: [JobsDiscoveryService], }) diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts index 52d158bc13..956cd77927 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts @@ -3,10 +3,10 @@ import { JobsDiscoveryParamsCommand, JobsDiscoveryResponse, } from './interfaces/jobs-discovery.interface'; -import { ExternalApiGateway } from '../../integrations/external-api/external-api.gateway'; +import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; @Injectable() export class JobsDiscoveryService { - constructor(private readonly externalApiGateway: ExternalApiGateway) {} + constructor(private readonly externalApiGateway: ExchangeOracleApiGateway) {} async processJobsDiscovery( command: JobsDiscoveryParamsCommand, diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/spec/jobs-discovery.controller.spec.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/spec/jobs-discovery.controller.spec.ts index ec638c300f..1895760a4a 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/spec/jobs-discovery.controller.spec.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/spec/jobs-discovery.controller.spec.ts @@ -57,7 +57,7 @@ describe('JobsDiscoveryController', () => { it('should call service processJobsDiscovery method with proper fields set', async () => { const dto = dtoFixture; const command = jobsDiscoveryParamsCommandFixture; - await controller.discoverJobs(dto, jobDiscoveryToken); + await controller.getJobs(dto, jobDiscoveryToken); expect(jobsDiscoveryService.processJobsDiscovery).toHaveBeenCalledWith( command, ); diff --git a/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.controller.ts b/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.controller.ts index c682105af5..22f0c13d83 100644 --- a/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.controller.ts +++ b/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.controller.ts @@ -22,10 +22,10 @@ export class OracleDiscoveryController { @InjectMapper() private readonly mapper: Mapper, ) {} @ApiTags('Oracle-Discovery') - @Get('/discovery/oracles') + @Get('/oracles') @ApiOperation({ summary: 'Oracles discovery' }) @UsePipes(new ValidationPipe()) - public oracleDiscovery( + public getOracles( @Body() oracleDiscoveryDto: OracleDiscoveryDto, ): Promise { const oracleDiscoveryCommand = this.mapper.map( diff --git a/packages/apps/human-app/server/src/modules/oracle-discovery/spec/oracle-discovery.controller.spec.ts b/packages/apps/human-app/server/src/modules/oracle-discovery/spec/oracle-discovery.controller.spec.ts index 0e86b9276e..468e6c7ab7 100644 --- a/packages/apps/human-app/server/src/modules/oracle-discovery/spec/oracle-discovery.controller.spec.ts +++ b/packages/apps/human-app/server/src/modules/oracle-discovery/spec/oracle-discovery.controller.spec.ts @@ -49,7 +49,7 @@ describe('OracleDiscoveryController', () => { role: 'Exchange Oracle', }; const result: OracleDiscoveryResponse[] = - await controller.oracleDiscovery(dto); + await controller.getOracles(dto); const expectedCommand = { chainId: ChainId.POLYGON_MUMBAI, address: dto.address, diff --git a/packages/apps/human-app/server/src/modules/statistics/interfaces/oracle-statistics.interface.ts b/packages/apps/human-app/server/src/modules/statistics/interfaces/oracle-statistics.interface.ts index 7c43877ade..661f0fabee 100644 --- a/packages/apps/human-app/server/src/modules/statistics/interfaces/oracle-statistics.interface.ts +++ b/packages/apps/human-app/server/src/modules/statistics/interfaces/oracle-statistics.interface.ts @@ -8,5 +8,5 @@ export class OracleStatisticsResponse { assignments_expired: number; } export class OracleStatisticsCommand { - oracleUrl: string; + exchangeOracleUrl: string; } diff --git a/packages/apps/human-app/server/src/modules/statistics/interfaces/user-statistics.interface.ts b/packages/apps/human-app/server/src/modules/statistics/interfaces/user-statistics.interface.ts index 2c658d01ff..cf42384d1a 100644 --- a/packages/apps/human-app/server/src/modules/statistics/interfaces/user-statistics.interface.ts +++ b/packages/apps/human-app/server/src/modules/statistics/interfaces/user-statistics.interface.ts @@ -6,6 +6,6 @@ export class UserStatisticsResponse { assignments_expired: number; } export class UserStatisticsCommand { - oracleUrl: string; + exchangeOracleUrl: string; token: string; } diff --git a/packages/apps/human-app/server/src/modules/statistics/spec/statistics.fixtures.ts b/packages/apps/human-app/server/src/modules/statistics/spec/statistics.fixtures.ts index e72636cab2..f4001c450e 100644 --- a/packages/apps/human-app/server/src/modules/statistics/spec/statistics.fixtures.ts +++ b/packages/apps/human-app/server/src/modules/statistics/spec/statistics.fixtures.ts @@ -20,10 +20,10 @@ const WORKERS_AMOUNT = 3409; const ASSIGNMENTS_COMPLETED_ORACLE = 154363; const ASSIGNMENTS_REJECTED_ORACLE = 231; const ASSIGNMENTS_EXPIRED_ORACLE = 434; -const ORACLE_URL = 'https://test.oracle.com'; +const EXCHANGE_ORACLE_URL = 'https://test.oracle.com'; const TOKEN = 'test-token'; export const statisticsToken = TOKEN; -export const statisticsOracleUrl = ORACLE_URL; +export const statisticsExchangeOracleUrl = EXCHANGE_ORACLE_URL; export const userStatsResponseFixture: UserStatisticsResponse = { assignments_amount: ASSIGNMENTS_AMOUNT, submissions_sent: SUBMISSIONS_SENT, @@ -43,19 +43,19 @@ export const oracleStatsResponseFixture: OracleStatisticsResponse = { }; export const userStatsCommandFixture: UserStatisticsCommand = { - oracleUrl: ORACLE_URL, + exchangeOracleUrl: EXCHANGE_ORACLE_URL, token: TOKEN, }; export const oracleStatsCommandFixture: OracleStatisticsCommand = { - oracleUrl: ORACLE_URL, + exchangeOracleUrl: EXCHANGE_ORACLE_URL, }; export const requestContextFixture = { token: TOKEN, }; export const userStatsOptionsFixture: AxiosRequestConfig = { method: 'GET', - url: `${ORACLE_URL}/stats/assignment`, + url: `${EXCHANGE_ORACLE_URL}/stats/assignment`, headers: { Authorization: `Bearer ${TOKEN}`, }, @@ -63,5 +63,5 @@ export const userStatsOptionsFixture: AxiosRequestConfig = { export const oracleStatsOptionsFixture: AxiosRequestConfig = { method: 'GET', - url: `${ORACLE_URL}/stats`, + url: `${EXCHANGE_ORACLE_URL}/stats`, }; diff --git a/packages/apps/human-app/server/src/modules/statistics/spec/statistisc.controller.spec.ts b/packages/apps/human-app/server/src/modules/statistics/spec/statistisc.controller.spec.ts index cbd9342e78..2e9794ee09 100644 --- a/packages/apps/human-app/server/src/modules/statistics/spec/statistisc.controller.spec.ts +++ b/packages/apps/human-app/server/src/modules/statistics/spec/statistisc.controller.spec.ts @@ -4,7 +4,10 @@ import { StatisticsService } from '../statistics.service'; import { statisticsServiceMock } from './statistics.service.mock'; import { oracleStatsCommandFixture, - oracleStatsResponseFixture, statisticsOracleUrl, statisticsToken, userStatsCommandFixture, + oracleStatsResponseFixture, + statisticsExchangeOracleUrl, + statisticsToken, + userStatsCommandFixture, userStatsResponseFixture, } from './statistics.fixtures'; @@ -32,21 +35,27 @@ describe('StatisticsController', () => { describe('getOracleStatistics', () => { it('should call getOracleStats service method with correct parameters', async () => { - const oracleUrl = statisticsOracleUrl; - const result = await controller.getOracleStatistics(oracleUrl); + const result = await controller.getOracleStatistics( + statisticsExchangeOracleUrl, + ); - expect(statisticsServiceMock.getOracleStats).toHaveBeenCalledWith(oracleStatsCommandFixture); + expect(statisticsServiceMock.getOracleStats).toHaveBeenCalledWith( + oracleStatsCommandFixture, + ); expect(result).toEqual(oracleStatsResponseFixture); }); }); describe('getUserStatistics', () => { it('should call getUserStats service method with correct parameters', async () => { - const oracleUrl = statisticsOracleUrl; - const token = statisticsToken; - const result = await controller.getUserStatistics(oracleUrl, token); - - expect(statisticsServiceMock.getUserStats).toHaveBeenCalledWith(userStatsCommandFixture); + const result = await controller.getUserStatistics( + statisticsExchangeOracleUrl, + statisticsToken, + ); + + expect(statisticsServiceMock.getUserStats).toHaveBeenCalledWith( + userStatsCommandFixture, + ); expect(result).toEqual(userStatsResponseFixture); }); }); diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.controller.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.controller.ts index b76ec63fac..454f595c03 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.controller.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.controller.ts @@ -27,7 +27,7 @@ export class StatisticsController { public getOracleStatistics( @Param('url') oracleUrl: string, ): Promise { - const command = { oracleUrl: oracleUrl } as OracleStatisticsCommand; + const command = { exchangeOracleUrl: oracleUrl } as OracleStatisticsCommand; return this.service.getOracleStats(command); } @@ -40,7 +40,7 @@ export class StatisticsController { @Headers('authorization') token: string, ): Promise { const command: UserStatisticsCommand = { - oracleUrl: oracleUrl, + exchangeOracleUrl: oracleUrl, token: token, } as UserStatisticsCommand; return this.service.getUserStats(command); diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts index 7792a4a3c1..9f4d76e416 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts @@ -1,9 +1,9 @@ import { StatisticsService } from './statistics.service'; import { Module } from '@nestjs/common'; -import { ExternalApiModule } from '../../integrations/external-api/external-api.module'; +import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; @Module({ - imports: [ExternalApiModule], + imports: [ExchangeOracleApiModule], providers: [StatisticsService], exports: [StatisticsService], }) diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts index 0720d9908f..f7f01c293a 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts @@ -10,19 +10,19 @@ import { import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { EnvironmentConfigService } from '../../common/config/environment-config.service'; -import { ExternalApiGateway } from '../../integrations/external-api/external-api.gateway'; +import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; @Injectable() export class StatisticsService { constructor( @Inject(CACHE_MANAGER) private cacheManager: Cache, - private externalApiGateway: ExternalApiGateway, + private externalApiGateway: ExchangeOracleApiGateway, private configService: EnvironmentConfigService, ) {} async getOracleStats( command: OracleStatisticsCommand, ): Promise { - const url = command.oracleUrl; + const url = command.exchangeOracleUrl; const cachedStatistics: OracleStatisticsResponse | undefined = await this.cacheManager.get(url); if (cachedStatistics) { @@ -40,7 +40,7 @@ export class StatisticsService { async getUserStats( command: UserStatisticsCommand, ): Promise { - const userCacheKey = command.oracleUrl + command.token; + const userCacheKey = command.exchangeOracleUrl + command.token; const cachedStatistics: UserStatisticsResponse | undefined = await this.cacheManager.get(userCacheKey); if (cachedStatistics) { From a58546134eab5a4c511365bd1e6461bb23a95dea Mon Sep 17 00:00:00 2001 From: "maciek.nabialek" Date: Thu, 14 Mar 2024 17:09:56 +0100 Subject: [PATCH 2/5] Alignment of naming convention and endpoint structure --- .../oracle-discovery.serivce.ts | 32 ++++++------------- 1 file changed, 10 insertions(+), 22 deletions(-) diff --git a/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.serivce.ts b/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.serivce.ts index c593a921e8..ea626f7588 100644 --- a/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.serivce.ts +++ b/packages/apps/human-app/server/src/modules/oracle-discovery/oracle-discovery.serivce.ts @@ -20,29 +20,17 @@ export class OracleDiscoveryService { let data: OracleDiscoveryResponse[] | undefined = await this.cacheManager.get(command.address); if (!data) { - data = await this.getOperatorsForOracleDiscovery(command); - await this.setOperatorsForAddress(command.address, data); + data = await OperatorUtils.getReputationNetworkOperators( + command.chainId, + command.address, + command.role, + ); + await this.cacheManager.set( + command.address, + data, + this.configService.cacheTtlOracleDiscovery, + ); } return data; } - - async getOperatorsForOracleDiscovery( - cmd: OracleDiscoveryCommand, - ): Promise { - return OperatorUtils.getReputationNetworkOperators( - cmd.chainId, - cmd.address, - cmd.role, - ); - } - async setOperatorsForAddress( - address: string, - operators: OracleDiscoveryResponse[], - ): Promise { - return this.cacheManager.set( - address, - operators, - this.configService.cacheTtlOracleDiscovery, - ); - } } From ec641924d85ebc303d65c428e4f745f4afe1c76e Mon Sep 17 00:00:00 2001 From: "maciek.nabialek" Date: Fri, 15 Mar 2024 11:24:33 +0100 Subject: [PATCH 3/5] ExchangeOracleApi->ExchangeOracle --- packages/apps/human-app/server/src/app.module.ts | 4 ++-- .../exchange-oracle-api.module.ts | 11 ----------- .../exchange-oracle.gateway.ts} | 2 +- .../exchange-oracle.mapper.ts} | 2 +- .../exchange-oracle/exchange-oracle.module.ts | 11 +++++++++++ .../spec/exchange-oracle-api.gateway.spec.ts | 12 ++++++------ .../modules/job-assignment/job-assignment.module.ts | 4 ++-- .../modules/job-assignment/job-assignment.service.ts | 4 ++-- .../modules/jobs-discovery/jobs-discovery.module.ts | 4 ++-- .../modules/jobs-discovery/jobs-discovery.service.ts | 4 ++-- .../src/modules/statistics/statistics.module.ts | 4 ++-- .../src/modules/statistics/statistics.service.ts | 4 ++-- 12 files changed, 33 insertions(+), 33 deletions(-) delete mode 100644 packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts rename packages/apps/human-app/server/src/integrations/{exchange-oracle-api/exchange-oracle-api.gateway.ts => exchange-oracle/exchange-oracle.gateway.ts} (98%) rename packages/apps/human-app/server/src/integrations/{exchange-oracle-api/exchange-oracle-api.mapper.ts => exchange-oracle/exchange-oracle.mapper.ts} (96%) create mode 100644 packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.module.ts rename packages/apps/human-app/server/src/integrations/{exchange-oracle-api => exchange-oracle}/spec/exchange-oracle-api.gateway.spec.ts (91%) diff --git a/packages/apps/human-app/server/src/app.module.ts b/packages/apps/human-app/server/src/app.module.ts index 5d662159d3..9b40f42a28 100644 --- a/packages/apps/human-app/server/src/app.module.ts +++ b/packages/apps/human-app/server/src/app.module.ts @@ -21,7 +21,7 @@ import { JobAssignmentController } from './modules/job-assignment/job-assignment import { JobAssignmentModule } from './modules/job-assignment/job-assignment.module'; import { StatisticsModule } from './modules/statistics/statistics.module'; import { StatisticsController } from './modules/statistics/statistics.controller'; -import { ExchangeOracleApiModule } from './integrations/exchange-oracle-api/exchange-oracle-api.module'; +import { ExchangeOracleModule } from './integrations/exchange-oracle/exchange-oracle.module'; @Module({ imports: [ @@ -40,7 +40,7 @@ import { ExchangeOracleApiModule } from './integrations/exchange-oracle-api/exch JobsDiscoveryModule, JobAssignmentModule, ReputationOracleModule, - ExchangeOracleApiModule, + ExchangeOracleModule, CommonConfigModule, OracleDiscoveryModule, StatisticsModule, diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts deleted file mode 100644 index 0cb3834433..0000000000 --- a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.module.ts +++ /dev/null @@ -1,11 +0,0 @@ -import { Module } from '@nestjs/common'; -import { HttpModule } from '@nestjs/axios'; -import { ExchangeOracleApiGateway } from './exchange-oracle-api.gateway'; -import { ExchangeOralceApiProfile } from './exchange-oracle-api.mapper'; - -@Module({ - imports: [HttpModule], - providers: [ExchangeOracleApiGateway, ExchangeOralceApiProfile], - exports: [ExchangeOracleApiGateway], -}) -export class ExchangeOracleApiModule {} diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.gateway.ts similarity index 98% rename from packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.gateway.ts index f13f03f4fb..a951813fc7 100644 --- a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.gateway.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.gateway.ts @@ -30,7 +30,7 @@ import { Mapper } from '@automapper/core'; import { InjectMapper } from '@automapper/nestjs'; @Injectable() -export class ExchangeOracleApiGateway { +export class ExchangeOracleGateway { constructor( private httpService: HttpService, @InjectMapper() private mapper: Mapper, diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.mapper.ts similarity index 96% rename from packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.mapper.ts index 9774fb50e8..4ff969693a 100644 --- a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/exchange-oracle-api.mapper.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.mapper.ts @@ -21,7 +21,7 @@ import { } from '../../modules/jobs-discovery/interfaces/jobs-discovery.interface'; @Injectable() -export class ExchangeOralceApiProfile extends AutomapperProfile { +export class ExchangeOracleProfile extends AutomapperProfile { constructor(@InjectMapper() mapper: Mapper) { super(mapper); } diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.module.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.module.ts new file mode 100644 index 0000000000..890249e7c1 --- /dev/null +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/exchange-oracle.module.ts @@ -0,0 +1,11 @@ +import { Module } from '@nestjs/common'; +import { HttpModule } from '@nestjs/axios'; +import { ExchangeOracleGateway } from './exchange-oracle.gateway'; +import { ExchangeOracleProfile } from './exchange-oracle.mapper'; + +@Module({ + imports: [HttpModule], + providers: [ExchangeOracleGateway, ExchangeOracleProfile], + exports: [ExchangeOracleGateway], +}) +export class ExchangeOracleModule {} diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts similarity index 91% rename from packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts index cfa673af1c..c5fa3b28cf 100644 --- a/packages/apps/human-app/server/src/integrations/exchange-oracle-api/spec/exchange-oracle-api.gateway.spec.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts @@ -1,6 +1,6 @@ import { Test, TestingModule } from '@nestjs/testing'; import { HttpService } from '@nestjs/axios'; -import { ExchangeOracleApiGateway } from '../exchange-oracle-api.gateway'; +import { ExchangeOracleGateway } from '../exchange-oracle.gateway'; import { oracleStatsCommandFixture, statisticsExchangeOracleUrl, @@ -17,14 +17,14 @@ import { jobsFetchParamsCommandFixture, jobsFetchParamsDataFixtureAsString, } from '../../../modules/job-assignment/spec/job-assignment.fixtures'; -import { ExchangeOralceApiProfile } from '../exchange-oracle-api.mapper'; +import { ExchangeOracleProfile } from '../exchange-oracle.mapper'; import { jobsDiscoveryParamsCommandFixture, paramsDataFixtureAsString, } from '../../../modules/jobs-discovery/spec/jobs-discovery.fixtures'; describe('ExchangeOracleApiGateway', () => { - let gateway: ExchangeOracleApiGateway; + let gateway: ExchangeOracleGateway; let httpService: HttpService; beforeEach(async () => { @@ -35,8 +35,8 @@ describe('ExchangeOracleApiGateway', () => { }), ], providers: [ - ExchangeOralceApiProfile, - ExchangeOracleApiGateway, + ExchangeOracleProfile, + ExchangeOracleGateway, { provide: HttpService, useValue: { @@ -46,7 +46,7 @@ describe('ExchangeOracleApiGateway', () => { ], }).compile(); - gateway = module.get(ExchangeOracleApiGateway); + gateway = module.get(ExchangeOracleGateway); httpService = module.get(HttpService); }); diff --git a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts index 9845e6f87b..a7c30f78f0 100644 --- a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts +++ b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.module.ts @@ -1,10 +1,10 @@ import { JobAssignmentService } from './job-assignment.service'; import { JobAssignmentProfile } from './job-assignment.mapper'; import { Module } from '@nestjs/common'; -import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; +import { ExchangeOracleModule } from '../../integrations/exchange-oracle/exchange-oracle.module'; @Module({ - imports: [ExchangeOracleApiModule], + imports: [ExchangeOracleModule], providers: [JobAssignmentService, JobAssignmentProfile], exports: [JobAssignmentService], }) diff --git a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts index 940bc19fb7..7cc3fc9bf3 100644 --- a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts +++ b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts @@ -5,10 +5,10 @@ import { JobAssignmentCommand, JobsFetchResponse, } from './interfaces/job-assignment.interface'; -import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; +import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway'; @Injectable() export class JobAssignmentService { - constructor(private readonly externalApiGateway: ExchangeOracleApiGateway) {} + constructor(private readonly externalApiGateway: ExchangeOracleGateway) {} async processJobAssignment( command: JobAssignmentCommand, diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts index 0baf64cd7a..d4ad3e6140 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.module.ts @@ -1,10 +1,10 @@ import { JobsDiscoveryService } from './jobs-discovery.service'; import { JobsDiscoveryProfile } from './jobs-discovery.mapper'; import { Module } from '@nestjs/common'; -import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; +import { ExchangeOracleModule } from '../../integrations/exchange-oracle/exchange-oracle.module'; @Module({ - imports: [ExchangeOracleApiModule], + imports: [ExchangeOracleModule], providers: [JobsDiscoveryService, JobsDiscoveryProfile], exports: [JobsDiscoveryService], }) diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts index 956cd77927..b35f1f644f 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts @@ -3,10 +3,10 @@ import { JobsDiscoveryParamsCommand, JobsDiscoveryResponse, } from './interfaces/jobs-discovery.interface'; -import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; +import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway'; @Injectable() export class JobsDiscoveryService { - constructor(private readonly externalApiGateway: ExchangeOracleApiGateway) {} + constructor(private readonly externalApiGateway: ExchangeOracleGateway) {} async processJobsDiscovery( command: JobsDiscoveryParamsCommand, diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts index 9f4d76e416..3b4eca4055 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.module.ts @@ -1,9 +1,9 @@ import { StatisticsService } from './statistics.service'; import { Module } from '@nestjs/common'; -import { ExchangeOracleApiModule } from '../../integrations/exchange-oracle-api/exchange-oracle-api.module'; +import { ExchangeOracleModule } from '../../integrations/exchange-oracle/exchange-oracle.module'; @Module({ - imports: [ExchangeOracleApiModule], + imports: [ExchangeOracleModule], providers: [StatisticsService], exports: [StatisticsService], }) diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts index f7f01c293a..0ce38e4e3a 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts @@ -10,13 +10,13 @@ import { import { CACHE_MANAGER } from '@nestjs/cache-manager'; import { Cache } from 'cache-manager'; import { EnvironmentConfigService } from '../../common/config/environment-config.service'; -import { ExchangeOracleApiGateway } from '../../integrations/exchange-oracle-api/exchange-oracle-api.gateway'; +import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway'; @Injectable() export class StatisticsService { constructor( @Inject(CACHE_MANAGER) private cacheManager: Cache, - private externalApiGateway: ExchangeOracleApiGateway, + private externalApiGateway: ExchangeOracleGateway, private configService: EnvironmentConfigService, ) {} async getOracleStats( From 816db94affabf9c1cd15ad5375f4d1ecddf903e3 Mon Sep 17 00:00:00 2001 From: "maciek.nabialek" Date: Fri, 15 Mar 2024 12:18:52 +0100 Subject: [PATCH 4/5] externalApiGateway-> gateway --- .../src/modules/job-assignment/job-assignment.service.ts | 6 +++--- .../src/modules/jobs-discovery/jobs-discovery.service.ts | 4 ++-- .../server/src/modules/statistics/statistics.service.ts | 6 +++--- 3 files changed, 8 insertions(+), 8 deletions(-) diff --git a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts index 7cc3fc9bf3..ab58a014e3 100644 --- a/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts +++ b/packages/apps/human-app/server/src/modules/job-assignment/job-assignment.service.ts @@ -8,17 +8,17 @@ import { import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway'; @Injectable() export class JobAssignmentService { - constructor(private readonly externalApiGateway: ExchangeOracleGateway) {} + constructor(private readonly gateway: ExchangeOracleGateway) {} async processJobAssignment( command: JobAssignmentCommand, ): Promise { - return this.externalApiGateway.postNewJobAssignment(command); + return this.gateway.postNewJobAssignment(command); } async processGetAssignedJobs( command: JobsFetchParamsCommand, ): Promise { - return this.externalApiGateway.fetchAssignedJobs(command); + return this.gateway.fetchAssignedJobs(command); } } diff --git a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts index b35f1f644f..5a5476a9ee 100644 --- a/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts +++ b/packages/apps/human-app/server/src/modules/jobs-discovery/jobs-discovery.service.ts @@ -6,11 +6,11 @@ import { import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchange-oracle.gateway'; @Injectable() export class JobsDiscoveryService { - constructor(private readonly externalApiGateway: ExchangeOracleGateway) {} + constructor(private readonly gateway: ExchangeOracleGateway) {} async processJobsDiscovery( command: JobsDiscoveryParamsCommand, ): Promise { - return this.externalApiGateway.fetchDiscoveredJobs(command); + return this.gateway.fetchDiscoveredJobs(command); } } diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts index 0ce38e4e3a..0589b94531 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts @@ -16,7 +16,7 @@ import { ExchangeOracleGateway } from '../../integrations/exchange-oracle/exchan export class StatisticsService { constructor( @Inject(CACHE_MANAGER) private cacheManager: Cache, - private externalApiGateway: ExchangeOracleGateway, + private gateway: ExchangeOracleGateway, private configService: EnvironmentConfigService, ) {} async getOracleStats( @@ -29,7 +29,7 @@ export class StatisticsService { return cachedStatistics; } const response: OracleStatisticsResponse = - await this.externalApiGateway.fetchOracleStatistics(command); + await this.gateway.fetchOracleStatistics(command); await this.cacheManager.set( url, response, @@ -46,7 +46,7 @@ export class StatisticsService { if (cachedStatistics) { return cachedStatistics; } - const response = this.externalApiGateway.fetchUserStatistics(command); + const response = this.gateway.fetchUserStatistics(command); await this.cacheManager.set( userCacheKey, response, From 06a5358c6128649e78dd24f5f2df48a20484da43 Mon Sep 17 00:00:00 2001 From: "maciek.nabialek" Date: Fri, 15 Mar 2024 13:58:09 +0100 Subject: [PATCH 5/5] Coverage improvement --- .../filter/global-exception.filter.spec.ts | 58 +++++++++ .../spec/exchange-oracle.gateway.mock.ts | 7 ++ ...pec.ts => exchange-oracle.gateway.spec.ts} | 31 ++++- .../spec/statistics.service.spec.ts | 118 ++++++++++++++++++ .../modules/statistics/statistics.service.ts | 2 +- 5 files changed, 214 insertions(+), 2 deletions(-) create mode 100644 packages/apps/human-app/server/src/common/filter/global-exception.filter.spec.ts create mode 100644 packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.mock.ts rename packages/apps/human-app/server/src/integrations/exchange-oracle/spec/{exchange-oracle-api.gateway.spec.ts => exchange-oracle.gateway.spec.ts} (80%) create mode 100644 packages/apps/human-app/server/src/modules/statistics/spec/statistics.service.spec.ts diff --git a/packages/apps/human-app/server/src/common/filter/global-exception.filter.spec.ts b/packages/apps/human-app/server/src/common/filter/global-exception.filter.spec.ts new file mode 100644 index 0000000000..5dfcb113bf --- /dev/null +++ b/packages/apps/human-app/server/src/common/filter/global-exception.filter.spec.ts @@ -0,0 +1,58 @@ +import { HttpException, HttpStatus } from '@nestjs/common'; +import { GlobalExceptionsFilter } from './global-exceptions.filter'; +import { Test, TestingModule } from '@nestjs/testing'; + +describe('GlobalExceptionsFilter', () => { + let filter: GlobalExceptionsFilter; + let mockJson: jest.Mock; + let mockStatus: jest.Mock; + let mockGetResponse: jest.Mock; + let mockHttpArgumentsHost: jest.Mock; + let mockArgumentsHost: any; + + beforeEach(async () => { + jest.clearAllMocks(); + const module: TestingModule = await Test.createTestingModule({ + providers: [GlobalExceptionsFilter], + }).compile(); + filter = module.get(GlobalExceptionsFilter); + mockJson = jest.fn(); + mockStatus = jest.fn().mockImplementation(() => ({ + json: mockJson, + })); + mockGetResponse = jest.fn().mockImplementation(() => ({ + status: mockStatus, + })); + mockHttpArgumentsHost = jest.fn().mockImplementation(() => ({ + getResponse: mockGetResponse, + getRequest: jest.fn(), + })); + + mockArgumentsHost = { + switchToHttp: mockHttpArgumentsHost, + getArgByIndex: jest.fn(), + getArgs: jest.fn(), + getType: jest.fn(), + switchToRpc: jest.fn(), + switchToWs: jest.fn(), + }; + }); + it('should be defined', () => { + expect(filter).toBeDefined(); + }); + + it('should handle HttpException', () => { + filter.catch( + new HttpException('Http exception', HttpStatus.BAD_REQUEST), + mockArgumentsHost, + ); + expect(mockHttpArgumentsHost).toBeCalledTimes(1); + expect(mockHttpArgumentsHost).toBeCalledWith(); + expect(mockGetResponse).toBeCalledTimes(1); + expect(mockGetResponse).toBeCalledWith(); + expect(mockStatus).toBeCalledTimes(1); + expect(mockStatus).toBeCalledWith(HttpStatus.BAD_REQUEST); + expect(mockJson).toBeCalledTimes(1); + expect(mockJson).toBeCalledWith('Http exception'); + }); +}); diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.mock.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.mock.ts new file mode 100644 index 0000000000..2f25a37d5e --- /dev/null +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.mock.ts @@ -0,0 +1,7 @@ +export const exchangeOracleGatewayMock = { + fetchOracleStatistics: jest.fn(), + fetchUserStatistics: jest.fn(), + fetchAssignedJobs: jest.fn(), + postNewJobAssignment: jest.fn(), + fetchDiscoveredJobs: jest.fn(), +}; diff --git a/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.spec.ts similarity index 80% rename from packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts rename to packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.spec.ts index c5fa3b28cf..da06f86b3e 100644 --- a/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle-api.gateway.spec.ts +++ b/packages/apps/human-app/server/src/integrations/exchange-oracle/spec/exchange-oracle.gateway.spec.ts @@ -9,7 +9,7 @@ import { import { AutomapperModule } from '@automapper/nestjs'; import { classes } from '@automapper/classes'; import nock, { RequestBodyMatcher } from 'nock'; -import { of } from 'rxjs'; +import { of, throwError } from 'rxjs'; import { jobAssignmentCommandFixture, jobAssignmentDataFixture, @@ -22,6 +22,7 @@ import { jobsDiscoveryParamsCommandFixture, paramsDataFixtureAsString, } from '../../../modules/jobs-discovery/spec/jobs-discovery.fixtures'; +import { GoneException, HttpException } from '@nestjs/common'; describe('ExchangeOracleApiGateway', () => { let gateway: ExchangeOracleGateway; @@ -64,6 +65,21 @@ describe('ExchangeOracleApiGateway', () => { await gateway.fetchUserStatistics(command); expect(httpService.request).toHaveBeenCalled(); }); + it('should handle errors on fetchUserStatistics', async () => { + const command = { + exchangeOracleUrl: 'https://example.com', + token: 'dummyToken', + }; + jest + .spyOn(httpService, 'request') + .mockReturnValue( + throwError(() => new HttpException('Service Unavailable', 503)), + ); + + await expect(gateway.fetchUserStatistics(command)).rejects.toThrow( + HttpException, + ); + }); }); describe('fetchOracleStatistics', () => { it('should successfully call the requested url for oracle statistics', async () => { @@ -72,6 +88,19 @@ describe('ExchangeOracleApiGateway', () => { await gateway.fetchOracleStatistics(command); expect(httpService.request).toHaveBeenCalled(); }); + it('should handle errors on fetchOracleStatistics', async () => { + const command = { + exchangeOracleUrl: 'https://example.com', + token: 'dummyToken', + }; + jest + .spyOn(httpService, 'request') + .mockReturnValue(throwError(() => new GoneException())); + + await expect(gateway.fetchUserStatistics(command)).rejects.toThrow( + GoneException, + ); + }); }); describe('fetchAssignedJobs', () => { it('should successfully call get assigned jobs', async () => { diff --git a/packages/apps/human-app/server/src/modules/statistics/spec/statistics.service.spec.ts b/packages/apps/human-app/server/src/modules/statistics/spec/statistics.service.spec.ts new file mode 100644 index 0000000000..90e13341ec --- /dev/null +++ b/packages/apps/human-app/server/src/modules/statistics/spec/statistics.service.spec.ts @@ -0,0 +1,118 @@ +import { Test, TestingModule } from '@nestjs/testing'; +import { CACHE_MANAGER } from '@nestjs/cache-manager'; +import { exchangeOracleGatewayMock } from '../../../integrations/exchange-oracle/spec/exchange-oracle.gateway.mock'; +import { StatisticsService } from '../statistics.service'; +import { ExchangeOracleGateway } from '../../../integrations/exchange-oracle/exchange-oracle.gateway'; +import { EnvironmentConfigService } from '../../../common/config/environment-config.service'; +import { Cache } from 'cache-manager'; + +describe('StatisticsService', () => { + let service: StatisticsService; + let cacheManager: Cache & { get: jest.Mock; set: jest.Mock }; // Explicitly type as jest.Mock + let gateway: ExchangeOracleGateway & { + fetchOracleStatistics: jest.Mock; + fetchUserStatistics: jest.Mock; + }; // Explicitly type as jest.Mock + let configService: EnvironmentConfigService; + beforeEach(async () => { + // Mock the dependencies + const cacheManagerMock = { + get: jest.fn(), + set: jest.fn(), + }; + const configServiceMock = { + cacheTtlOracleStats: 300, + cacheTtlUserStats: 300, + }; + + const module: TestingModule = await Test.createTestingModule({ + providers: [ + StatisticsService, + { provide: CACHE_MANAGER, useValue: cacheManagerMock }, + { provide: ExchangeOracleGateway, useValue: exchangeOracleGatewayMock }, + { provide: EnvironmentConfigService, useValue: configServiceMock }, + ], + }).compile(); + + service = module.get(StatisticsService); + cacheManager = module.get(CACHE_MANAGER); + gateway = module.get(ExchangeOracleGateway); + configService = module.get(EnvironmentConfigService); + }); + + it('should be defined', () => { + expect(service).toBeDefined(); + }); + + describe('getOracleStats', () => { + it('should return cached data if available', async () => { + const cachedData = { some: 'data' }; + cacheManager.get.mockResolvedValue(cachedData); + + const command = { exchangeOracleUrl: 'https://example.com' }; + const result = await service.getOracleStats(command); + + expect(cacheManager.get).toHaveBeenCalledWith(command.exchangeOracleUrl); + expect(result).toEqual(cachedData); + expect(gateway.fetchOracleStatistics).not.toHaveBeenCalled(); + }); + + it('should fetch, cache, and return new data if not in cache', async () => { + const newData = { newData: 'data' }; + cacheManager.get.mockResolvedValue(undefined); + gateway.fetchOracleStatistics.mockResolvedValue(newData); + + const command = { exchangeOracleUrl: 'https://example.com' }; + const result = await service.getOracleStats(command); + + expect(cacheManager.get).toHaveBeenCalledWith(command.exchangeOracleUrl); + expect(gateway.fetchOracleStatistics).toHaveBeenCalledWith(command); + expect(cacheManager.set).toHaveBeenCalledWith( + command.exchangeOracleUrl, + newData, + configService.cacheTtlOracleStats, + ); + expect(result).toEqual(newData); + }); + }); + + describe('getUserStats', () => { + it('should return cached data if available', async () => { + const cachedData = { userData: 'data' }; + const userCacheKey = 'https://example.comtoken'; + cacheManager.get.mockResolvedValue(cachedData); + + const command = { + exchangeOracleUrl: 'https://example.com', + token: 'token', + }; + const result = await service.getUserStats(command); + + expect(cacheManager.get).toHaveBeenCalledWith(userCacheKey); + expect(result).toEqual(cachedData); + expect(gateway.fetchUserStatistics).not.toHaveBeenCalled(); + }); + + it('should fetch, cache, and return new data if not in cache', async () => { + const newData = { newData: 'data' }; + const userCacheKey = 'https://example.comtoken'; + cacheManager.get.mockResolvedValue(undefined); + gateway.fetchUserStatistics.mockResolvedValue(newData); + + const command = { + exchangeOracleUrl: 'https://example.com', + token: 'token', + }; + const result = await service.getUserStats(command); + + expect(cacheManager.get).toHaveBeenCalledWith(userCacheKey); + expect(gateway.fetchUserStatistics).toHaveBeenCalledWith(command); + expect(cacheManager.set).toHaveBeenCalledWith( + userCacheKey, + newData, + configService.cacheTtlUserStats, + ); + expect(result).toEqual(newData); + }); + }); +}); diff --git a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts index 0589b94531..f1e7c77a4a 100644 --- a/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts +++ b/packages/apps/human-app/server/src/modules/statistics/statistics.service.ts @@ -46,7 +46,7 @@ export class StatisticsService { if (cachedStatistics) { return cachedStatistics; } - const response = this.gateway.fetchUserStatistics(command); + const response = await this.gateway.fetchUserStatistics(command); await this.cacheManager.set( userCacheKey, response,