Skip to content
Draft
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
7 changes: 6 additions & 1 deletion apps/api-gateway/src/dtos/create-schema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -158,7 +158,12 @@ export class GenericSchemaDTO {
@IsEnum(SchemaTypeEnum, { message: 'Type must be a valid schema type' })
@IsNotEmpty({ message: 'Type is required' })
type: SchemaTypeEnum;


@ApiPropertyOptional({ default: false })
@IsBoolean()
@IsOptional()
@IsNotEmpty({ message: 'isSample property is required' })
isSample: boolean = false;
@ApiProperty({
type: Object,
oneOf: [
Expand Down
14 changes: 14 additions & 0 deletions apps/api-gateway/src/schema/dtos/get-schema-by-ids-dto.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
import { ApiProperty } from '@nestjs/swagger';
import { IsArray, IsNotEmpty, IsString } from 'class-validator';

export class SchemaIdsDto {
@ApiProperty({
description: 'Array of schema IDs',
type: [String],
example: ['schema1', 'schema2']
})
@IsArray()
@IsNotEmpty({ message: 'Schema IDs array cannot be empty' })
@IsString({ each: true })
schemaIds: string[];
}
28 changes: 28 additions & 0 deletions apps/api-gateway/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { GenericSchemaDTO } from '../dtos/create-schema.dto';
import { CustomExceptionFilter } from 'apps/api-gateway/common/exception-handler';
import { CredDefSortFields, SortFields } from '@credebl/enum/enum';
import { TrimStringParamPipe } from '@credebl/common/cast.helper';
import { SchemaIdsDto } from './dtos/get-schema-by-ids-dto';

@UseFilters(CustomExceptionFilter)
@Controller('orgs')
Expand Down Expand Up @@ -161,6 +162,32 @@ export class SchemaController {
}


/**
* Retrieves a list of schemas associated with a given Ids.
* @returns A list of schemas associated with a given Ids.
*/
@Get('/schemas-by-ids')
@ApiOperation({
summary: 'Retrieves a list of schemas associated with a given Ids',
description: 'Retrieves a list of schemas associated with a given Ids'
})
@ApiResponse({ status: HttpStatus.OK, description: 'Success', type: ApiResponseDto })
async getSchemasByIds(
@Body() schemaDetails: SchemaIdsDto,
@Res() res: Response

): Promise<Response> {
const schemasResponse = await this.appService.getSchemasByIds(schemaDetails);

const finalResponse: IResponse = {
statusCode: HttpStatus.OK,
message: ResponseMessages.schema.success.fetch,
data: schemasResponse
};
return res.status(HttpStatus.OK).json(finalResponse);
}


/**
* Create and register various types of schemas.
*
Expand All @@ -186,4 +213,5 @@ export class SchemaController {
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

}
6 changes: 6 additions & 0 deletions apps/api-gateway/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@ import { ISchemaInfo, IUserRequestInterface } from './interfaces';
import { ICredDefWithPagination, ISchemaData, ISchemasWithPagination } from '@credebl/common/interfaces/schema.interface';
import { GetCredentialDefinitionBySchemaIdDto } from './dtos/get-all-schema.dto';
import { NATSClient } from '@credebl/common/NATSClient';
import { SchemaIdsDto } from './dtos/get-schema-by-ids-dto';
import { SchemaDetails } from 'apps/issuance/interfaces/issuance.interfaces';

@Injectable()
export class SchemaService extends BaseService {
Expand All @@ -32,6 +34,10 @@ export class SchemaService extends BaseService {
return this.natsClient.sendNatsMessage(this.schemaServiceProxy, 'get-schemas', schemaSearch);
}

getSchemasByIds(schemaDetails: SchemaIdsDto): Promise<SchemaDetails[]> {
return this.natsClient.sendNatsMessage(this.schemaServiceProxy, 'get-schemas-by-ids', schemaDetails);
}

getcredDefListBySchemaId(schemaSearchCriteria: GetCredentialDefinitionBySchemaIdDto, user: IUserRequestInterface): Promise<ICredDefWithPagination> {
const payload = { schemaSearchCriteria, user };
return this.natsClient.sendNatsMessage(this.schemaServiceProxy, 'get-cred-def-list-by-schemas-id', payload);
Expand Down
10 changes: 9 additions & 1 deletion apps/ledger/src/schema/interfaces/schema-payload.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -121,4 +121,12 @@ export interface ISaveSchema {

export interface SaveSchemaPayload {
schemaDetails: ISaveSchema
}
}


export interface SchemaDetails {
id: string;
type: string;
isSchemaArchived: boolean;
isSample: boolean;
}
1 change: 1 addition & 0 deletions apps/ledger/src/schema/interfaces/schema.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ export interface ICreateW3CSchema {
}
export interface IGenericSchema {
type: SchemaTypeEnum;
isSample:boolean,
schemaPayload: ICreateSchema | ICreateW3CSchema;
}

Expand Down
26 changes: 25 additions & 1 deletion apps/ledger/src/schema/repositories/schema.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import { ConflictException, Injectable, InternalServerErrorException, Logger } from '@nestjs/common';
import { PrismaService } from '@credebl/prisma-service';
import { ledgers, org_agents, org_agents_type, organisation, Prisma, schema } from '@prisma/client';
import { ISchema, ISchemaExist, ISchemaSearchCriteria, ISaveSchema } from '../interfaces/schema-payload.interface';
import { ISchema, ISchemaExist, ISchemaSearchCriteria, ISaveSchema, SchemaDetails } from '../interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { AgentDetails, ISchemasWithCount } from '../interfaces/schema.interface';
import { SchemaType, SortValue } from '@credebl/enum/enum';
Expand Down Expand Up @@ -456,4 +456,28 @@ export class SchemaRepository {
throw error;
}
}


async getSchemasByIds(schemaDetails:string[]): Promise<SchemaDetails[]> {
try {
return await this.prisma.schema.findMany({
where: {
schemaLedgerId: {
in: schemaDetails
}
},
select: {
id: true,
type: true,
isSchemaArchived: true,
isSample: true
}
});

} catch (error) {
this.logger.error(`Error in retrieving schemas by schema ids: ${error}`);
throw new RpcException(error.response ? error.response : error);
}
}

}
9 changes: 8 additions & 1 deletion apps/ledger/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@ import {
ISchemaCredDeffSearchInterface,
ISchemaExist,
ISchemaSearchPayload,
SaveSchemaPayload
SaveSchemaPayload,
SchemaDetails
} from './interfaces/schema-payload.interface';
import { Prisma, schema } from '@prisma/client';
import {
Expand All @@ -17,6 +18,7 @@ import {
} from '@credebl/common/interfaces/schema.interface';
import { IschemaPayload } from './interfaces/schema.interface';
import { ISchemaId } from './schema.interface';
import { SchemaIdsDto } from 'apps/api-gateway/src/schema/dtos/get-schema-by-ids-dto';

@Controller('schema')
export class SchemaController {
Expand Down Expand Up @@ -52,6 +54,11 @@ export class SchemaController {
return this.schemaService.getSchemas(schemaSearchCriteria, orgId);
}

@MessagePattern({ cmd: 'get-schemas-by-ids' })
async getSchemasByIds(schemaDetails: SchemaIdsDto): Promise<SchemaDetails[]> {
return this.schemaService.getSchemasByIds(schemaDetails.schemaIds);
}

@MessagePattern({ cmd: 'get-cred-def-list-by-schemas-id' })
async getcredDefListBySchemaId(payload: ISchemaCredDeffSearchInterface): Promise<ICredDefWithPagination> {
return this.schemaService.getcredDefListBySchemaId(payload);
Expand Down
26 changes: 19 additions & 7 deletions apps/ledger/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ import { ClientProxy, RpcException } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { SchemaRepository } from './repositories/schema.repository';
import { Prisma, schema } from '@prisma/client';
import { ISaveSchema, ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaSearchCriteria, W3CCreateSchema } from './interfaces/schema-payload.interface';
import { ISaveSchema, ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaSearchCriteria, SchemaDetails, W3CCreateSchema } from './interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { ICreateSchema, ICreateW3CSchema, IGenericSchema, IUserRequestInterface } from './interfaces/schema.interface';
import { CreateSchemaAgentRedirection, GetSchemaAgentRedirection, ISchemaId } from './schema.interface';
Expand Down Expand Up @@ -48,7 +48,7 @@ export class SchemaService extends BaseService {

const userId = user.id;
try {
const {schemaPayload, type} = schemaDetails;
const {schemaPayload, type, isSample } = schemaDetails;

if (type === SchemaTypeEnum.INDY) {

Expand Down Expand Up @@ -247,7 +247,7 @@ export class SchemaService extends BaseService {
}
} else if (type === SchemaTypeEnum.JSON) {
const josnSchemaDetails = schemaPayload as unknown as ICreateW3CSchema;
const createW3CSchema = await this.createW3CSchema(orgId, josnSchemaDetails, user.id);
const createW3CSchema = await this.createW3CSchema(orgId, josnSchemaDetails, user.id, isSample);
return createW3CSchema;
}
} catch (error) {
Expand All @@ -259,7 +259,7 @@ export class SchemaService extends BaseService {
}
}

async createW3CSchema(orgId:string, schemaPayload: ICreateW3CSchema, user: string): Promise<ISchemaData> {
async createW3CSchema(orgId:string, schemaPayload: ICreateW3CSchema, user: string, isSample:boolean): Promise<ISchemaData> {
try {
let createSchema;

Expand Down Expand Up @@ -325,7 +325,7 @@ export class SchemaService extends BaseService {
createSchema.schemaUrl = `${process.env.SCHEMA_FILE_SERVER_URL}${createSchemaPayload.data.schemaId}`;
}

const storeW3CSchema = await this.storeW3CSchemas(createSchema, user, orgId, attributes);
const storeW3CSchema = await this.storeW3CSchemas(createSchema, user, orgId, attributes, isSample);

if (!storeW3CSchema) {
throw new BadRequestException(ResponseMessages.schema.error.storeW3CSchema, {
Expand Down Expand Up @@ -524,7 +524,7 @@ export class SchemaService extends BaseService {
return W3CSchema;
}

private async storeW3CSchemas(schemaDetails, user, orgId, attributes): Promise <schema> {
private async storeW3CSchemas(schemaDetails, user, orgId, attributes, isSample): Promise <schema> {
let ledgerDetails;
const schemaServerUrl = `${process.env.SCHEMA_FILE_SERVER_URL}${schemaDetails.schemaId}`;
const schemaRequest = await this.commonService
Expand Down Expand Up @@ -563,7 +563,8 @@ export class SchemaService extends BaseService {
publisherDid: schemaDetails.did,
orgId,
ledgerId: ledgerDetails.id,
type: SchemaType.W3C_Schema
type: SchemaType.W3C_Schema,
isSample
};
const saveResponse = await this.schemaRepository.saveSchema(
storeSchemaDetails
Expand Down Expand Up @@ -928,4 +929,15 @@ export class SchemaService extends BaseService {
throw new RpcException(error.response ? error.response : error);
}
}


async getSchemasByIds(schemaDetails: string[]): Promise<SchemaDetails[]> {
try {
const schemaSearchResult = await this.schemaRepository.getSchemasByIds(schemaDetails);
return schemaSearchResult;
} catch (error) {
this.logger.error(`Error in getSchemasByIds: ${error}`);
throw new RpcException(error.response ? error.response : error);
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- AlterTable
ALTER TABLE "schema" ADD COLUMN "isSample" BOOLEAN DEFAULT false;
1 change: 1 addition & 0 deletions libs/prisma-service/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,7 @@ model schema {
type String? @db.VarChar
isSchemaArchived Boolean @default(false)
credential_definition credential_definition[]
isSample Boolean? @default(false)
}

model credential_definition {
Expand Down