8000 feat: Added W3C-schema API by tipusinghaw · Pull Request #600 · credebl/platform · GitHub
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

feat: Added W3C-schema API #600

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

Merged
merged 11 commits into from
Mar 28, 2024
6 changes: 6 additions & 0 deletions apps/agent-service/src/agent-service.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,12 @@ export class AgentServiceController {
return this.agentServiceService.createSchema(payload);
}

//DONE
@MessagePattern({ cmd: 'agent-create-w3c-schema' })
async createW3CSchema(payload: { url, orgId, schemaRequestPayload }): Promise<object> {
return this.agentServiceService.createW3CSchema(payload.url, payload.orgId, payload.schemaRequestPayload);
}

//DONE
@MessagePattern({ cmd: 'agent-get-schema' })
async getSchemaById(payload: IGetSchemaAgentRedirection): Promise<object> {
Expand Down
11 changes: 11 additions & 0 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1561,6 +1561,17 @@ export class AgentServiceService {
return data;
}

async createW3CSchema(url: string, orgId: string, schemaRequestPayload): Promise<object> {
try {
const getApiKey = await this.getOrgAgentApiKey(orgId);
const schemaRequest = await this.commonService
.httpPost(url, schemaRequestPayload, { headers: { 'authorization': getApiKey } })
.then(async response => response);
return schemaRequest;
} catch (error) {
this.logger.error(`Error in schema endorsement request in agent service : ${JSON.stringify(error)}`);
}
}
async natsCall(pattern: object, payload: object): Promise<{
response: string;
}> {
Expand Down
19 changes: 19 additions & 0 deletions apps/api-gateway/src/dtos/create-schema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -70,3 +70,22 @@ export class CreateSchemaDto {
@IsString({ message: 'orgDid must be a string' })
orgDid: string;
}

export class CreateW3CSchemaDto {
@ApiProperty()
@IsNotEmpty({ message: 'schemaObject is required' })
schema: object;

@ApiProperty()
@IsString({ message: 'schemaName must be a string' })
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'schemaName is required' })
schemaName: string;

@ApiProperty()
@IsString({ message: 'did must be a string' })
@Transform(({ value }) => trim(value))
@IsNotEmpty({ message: 'did is required' })
did: string;

}
6 changes: 6 additions & 0 deletions apps/api-gateway/src/interfaces/ISchemaSearch.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,9 @@ export interface ISchemaSearchPayload {
user?: IUserRequestInterface
}


export interface W3CSchemaPayload {
schema: object;
schemaName: string;
did: string;
}
22 changes: 21 additions & 1 deletion apps/api-gateway/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { OrgRoles } from 'libs/org-roles/enums';
import { Roles } from '../authz/decorators/roles.decorator';
import { IUserRequestInterface } from './interfaces';
import { OrgRolesGuard } from '../authz/guards/org-roles.guard';
import { CreateSchemaDto } from '../dtos/create-schema.dto';
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';

Expand Down Expand Up @@ -133,6 +133,26 @@ export class SchemaController {
return res.status(HttpStatus.OK).json(finalResponse);
}

@Post('/:orgId/polygon-w3c/schemas')
@ApiOperation({
summary: 'Create and sends a W3C-schema to the ledger.',
description: 'Create and sends a W3C-schema to the ledger.'
})
@Roles(OrgRoles.OWNER, OrgRoles.ADMIN, OrgRoles.ISSUER, OrgRoles.VERIFIER, OrgRoles.MEMBER)
@UseGuards(AuthGuard('jwt'), OrgRolesGuard)
@ApiResponse({ status: HttpStatus.CREATED, description: 'Success', type: ApiResponseDto })
async createW3CSchema(@Res() res: Response, @Body() schemaPayload: CreateW3CSchemaDto, @Param('orgId') orgId: string, @User() user: IUserRequestInterface): Promise<Response> {

const schemaDetails = await this.appService.createW3CSchema(schemaPayload, orgId);

const finalResponse: IResponseType = {
statusCode: HttpStatus.CREATED,
message: ResponseMessages.schema.success.create,
data: schemaDetails
};
return res.status(HttpStatus.CREATED).json(finalResponse);
}

@Post('/:orgId/schemas')
@ApiOperation({
summary: 'Create and sends a schema to the ledger.',
Expand Down
7 changes: 6 additions & 1 deletion apps/api-gateway/src/schema/schema.service.ts
< 5DA1 td class="blob-code blob-code-deletion js-file-line"> import { ISchemaSearchPayload } from '../interfaces/ISchemaSearch.interface';
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import { Injectable, Inject } from '@nestjs/common';
import { ClientProxy } from '@nestjs/microservices';
import { BaseService } from '../../../../libs/service/base.service';
import { CreateSchemaDto } from '../dtos/create-schema.dto';
import { ISchemaSearchPayload, W3CSchemaPayload } from '../interfaces/ISchemaSearch.interface';
import { IUserRequestInterface } from './interfaces';
import { ICredDefWithPagination, ISchemaData, ISchemasWithPagination } from '@credebl/common/interfaces/schema.interface';
import { GetCredentialDefinitionBySchemaIdDto } from './dtos/get-all-schema.dto';
Expand All @@ -19,6 +19,11 @@ export class SchemaService extends BaseService {
return this.sendNatsMessage(this.schemaServiceProxy, 'create-schema', payload);
}

createW3CSchema(schemaPayload: W3CSchemaPayload, orgId: string): Promise<object> {
const payload = { schemaPayload, orgId };
return this.sendNatsMessage(this.schemaServiceProxy, 'create-w3c-schema', payload);
}

getSchemaById(schemaId: string, orgId: string): Promise<{
response: object;
}> {
Expand Down
16 changes: 16 additions & 0 deletions apps/ledger/src/schema/interfaces/schema-payload.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,3 +72,19 @@ export interface ISchemaExist {
version: string;
}

interface SchemaPayload {
schema: object,
schemaName: string,
did: string
}

export interface W3CSchemaPayload {
schemaPayload: SchemaPayload,
orgId: string
}

export interface W3CCreateSchema {
url: string,
orgId: string,
schemaRequestPayload: SchemaPayload
}
8 changes: 7 additions & 1 deletion apps/ledger/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,8 @@ import {
ISchema,
ISchemaCredDeffSearchInterface,
ISchemaExist,
ISchemaSearchPayload
ISchemaSearchPayload,
W3CSchemaPayload
} from './interfaces/schema-payload.interface';
import { schema } from '@prisma/client';
import {
Expand All @@ -24,6 +25,11 @@ export class SchemaController {
return this.schemaService.createSchema(schema, user, orgId);
}

@MessagePattern({ cmd: 'create-w3c-schema' })
async createW3CSchema(payload: W3CSchemaPayload): Promise<object> {
return this.schemaService.createW3CSchema(payload);
}

@MessagePattern({ cmd: 'get-schema-by-id' })
async getSchemaById(payload: ISchema): Promise<schema> {
const { schemaId, orgId } = payload;
Expand Down
63 changes: 61 additions & 2 deletions apps/ledger/src/schema/schema.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ import { ClientProxy, RpcException } from '@nestjs/microservices';
import { BaseService } from 'libs/service/base.service';
import { SchemaRepository } from './repositories/schema.repository';
import { schema } from '@prisma/client';
import { ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaPayload, ISchemaSearchCriteria } from './interfaces/schema-payload.interface';
import { ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaPayload, ISchemaSearchCriteria, W3CCreateSchema, W3CSchemaPayload } from './interfaces/schema-payload.interface';
import { ResponseMessages } from '@credebl/common/response-messages';
import { IUserRequestInterface } from './interfaces/schema.interface';
import { CreateSchemaAgentRedirection, GetSchemaAgentRedirection } from './schema.interface';
Expand All @@ -20,6 +20,7 @@ import { OrgAgentType } from '@credebl/enum/enum';
import { ICredDefWithPagination, ISchemaData, ISchemasWithPagination } from '@credebl/common/interfaces/schema.interface';
import { Cache } from 'cache-manager';
import { CACHE_MANAGER } from '@nestjs/cache-manager';
import { CommonConstants } from '@credebl/common/common.constant';

@Injectable()
export class SchemaService extends BaseService {
Expand Down Expand Up @@ -241,6 +242,43 @@ export class SchemaService extends BaseService {
}
}

async createW3CSchema(
schemaRequestPayload: W3CSchemaPayload
): Promise<object> {
try {
const { orgId } = schemaRequestPayload;
const agentDetails = await this.schemaRepository.getAgentDetailsByOrgId(orgId);
if (!agentDetails) {
throw new NotFoundException(
ResponseMessages.schema.error.agentDetailsNotFound,
{ cause: new Error(), description: ResponseMessages.errorMessages.notFound }
);
}
const { agentEndPoint } = agentDetails;
const getAgentDetails = await this.schemaRepository.getAgentType(orgId);
const orgAgentType = await this.schemaRepository.getOrgAgentType(getAgentDetails.org_agents[0].orgAgentTypeId);
let url;
if (OrgAgentType.DEDICATED === orgAgentType) {
url = `${agentEndPoint}${CommonConstants.DEDICATED_CREATE_POLYGON_W3C_SCHEMA}`;
} else if (OrgAgentType.SHARED === orgAgentType) {
const { tenantId } = await this.schemaRepository.getAgentDetailsByOrgId(orgId);
url = `${agentEndPoint}${CommonConstants.SHARED_CREATE_POLYGON_W3C_SCHEMA}${tenantId}`;
}
const W3cSchemaPayload = {
url,
orgId,
schemaRequestPayload: schemaRequestPayload.schemaPayload
};
return this._createW3CSchema(W3cSchemaPayload);
} catch (error) {
this.logger.error(
`[createSchema] - outer Error: ${JSON.stringify(error)}`
);
throw new RpcException(error.error ? error.error.message : error.message);
}
}


async _createSchema(payload: CreateSchemaAgentRedirection): Promise<{
response: string;
}> {
Expand All @@ -267,6 +305,27 @@ export class SchemaService extends BaseService {
return schemaResponse;
}

async _createW3CSchema(payload: W3CCreateSchema): Promise<{
response: string;
}> {
const natsPattern = {
cmd: 'agent-create-w3c-schema'
};
const W3CSchemaResponse = await this.schemaServiceProxy
.send(natsPattern, payload)
.pipe(
map((response) => (
{
response
}))
).toPromise()
.catch(error => {
this.logger.error(`Error in creating W3C schema : ${JSON.stringify(error)}`);
throw new RpcException(error.error ? error.error.message : error.message);
});
return W3CSchemaResponse;
}


async getSchemaById(schemaId: string, orgId: string): Promise<schema> {
try {
Expand Down Expand Up @@ -455,7 +514,7 @@ export class SchemaService extends BaseService {
async _getOrgAgentApiKey(orgId: string): Promise<string> {
const pattern = { cmd: 'get-org-agent-api-key' };
const payload = { orgId };

try {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const message = await this.schemaServiceProxy.send<any>(pattern, payload).toPromise();
Expand Down
4 changes: 4 additions & 0 deletions libs/common/src/common.constant.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ export enum CommonConstants {
URL_SCHM_GET_CRED_DEF_BY_ID = '/credential-definitions/#',
URL_SCHM_GET_CRED_DEF_BY_ATTRB = '/credential-definitions/created',

// POLYGON BASED W3C SCHEMAS
DEDICATED_CREATE_POLYGON_W3C_SCHEMA = '/polygon/create-schema',
SHARED_CREATE_POLYGON_W3C_SCHEMA = '/multi-tenancy/polygon-wc3/schema/',
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
SHARED_CREATE_POLYGON_W3C_SCHEMA = '/multi-tenancy/polygon-wc3/schema/',
SHARED_CREATE_POLYGON_W3C_SCHEMA = '/multi-tenancy/polygon-w3c/schema/',


// SHARED AGENT
URL_SHAGENT_CREATE_TENANT = '/multi-tenancy/create-tenant',
URL_SHAGENT_CREATE_DID = '/multi-tenancy/create-did/',
Expand Down
0