Skip to content

Commit

Permalink
feat: fetch all credential definitions from platform (#684)
Browse files Browse the repository at this point in the history
* feat: get all platform cred defs

Signed-off-by: bhavanakarwade <bhavana.karwade@ayanworks.com>

* reafctor: modify get all platform cred defs dto

Signed-off-by: bhavanakarwade <bhavana.karwade@ayanworks.com>

---------

Signed-off-by: bhavanakarwade <bhavana.karwade@ayanworks.com>
Signed-off-by: KulkarniShashank <shashank.kulkarni@ayanworks.com>
  • Loading branch information
bhavanakarwade authored and KulkarniShashank committed Sep 11, 2024
1 parent b9f8154 commit a70296d
Show file tree
Hide file tree
Showing 12 changed files with 192 additions and 26 deletions.
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
import { trim } from '@credebl/common/cast.helper';
import { PaginationDto } from '@credebl/common/dtos/pagination.dto';
import { CredDefSortFields, SortValue } from '@credebl/enum/enum';
import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform } from 'class-transformer';
import { IsEnum, IsOptional, IsUUID } from 'class-validator';

export class GetAllPlatformCredDefsDto extends PaginationDto {

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

@ApiProperty({
required: false
})
@Transform(({ value }) => trim(value))
@IsOptional()
@IsEnum(CredDefSortFields)
sortField: string = CredDefSortFields.CREATED_DATE_TIME;

@ApiProperty({ required: false })
@Transform(({ value }) => trim(value))
@IsOptional()
sortBy: string = SortValue.DESC;

}
33 changes: 32 additions & 1 deletion apps/api-gateway/src/platform/platform.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ 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 '@credebl/enum/enum';
import { CredDefSortFields, SortFields } from '@credebl/enum/enum';
import { GetAllPlatformCredDefsDto } from '../credential-definition/dto/get-all-platform-cred-defs.dto';

@Controller('')
@UseFilters(CustomExceptionFilter)
Expand Down Expand Up @@ -58,6 +59,36 @@ export class PlatformController {
return res.status(HttpStatus.OK).json(finalResponse);
}


@Get('/platform/cred-defs')
@ApiTags('credential-definitions')
@ApiOperation({
summary: 'Get all credential-definitions from platform.',
description: 'Get all credential-definitions list from platform.'
})
@ApiQuery({
name: 'sortField',
enum: CredDefSortFields,
required: false
})
@ApiBearerAuth()
@UseGuards(AuthGuard('jwt'))
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
async getAllCredDefs(
@Query() getAllPlatformCredDefs: GetAllPlatformCredDefsDto,
@Res() res: Response,
@User() user: IUserRequestInterface
): Promise<Response> {
const schemasResponse = await this.platformService.getAllPlatformCredDefs(getAllPlatformCredDefs, user);
const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.credentialDefinition.success.fetch,
data: schemasResponse
};
return res.status(HttpStatus.OK).json(finalResponse);
}


@Get('/platform/ledgers')
@ApiTags('ledgers')
@ApiOperation({
Expand Down
7 changes: 7 additions & 0 deletions apps/api-gateway/src/platform/platform.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import { BaseService } from '../../../../libs/service/base.service';
import { ILedgers, ISchemaSearchPayload } from '../interfaces/ISchemaSearch.interface';
import { IUserRequestInterface } from '../interfaces/IUserRequestInterface';
import { INetworkUrl, ISchemaDetails } from '@credebl/common/interfaces/schema.interface';
import { GetAllPlatformCredDefsDto } from '../credential-definition/dto/get-all-platform-cred-defs.dto';
import { IPlatformCredDefsData } from '@credebl/common/interfaces/cred-def.interface';

@Injectable()
export class PlatformService extends BaseService {
Expand All @@ -19,6 +21,11 @@ export class PlatformService extends BaseService {

}

async getAllPlatformCredDefs(getAllPlatformCredDefs: GetAllPlatformCredDefsDto, user: IUserRequestInterface): Promise<IPlatformCredDefsData> {
const credDefs = { ...getAllPlatformCredDefs, user };
return this.sendNatsMessage(this.platformServiceProxy, 'get-all-platform-cred-defs', credDefs);
}

async getAllLedgers(): Promise<ILedgers> {
const payload = {};
return this.sendNatsMessage(this.platformServiceProxy, 'get-all-ledgers', payload);
Expand Down
3 changes: 1 addition & 2 deletions apps/api-gateway/src/schema/dtos/get-all-schema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@ import { ApiProperty, ApiPropertyOptional } from '@nestjs/swagger';
import { Transform, Type } from 'class-transformer';
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';
import { SortValue } from '@credebl/enum/enum';
import { CredDefSortFields, SortFields, SortValue } from '@credebl/enum/enum';

export class GetAllSchemaDto {
@ApiProperty({ required: false })
Expand Down
2 changes: 1 addition & 1 deletion apps/api-gateway/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { IUserRequestInterface } from './interfaces';
import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
import { CreateSchemaDto, CreateW3CSchemaDto } from '../dtos/create-schema.dto';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { CredDefSortFields, SortFields } from 'apps/ledger/src/schema/enum/schema.enum';
import { CredDefSortFields, SortFields } from '@credebl/enum/enum';

@UseFilters(CustomExceptionFilter)
@Controller('orgs')
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,11 @@ import { Controller, Logger } from '@nestjs/common';

import { CredentialDefinitionService } from './credential-definition.service';
import { MessagePattern } from '@nestjs/microservices';
import { GetAllCredDefsPayload, GetCredDefBySchemaId } from './interfaces/create-credential-definition.interface';
import { GetAllCredDefsPayload, GetCredDefBySchemaId, IPlatformCredDefs } from './interfaces/create-credential-definition.interface';
import { CreateCredDefPayload, GetCredDefPayload } from './interfaces/create-credential-definition.interface';
import { credential_definition } from '@prisma/client';
import { CredDefSchema } from './interfaces/credential-definition.interface';
import { ICredDefDetails } from '@credebl/common/interfaces/cred-def.interface';
import { ICredDefDetails, IPlatformCredDefsData } from '@credebl/common/interfaces/cred-def.interface';
@Controller('credential-definitions')
export class CredentialDefinitionController {
private logger = new Logger();
Expand All @@ -25,6 +25,11 @@ export class CredentialDefinitionController {
return this.credDefService.getCredentialDefinitionById(payload);
}

@MessagePattern({ cmd: 'get-all-platform-cred-defs' })
async getAllSchema(credDefPayload: IPlatformCredDefs): Promise<IPlatformCredDefsData> {
return this.credDefService.getAllPlatformCredDefs(credDefPayload);
}

@MessagePattern({ cmd: 'get-all-credential-definitions' })
async getAllCredDefs(payload: GetAllCredDefsPayload): Promise<ICredDefDetails> {
return this.credDefService.getAllCredDefs(payload);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,15 +9,15 @@ import {
import { ClientProxy, RpcException } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { CredentialDefinitionRepository } from './repositories/credential-definition.repository';
import { CreateCredDefPayload, CredDefPayload, GetAllCredDefsPayload, GetCredDefBySchemaId, GetCredDefPayload } from './interfaces/create-credential-definition.interface';
import { CreateCredDefPayload, CredDefPayload, GetAllCredDefsPayload, GetCredDefBySchemaId, GetCredDefPayload, IPlatformCredDefs } from './interfaces/create-credential-definition.interface';
import { credential_definition } from '@prisma/client';
import { ResponseMessages } from '@credebl/common/response-messages';
import { CreateCredDefAgentRedirection, CredDefSchema, GetCredDefAgentRedirection } from './interfaces/credential-definition.interface';
import { map } from 'rxjs/operators';
import { OrgAgentType, SortValue } from '@credebl/enum/enum';
import { Cache } from 'cache-manager';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { ICredDefDetails } from '@credebl/common/interfaces/cred-def.interface';
import { ICredDefDetails, IPlatformCredDefsData } from '@credebl/common/interfaces/cred-def.interface';
@Injectable()
export class CredentialDefinitionService extends BaseService {
constructor(
Expand Down Expand Up @@ -171,6 +171,34 @@ export class CredentialDefinitionService extends BaseService {
}
}

async getAllPlatformCredDefs(credDefsPayload: IPlatformCredDefs): Promise<IPlatformCredDefsData> {
try {
const { pageSize, pageNumber } = credDefsPayload;
const response = await this.credentialDefinitionRepository.getAllPlatformCredDefsDetails(credDefsPayload);

const credDefResponse: IPlatformCredDefsData = {
totalItems: response.credDefCount,
hasNextPage: pageSize * pageNumber < response.credDefCount,
hasPreviousPage: 1 < pageNumber,
nextPage: pageNumber + 1,
previousPage: pageNumber - 1,
lastPage: Math.ceil(response.credDefCount / pageSize),
data: response.credDefResult
};

if (0 !== response.credDefCount) {
return credDefResponse;
} else {
throw new NotFoundException(ResponseMessages.credentialDefinition.error.NotFound);
}


} catch (error) {
this.logger.error(`Error in retrieving all credential definitions: ${error}`);
throw new RpcException(error.response ? error.response : error);
}
}

async getCredentialDefinitionById(payload: GetCredDefPayload): Promise<credential_definition> {
try {
const { credentialDefinitionId, orgId } = payload;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,16 @@ export interface CreateCredDefPayload {
orgId?: string;
}

export interface IPlatformCredDefs {
ledgerId?: string;
pageNumber?: number;
pageSize?: number;
sortField?: string;
sortBy?: string;
search?: string;
user: IUserRequestInterface
}

export interface CredDefPayload {
userId?: string,
schemaId?: string;
Expand All @@ -46,7 +56,6 @@ export class GetAllCredDefsDto {
revocable?: boolean;
sortByValue?: string;
}

export interface GetAllCredDefsPayload {
credDefSearchCriteria: GetAllCredDefsDto,
user: IUserRequestInterface,
Expand Down
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
/* eslint-disable camelcase */
import { CredDefPayload, GetAllCredDefsDto } from '../interfaces/create-credential-definition.interface';
import { CredDefPayload, GetAllCredDefsDto, IPlatformCredDefs } from '../interfaces/create-credential-definition.interface';
import { PrismaService } from '@credebl/prisma-service';
import { credential_definition, org_agents, org_agents_type, organisation, schema } from '@prisma/client';
import { Injectable, Logger } from '@nestjs/common';
import { ResponseMessages } from '@credebl/common/response-messages';
import { BulkCredDefSchema, CredDefSchema } from '../interfaces/credential-definition.interface';
import { ICredDefData } from '@credebl/common/interfaces/cred-def.interface';
import { ICredDefData, IPlatformCredDefDetails } from '@credebl/common/interfaces/cred-def.interface';
import { SortValue } from '@credebl/enum/enum';

@Injectable()
Expand Down Expand Up @@ -57,6 +57,51 @@ export class CredentialDefinitionRepository {
}
}

async getAllPlatformCredDefsDetails(credDefsPayload: IPlatformCredDefs): Promise<IPlatformCredDefDetails> {
try {
const { ledgerId, search, sortBy, sortField, pageNumber, pageSize } = credDefsPayload || {};
const credDefResult = await this.prisma.credential_definition.findMany({
where: {
schema: {
ledgerId
},
OR: [
{ tag: { contains: search, mode: 'insensitive' } },
{ credentialDefinitionId: { contains: search, mode: 'insensitive' } },
{ schemaLedgerId: { contains: search, mode: 'insensitive' } }
]
},
select: {
createDateTime: true,
tag: true,
schemaId: true,
orgId: true,
schemaLedgerId: true,
createdBy: true,
credentialDefinitionId: true,
revocable: true
},
orderBy: {
[sortField]: SortValue.DESC === sortBy ? SortValue.DESC : SortValue.ASC
},
take: Number(pageSize),
skip: (pageNumber - 1) * pageSize
});

const credDefCount = await this.prisma.credential_definition.count({
where: {
schema: {
ledgerId
}
}
});
return { credDefCount, credDefResult };
} catch (error) {
this.logger.error(`Error in getting credential definitions: ${error}`);
throw error;
}
}

async getByAttribute(schema: string, tag: string): Promise<credential_definition> {
try {
const response = await this.prisma.credential_definition.findFirst({ where: { schemaLedgerId: schema, tag: { contains: tag, mode: 'insensitive' } } });
Expand Down
8 changes: 0 additions & 8 deletions apps/ledger/src/schema/enum/schema.enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,3 @@ export enum SortFields {
PUBLISHER_DID = 'publisherDid',
ISSUER_ID = 'issuerId'
}

export enum CredDefSortFields {
ID = 'id',
CREATED_DATE_TIME = 'createDateTime',
TAG = 'tag',
LEDGER_ID = 'schemaLedgerId',
CRED_DEF_ID= 'credentialDefinitionId'
}
27 changes: 20 additions & 7 deletions libs/common/src/interfaces/cred-def.interface.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,4 @@
export interface ICredDefDetails {
totalItems: number;
hasNextPage: boolean;
hasPreviousPage: boolean;
nextPage: number;
previousPage: number;
lastPage: number;
export interface ICredDefDetails extends IPaginationDetails{
data: ICredDefData[];
}

Expand All @@ -18,3 +12,22 @@ export interface ICredDefData {
orgId: string;
revocable: boolean;
}

export interface IPlatformCredDefDetails {
credDefCount: number;
credDefResult: ICredDefData[];
}

export interface IPaginationDetails {
totalItems: number;
hasNextPage: boolean;
hasPreviousPage: boolean;
nextPage: number;
previousPage: number;
lastPage: number;
}

export interface IPlatformCredDefsData extends IPaginationDetails{
data: ICredDefData[];
}

7 changes: 7 additions & 0 deletions libs/enum/src/enum.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,13 @@ export enum SortFields {
ISSUER_ID = 'issuerId'
}

export enum CredDefSortFields {
CREATED_DATE_TIME = 'createDateTime',
TAG = 'tag',
LEDGER_ID = 'schemaLedgerId',
CRED_DEF_ID= 'credentialDefinitionId'
}

export enum AgentType {
AFJ = 'AFJ',
ACAPY = 'ACAPY'
Expand Down

0 comments on commit a70296d

Please sign in to comment.