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

refcator: get all schemas api #647

Closed
wants to merge 1 commit into from
Closed
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
16 changes: 16 additions & 0 deletions apps/api-gateway/src/interfaces/ISchemaSearch.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,3 +21,19 @@ export interface W3CSchemaPayload {
type: string,
title: string
}

export interface ILedgers {
id: string;
createDateTime: string;
lastChangedDateTime: string;
name: string;
networkType: string;
poolConfig: string;
isActive: boolean;
networkString: string;
registerDIDEndpoint: string;
registerDIDPayload: string;
indyNamespace: string;
networkUrl: string | null;
}

28 changes: 17 additions & 11 deletions apps/api-gateway/src/platform/platform.controller.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,18 @@
import { Controller, Get, HttpStatus, Logger, Param, Query, Res, UseFilters, UseGuards } from '@nestjs/common';
import { PlatformService } from './platform.service';
import { ApiBearerAuth, ApiExcludeEndpoint, ApiOperation, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiBearerAuth, ApiExcludeEndpoint, ApiOperation, ApiQuery, ApiResponse, ApiTags } from '@nestjs/swagger';
import { ApiResponseDto } from '../dtos/apiResponse.dto';
import { GetAllSchemaByPlatformDto } from '../schema/dtos/get-all-schema.dto';
import { IUserRequestInterface } from '../interfaces/IUserRequestInterface';
import { User } from '../authz/decorators/user.decorator';
import { Response } from 'express';
import { ISchemaSearchPayload } from '../interfaces/ISchemaSearch.interface';
import IResponseType, { IResponse } from '@credebl/common/interfaces/response.interface';
import { IResponse } from '@credebl/common/interfaces/response.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { AuthGuard } from '@nestjs/passport';
import * as QRCode from 'qrcode';
import { SortFields } from 'apps/ledger/src/schema/enum/schema.enum';


@Controller('')
Expand All @@ -27,14 +28,19 @@ export class PlatformController {
summary: 'Get all schemas from platform.',
description: 'Get all schemas from platform.'
})
@ApiQuery({
name: 'sortField',
enum: SortFields,
required: false
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
async getAllSchema(
@Query() getAllSchemaDto: GetAllSchemaByPlatformDto,
@Res() res: Response,
@User() user: IUserRequestInterface
): Promise<object> {
): Promise<Response> {
const { ledgerId, pageSize, searchByText, pageNumber, sorting, sortByValue } = getAllSchemaDto;
const schemaSearchCriteria: ISchemaSearchPayload = {
ledgerId,
Expand All @@ -45,10 +51,10 @@ export class PlatformController {
sortBy: sortByValue
};
const schemasResponse = await this.platformService.getAllSchema(schemaSearchCriteria, user);
const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.schema.success.fetch,
data: schemasResponse.response
data: schemasResponse
};
return res.status(HttpStatus.OK).json(finalResponse);
}
Expand All @@ -67,10 +73,10 @@ export class PlatformController {
): Promise<object> {
const networksResponse = await this.platformService.getAllLedgers();

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.ledger.success.fetch,
data: networksResponse.response
data: networksResponse
};
return res.status(HttpStatus.OK).json(finalResponse);
}
Expand All @@ -87,13 +93,13 @@ export class PlatformController {
async getNetwrkUrl(
@Param('indyNamespace') indyNamespace: string,
@Res() res: Response
): Promise<object> {
): Promise<Response> {
const networksResponse = await this.platformService.getNetworkUrl(indyNamespace);

const finalResponse: IResponseType = {
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.ledger.success.fetch,
data: networksResponse.response
message: ResponseMessages.ledger.success.fetchNetworkUrl,
data: networksResponse
};
return res.status(HttpStatus.OK).json(finalResponse);
}
Expand Down
24 changes: 8 additions & 16 deletions apps/api-gateway/src/platform/platform.service.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { Injectable, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from '../../../../libs/service/base.service';
import { ISchemaSearchPayload } from '../interfaces/ISchemaSearch.interface';
import { ILedgers, ISchemaSearchPayload } from '../interfaces/ISchemaSearch.interface';
import { IUserRequestInterface } from '../interfaces/IUserRequestInterface';
import { INetworkUrl, ISchemaDetails } from '@credebl/common/interfaces/schema.interface';

@Injectable()
export class PlatformService extends BaseService {
Expand All @@ -12,28 +13,19 @@ export class PlatformService extends BaseService {
super('PlatformService');
}

async getAllSchema(schemaSearchCriteria: ISchemaSearchPayload, user: IUserRequestInterface): Promise<{
response: object;
}> {
async getAllSchema(schemaSearchCriteria: ISchemaSearchPayload, user: IUserRequestInterface): Promise<ISchemaDetails> {
const schemaSearch = { schemaSearchCriteria, user };
return this.sendNats(this.platformServiceProxy, 'get-all-schemas', schemaSearch);
return this.sendNatsMessage(this.platformServiceProxy, 'get-all-schemas', schemaSearch);

}

async getAllLedgers(): Promise<{
response: object;
}> {
async getAllLedgers(): Promise<ILedgers> {
const payload = {};
return this.sendNats(this.platformServiceProxy, 'get-all-ledgers', payload);
return this.sendNatsMessage(this.platformServiceProxy, 'get-all-ledgers', payload);
}

async getNetworkUrl(indyNamespace: string): Promise<{
response: object;
}> {
const payload = {
indyNamespace
};
return this.sendNats(this.platformServiceProxy, 'get-network-url', payload);
async getNetworkUrl(indyNamespace: string): Promise<INetworkUrl> {
return this.sendNatsMessage(this.platformServiceProxy, 'get-network-url', indyNamespace);
}

async getShorteningUrlById(referenceId: string): Promise<string> {
Expand Down
25 changes: 18 additions & 7 deletions apps/api-gateway/src/schema/dtos/get-all-schema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { SortValue } from '../../enum';
import { Transform, Type } from 'class-transformer';
import { IsEnum, IsOptional } from 'class-validator';
import { trim } from '@credebl/common/cast.helper';
import { IsEnum, IsOptional, IsUUID, Min } from 'class-validator';
import { toNumber, trim } from '@credebl/common/cast.helper';
import { CredDefSortFields, SortFields } from 'apps/ledger/src/schema/enum/schema.enum';

export class GetAllSchemaDto {
Expand Down Expand Up @@ -73,31 +73,42 @@ export class GetCredentialDefinitionBySchemaIdDto {
orgId: string;
}


export class GetAllSchemaByPlatformDto {

@ApiProperty()
@ApiProperty({ example: '1a7eac11-ff05-40d7-8351-4d7467687cad'})
@ApiPropertyOptional()
@IsOptional()
@IsUUID('4', { message: 'Invalid format of ledgerId' })
ledgerId?: string;

@ApiProperty({ required: false })
@ApiProperty({ required: false, default: 1 })
@IsOptional()
@Transform(({ value }) => toNumber(value))
@Min(1, { message: 'Page number must be greater than 0' })
pageNumber: number = 1;

@ApiProperty({ required: false })
@IsOptional()
@Type(() => String)
searchByText: string = '';

@ApiProperty({ required: false })
@ApiProperty({ required: false, default: 10 })
@IsOptional()
@Transform(({ value }) => toNumber(value))
@Min(1, { message: 'Page size must be greater than 0' })
pageSize: number = 10;

@ApiProperty({ required: false })
@ApiProperty({
required: false
})
@Transform(({ value }) => trim(value))
@IsOptional()
sorting: string = 'id';
@IsEnum(SortFields)
sorting: string = SortFields.CREATED_DATE_TIME;

@ApiProperty({ required: false })
@IsOptional()
sortByValue: string = SortValue.DESC;

}
7 changes: 3 additions & 4 deletions apps/ledger/src/ledger.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { LedgerService } from './ledger.service';
import { MessagePattern } from '@nestjs/microservices';
import { ledgers } from '@prisma/client';
import { LedgerDetails } from './interfaces/ledgers.interface';
import { INetworkUrl } from '@credebl/common/interfaces/schema.interface';

@Controller()
export class LedgerController {
Expand All @@ -14,10 +15,8 @@ export class LedgerController {
}

@MessagePattern({ cmd: 'get-network-url' })
async getNetworkUrl(payload: object): Promise<{
networkUrl: string;
}> {
return this.ledgerService.getNetworkUrl(payload);
async getNetworkUrl(indyNamespace: string): Promise<INetworkUrl> {
return this.ledgerService.getNetworkUrl(indyNamespace);
}

@MessagePattern({ cmd: 'get-network-details-by-id' })
Expand Down
7 changes: 3 additions & 4 deletions apps/ledger/src/ledger.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import { RpcException } from '@nestjs/microservices';
import { ledgers } from '@prisma/client';
import { ResponseMessages } from '@credebl/common/response-messages';
import { LedgerDetails } from './interfaces/ledgers.interface';
import { INetworkUrl } from '@credebl/common/interfaces/schema.interface';

@Injectable()
export class LedgerService extends BaseService {
Expand All @@ -30,11 +31,9 @@ export class LedgerService extends BaseService {
}
}

async getNetworkUrl(payload: object): Promise<{
networkUrl: string;
}> {
async getNetworkUrl(indyNamespace: string): Promise<INetworkUrl> {
try {
const getNetworkUrl = await this.ledgerRepository.getNetworkUrl(payload);
const getNetworkUrl = await this.ledgerRepository.getNetworkUrl(indyNamespace);

if (!getNetworkUrl) {
throw new NotFoundException(ResponseMessages.ledger.error.NotFound);
Expand Down
25 changes: 12 additions & 13 deletions apps/ledger/src/repositories/ledger.repository.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import { PrismaService } from "@credebl/prisma-service";
import { Injectable, Logger } from "@nestjs/common";
import { ledgers } from "@prisma/client";
import { LedgerDetails } from "../interfaces/ledgers.interface";
import { PrismaService } from '@credebl/prisma-service';
import { Injectable, Logger } from '@nestjs/common';
import { ledgers } from '@prisma/client';
import { LedgerDetails } from '../interfaces/ledgers.interface';
import { INetworkUrl } from '@credebl/common/interfaces/schema.interface';


@Injectable()
Expand All @@ -21,18 +22,16 @@ export class LedgerRepository {
}
}

async getNetworkUrl(payload: object): Promise<{
networkUrl: string;
}> {
async getNetworkUrl(indyNamespace: string): Promise<INetworkUrl> {

try {
return this.prisma.ledgers.findFirst({
where: {
indyNamespace: payload["indyNamespace"]
},
select: {
networkUrl: true
}
where: {
indyNamespace
},
select: {
networkUrl: true
}
});
} catch (error) {
this.logger.error(`Error in getNetworkUrl: ${error}`);
Expand Down
20 changes: 4 additions & 16 deletions apps/ledger/src/schema/repositories/schema.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import { ISchema, ISchemaExist, ISchemaSearchCriteria } from '../interfaces/sche
import { ResponseMessages } from '@credebl/common/response-messages';
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
import { SortValue } from '@credebl/enum/enum';
import { ICredDefWithCount } from '@credebl/common/interfaces/schema.interface';
import { ICredDefWithCount, IPlatformSchemas } from '@credebl/common/interfaces/schema.interface';
import { sortValue } from 'apps/api-gateway/src/enum';

@Injectable()
export class SchemaRepository {
Expand Down Expand Up @@ -192,20 +193,7 @@ export class SchemaRepository {
}
}

async getAllSchemaDetails(payload: ISchemaSearchCriteria): Promise<{
schemasCount: number;
schemasResult: {
createDateTime: Date;
createdBy: string;
name: string;
version: string;
attributes: string;
schemaLedgerId: string;
publisherDid: string;
issuerId: string;
orgId: string;
}[];
}> {
async getAllSchemaDetails(payload: ISchemaSearchCriteria): Promise<IPlatformSchemas> {
try {
const schemasResult = await this.prisma.schema.findMany({
where: {
Expand All @@ -229,7 +217,7 @@ export class SchemaRepository {
issuerId: true
},
orderBy: {
[payload.sortField]: 'desc' === payload.sortBy ? 'desc' : 'asc'
[payload.sortField]: sortValue.DESC === payload.sortBy ? sortValue.DESC : sortValue.ASC
},
take: Number(payload.pageSize),
skip: (payload.pageNumber - 1) * payload.pageSize
Expand Down
20 changes: 2 additions & 18 deletions apps/ledger/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import { schema } from '@prisma/client';
import {
ICredDefWithPagination,
ISchemaData,
ISchemaDetails,
ISchemasWithPagination
} from '@credebl/common/interfaces/schema.interface';

Expand Down Expand Up @@ -49,24 +50,7 @@ export class SchemaController {
}

@MessagePattern({ cmd: 'get-all-schemas' })
async getAllSchema(schemaSearch: ISchemaSearchPayload): Promise<{
totalItems: number;
hasNextPage: boolean;
hasPreviousPage: boolean;
nextPage: number;
previousPage: number;
lastPage: number;
data: {
createDateTime: Date;
createdBy: string;
name: string;
schemaLedgerId: string;
version: string;
attributes: string;
publisherDid: string;
issuerId: string;
}[];
}> {
async getAllSchema(schemaSearch: ISchemaSearchPayload): Promise<ISchemaDetails> {
const { schemaSearchCriteria } = schemaSearch;
return this.schemaService.getAllSchema(schemaSearchCriteria);
}
Expand Down
Loading
Loading