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

[Reputation Oracle] NDA #3132

Open
wants to merge 5 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 4 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
4 changes: 4 additions & 0 deletions packages/apps/human-app/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,8 @@ import { EnvironmentConfigService } from './common/config/environment-config.ser
import { ForbidUnauthorizedHostMiddleware } from './common/middleware/host-check.middleware';
import { HealthModule } from './modules/health/health.module';
import { UiConfigurationModule } from './modules/ui-configuration/ui-configuration.module';
import { NDAModule } from './modules/nda/nda.module';
import { NDAController } from './modules/nda/nda.controller';

const JOI_BOOLEAN_STRING_SCHEMA = Joi.string().valid('true', 'false');

Expand Down Expand Up @@ -125,6 +127,7 @@ const JOI_BOOLEAN_STRING_SCHEMA = Joi.string().valid('true', 'false');
CronJobModule,
HealthModule,
UiConfigurationModule,
NDAModule,
],
controllers: [
AppController,
Expand All @@ -137,6 +140,7 @@ const JOI_BOOLEAN_STRING_SCHEMA = Joi.string().valid('true', 'false');
HCaptchaController,
RegisterAddressController,
TokenRefreshController,
NDAController,
],
exports: [HttpModule],
providers: [EnvironmentConfigService],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -110,6 +110,16 @@ export class GatewayConfigService {
method: HttpMethod.GET,
headers: this.JSON_HEADER,
},
[ReputationOracleEndpoints.GET_LATEST_NDA]: {
endpoint: '/nda/latest',
method: HttpMethod.GET,
headers: this.JSON_HEADER,
},
[ReputationOracleEndpoints.SIGN_NDA]: {
endpoint: '/nda/sign',
method: HttpMethod.POST,
headers: this.JSON_HEADER,
},
} as Record<ReputationOracleEndpoints, GatewayEndpointConfig>,
},
[ExternalApiName.HCAPTCHA_LABELING_STATS]: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ export enum ReputationOracleEndpoints {
KYC_ON_CHAIN = 'kyc_on_chain',
REGISTRATION_IN_EXCHANGE_ORACLE = 'registration_in_exchange_oracle',
GET_REGISTRATION_IN_EXCHANGE_ORACLES = 'get_registration_in_exchange_oracles',
GET_LATEST_NDA = 'GET_LATEST_NDA',
SIGN_NDA = 'SIGN_NDA',
}
export enum HCaptchaLabelingStatsEndpoints {
USER_STATS = 'user_stats',
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,13 @@ import {
SigninOperatorData,
SigninOperatorResponse,
} from '../../modules/user-operator/model/operator-signin.model';
import {
GetNDACommand,
GetNDAResponse,
SignNDACommand,
SignNDAData,
SignNDAResponse,
} from '../../modules/nda/model/nda.model';

@Injectable()
export class ReputationOracleGateway {
Expand Down Expand Up @@ -329,6 +336,25 @@ export class ReputationOracleGateway {
return this.handleRequestToReputationOracle<TokenRefreshResponse>(options);
}

async getLatestNDA(command: GetNDACommand) {
const options = this.getEndpointOptions(
ReputationOracleEndpoints.GET_LATEST_NDA,
undefined,
command.token,
);
return this.handleRequestToReputationOracle<GetNDAResponse>(options);
}

async sendSignedNDA(command: SignNDACommand) {
const data = this.mapper.map(command, SignNDACommand, SignNDAData);
const options = this.getEndpointOptions(
ReputationOracleEndpoints.SIGN_NDA,
data,
command.token,
);
return this.handleRequestToReputationOracle<SignNDAResponse>(options);
}

sendKycOnChain(token: string) {
const options = this.getEndpointOptions(
ReputationOracleEndpoints.KYC_ON_CHAIN,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ import {
SigninOperatorCommand,
SigninOperatorData,
} from '../../modules/user-operator/model/operator-signin.model';
import { SignNDACommand, SignNDAData } from '../../modules/nda/model/nda.model';

@Injectable()
export class ReputationOracleProfile extends AutomapperProfile {
Expand Down Expand Up @@ -143,6 +144,15 @@ export class ReputationOracleProfile extends AutomapperProfile {
destination: new SnakeCaseNamingConvention(),
}),
);
createMap(
mapper,
SignNDACommand,
SignNDAData,
namingConventions({
source: new CamelCaseNamingConvention(),
destination: new SnakeCaseNamingConvention(),
}),
);
};
}
}
34 changes: 34 additions & 0 deletions packages/apps/human-app/server/src/modules/nda/model/nda.model.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { AutoMap } from '@automapper/classes';
import { IsString, IsUrl } from 'class-validator';
import { ApiProperty } from '@nestjs/swagger';

export class GetNDACommand {
token: string;
}

export class GetNDAResponse {
url: string;
}

export class SignNDADto {
@AutoMap()
@IsUrl()
@IsString()
@ApiProperty({ example: 'string' })
url: string;
}

export class SignNDACommand {
@AutoMap()
url: string;
token: string;
}

export class SignNDAData {
@AutoMap()
url: string;
}

export class SignNDAResponse {
message: string;
}
56 changes: 56 additions & 0 deletions packages/apps/human-app/server/src/modules/nda/nda.controller.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Mapper } from '@automapper/core';
import { InjectMapper } from '@automapper/nestjs';
import {
Body,
Controller,
Get,
HttpCode,
Post,
UsePipes,
ValidationPipe,
} from '@nestjs/common';
import { ApiBearerAuth, ApiOperation, ApiTags } from '@nestjs/swagger';
import { Authorization } from '../../common/config/params-decorators';
import { GetNDACommand, SignNDACommand, SignNDADto } from './model/nda.model';
import { NDAService } from './nda.service';

@Controller('/nda')
@ApiTags('NDA')
@UsePipes(new ValidationPipe())
@ApiBearerAuth()
export class NDAController {
@InjectMapper() private readonly mapper: Mapper;

constructor(
private readonly ndaService: NDAService,
@InjectMapper() mapper: Mapper,
) {
this.mapper = mapper;
}

@ApiOperation({
summary: 'Retrieves latest NDA URL',
description:
'Retrieves the latest NDA URL that users must sign to join the oracle',
})
@Get('/')
async getLatestNDA(@Authorization() token: string) {
const command = new GetNDACommand();
command.token = token;
return this.ndaService.getLatestNDA(command);
}

@ApiOperation({
summary: 'Sign NDA',
description:
'Signs the NDA with the provided URL. The URL must match the latest NDA URL.',
})
@HttpCode(200)
@Post('sign')
async signNDA(@Body() dto: SignNDADto, @Authorization() token: string) {
const command = this.mapper.map(dto, SignNDADto, SignNDACommand);
command.token = token;
await this.ndaService.signNDA(command);
return { message: 'NDA signed successfully' };
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
import { Injectable } from '@nestjs/common';
import {
CamelCaseNamingConvention,
createMap,
Mapper,
namingConventions,
SnakeCaseNamingConvention,
} from '@automapper/core';
import { AutomapperProfile, InjectMapper } from '@automapper/nestjs';
import { SignNDACommand, SignNDADto } from './model/nda.model';

@Injectable()
export class SignNDAProfile extends AutomapperProfile {
constructor(@InjectMapper() mapper: Mapper) {
super(mapper);
}

override get profile() {
return (mapper: Mapper) => {
createMap(
mapper,
SignNDADto,
SignNDACommand,
namingConventions({
source: new SnakeCaseNamingConvention(),
destination: new CamelCaseNamingConvention(),
}),
);
};
}
}
11 changes: 11 additions & 0 deletions packages/apps/human-app/server/src/modules/nda/nda.module.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { Module } from '@nestjs/common';
import { NDAService } from 'src/modules/nda/nda.service';
import { ReputationOracleModule } from '../../integrations/reputation-oracle/reputation-oracle.module';
import { SignNDAProfile } from './nda.mapper.profile';

@Module({
imports: [ReputationOracleModule],
providers: [NDAService, SignNDAProfile],
exports: [NDAService],
})
export class NDAModule {}
21 changes: 21 additions & 0 deletions packages/apps/human-app/server/src/modules/nda/nda.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
import { Injectable } from '@nestjs/common';
import { ReputationOracleGateway } from '../../integrations/reputation-oracle/reputation-oracle.gateway';
import {
GetNDACommand,
GetNDAResponse,
SignNDACommand,
SignNDAResponse,
} from './model/nda.model';

@Injectable()
export class NDAService {
constructor(private readonly gateway: ReputationOracleGateway) {}

async getLatestNDA(command: GetNDACommand): Promise<GetNDAResponse> {
return this.gateway.getLatestNDA(command);
}

async signNDA(command: SignNDACommand): Promise<SignNDAResponse> {
return await this.gateway.sendSignedNDA(command);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import { Test, TestingModule } from '@nestjs/testing';
import { AutomapperModule } from '@automapper/nestjs';
import { classes } from '@automapper/classes';

import { NDAController } from '../nda.controller';
import { NDAService } from '../nda.service';
import { ndaServiceMock } from './nda.service.mock';
import {
NDA_TOKEN,
signNDACommandFixture,
signNDADtoFixture,
} from './nda.fixtures';
import { SignNDAProfile } from '../nda.mapper.profile';

describe('NDAController', () => {
let controller: NDAController;

beforeEach(async () => {
const module: TestingModule = await Test.createTestingModule({
controllers: [NDAController],
imports: [
AutomapperModule.forRoot({
strategyInitializer: classes(),
}),
],
providers: [NDAService, SignNDAProfile],
})
.overrideProvider(NDAService)
.useValue(ndaServiceMock)
.compile();

controller = module.get<NDAController>(NDAController);
});

it('should be defined', () => {
expect(controller).toBeDefined();
});

describe('nda', () => {
it('should call service signNDA method with proper fields set', async () => {
const dto = signNDADtoFixture;
const command = signNDACommandFixture;
await controller.signNDA(dto, NDA_TOKEN);
expect(ndaServiceMock.signNDA).toHaveBeenCalledWith(command);
});

it('should call service getLatestNDA method with proper fields set', async () => {
const token = NDA_TOKEN;
await controller.getLatestNDA(token);
expect(ndaServiceMock.getLatestNDA).toHaveBeenCalledWith({ token });
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
import {
GetNDACommand,
GetNDAResponse,
SignNDACommand,
SignNDAData,
SignNDADto,
SignNDAResponse,
} from '../model/nda.model';

const URL = 'http://some_url.com';
export const NDA_TOKEN = 'my_access_token';

export const signNDADtoFixture: SignNDADto = {
url: URL,
};
export const signNDACommandFixture: SignNDACommand = {
url: URL,
token: NDA_TOKEN,
};
export const signNDADataFixture: SignNDAData = {
url: URL,
};
export const signNDAResponseFixture: SignNDAResponse = {
message: 'NDA signed successfully',
};

export const getNDACommandFixture: GetNDACommand = {
token: NDA_TOKEN,
};

export const getNDAResponseFixture: GetNDAResponse = {
url: 'URL',
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export const ndaServiceMock = {
getLatestNDA: jest.fn(),
signNDA: jest.fn(),
};
4 changes: 4 additions & 0 deletions packages/apps/reputation-oracle/server/.env.example
Original file line number Diff line number Diff line change
Expand Up @@ -77,3 +77,7 @@ SYNAPS_WEBHOOK_SECRET=
SYNAPS_BASE_URL=
SYNAPS_STEP_DOCUMENT_ID=


# NDA
NDA_URL=

2 changes: 1 addition & 1 deletion packages/apps/reputation-oracle/server/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@
"migration:show": "yarn build && typeorm-ts-node-commonjs migration:show -d typeorm.config.ts",
"docker:db:up": "docker compose up -d postgres && yarn migration:run",
"docker:db:down": "docker compose down postgres",
"setup:local": "ts-node ./test/setup.ts",
"setup:local": "ts-node ./scripts/setup-kv-store.ts",
"lint": "eslint \"{src,apps,libs,test}/**/*.ts\" --fix",
"test": "jest",
"test:watch": "jest --watch",
Expand Down
3 changes: 3 additions & 0 deletions packages/apps/reputation-oracle/server/src/app.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import { WebhookIncomingModule } from './modules/webhook/webhook-incoming.module
import { WebhookOutgoingModule } from './modules/webhook/webhook-outgoing.module';
import { UserModule } from './modules/user/user.module';
import { EmailModule } from './modules/email/module';
import { NDAModule } from './modules/nda/nda.module';
import { StorageModule } from './modules/storage/storage.module';
import Environment from './utils/environment';

Expand Down Expand Up @@ -64,6 +65,8 @@ import Environment from './utils/environment';
AuthModule,
CronJobModule,
EmailModule,
UserModule,
NDAModule,
EscrowCompletionModule,
HealthModule,
KycModule,
Expand Down
Loading