Skip to content

Commit

Permalink
Merge pull request #652 from credebl/develop-to-qa
Browse files Browse the repository at this point in the history
merge: develop to qa
  • Loading branch information
bhavanakarwade authored Apr 11, 2024
2 parents 7474a2c + b101f4a commit baa1f12
Show file tree
Hide file tree
Showing 10 changed files with 269 additions and 32 deletions.
2 changes: 1 addition & 1 deletion apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1570,7 +1570,7 @@ export class AgentServiceService {
.then(async response => response);
return schemaRequest;
} catch (error) {
this.logger.error(`Error in schema endorsement request in agent service : ${JSON.stringify(error)}`);
this.logger.error(`Error in createW3CSchema request in agent service : ${JSON.stringify(error)}`);
}
}

Expand Down
2 changes: 1 addition & 1 deletion apps/api-gateway/src/authz/guards/org-roles.guard.ts
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ export class OrgRolesGuard implements CanActivate {
return false;

} else {
throw new BadRequestException('organization is required');
throw new BadRequestException('Please provide valid orgId');
}

// Sending user friendly message if a user attempts to access an API that is inaccessible to their role
Expand Down
36 changes: 30 additions & 6 deletions apps/api-gateway/src/dtos/create-schema.dto.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,20 +72,44 @@ export class CreateSchemaDto {
}

export class CreateW3CSchemaDto {
@ApiProperty()
@IsNotEmpty({ message: 'schemaObject is required' })
schema: object;
@ApiProperty({
type: [],
'example': [
{
title: 'name',
type: 'string'
}
]
})
@IsNotEmpty({ message: 'Schema attributes are required' })
schemaAttributes: SchemaAttributes [];

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

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


@ApiProperty()
@IsString({ message: 'description must be a string' })
@IsNotEmpty({ message: 'description is required' })
description: string;
}

export class SchemaAttributes {
@ApiProperty()
@IsNotEmpty({ message: 'type is required' })
@IsString({ message: 'type must be a string' })
type: string;

@ApiProperty()
@IsNotEmpty({ message: 'title is required' })
@IsString({ message: 'title must be a string' })
title: string;
}
7 changes: 6 additions & 1 deletion apps/api-gateway/src/interfaces/ISchemaSearch.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,12 @@ export interface ISchemaSearchPayload {


export interface W3CSchemaPayload {
schema: object;
schemaAttributes: W3CSchemaAttributes [];
schemaName: string;
did: string;
}

interface W3CSchemaAttributes {
type: string,
title: string
}
20 changes: 16 additions & 4 deletions apps/ledger/src/schema/interfaces/schema-payload.interface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -72,11 +72,17 @@ export interface ISchemaExist {
version: string;
}

interface SchemaPayload {
schema: object,
export interface SchemaPayload {
schemaAttributes: W3CSchemaAttributes [],
schemaName: string,
did: string
did: string,
description: string
}

export interface W3CSchemaAttributes {
type: string,
title: string,
}

export interface W3CSchemaPayload {
schemaPayload: SchemaPayload,
Expand All @@ -86,5 +92,11 @@ export interface W3CSchemaPayload {
export interface W3CCreateSchema {
url: string,
orgId: string,
schemaRequestPayload: SchemaPayload
schemaRequestPayload: object
}

export interface IdAttribute extends W3CSchemaAttributes {
format: string;
order?: string
}

3 changes: 2 additions & 1 deletion apps/ledger/src/schema/schema.controller.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,8 @@ export class SchemaController {

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

@MessagePattern({ cmd: 'get-schema-by-id' })
Expand Down
222 changes: 208 additions & 14 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, W3CCreateSchema, W3CSchemaPayload } from './interfaces/schema-payload.interface';
import { ISchema, ISchemaCredDeffSearchInterface, ISchemaExist, ISchemaPayload, ISchemaSearchCriteria, SchemaPayload, W3CCreateSchema } 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 Down Expand Up @@ -242,17 +242,15 @@ export class SchemaService extends BaseService {
}
}

async createW3CSchema(
schemaRequestPayload: W3CSchemaPayload
): Promise<object> {
async createW3CSchema(orgId:string, schemaPayload: SchemaPayload): Promise<object> {
try {
const { orgId } = schemaRequestPayload;
const { description, did, schemaAttributes, schemaName} = schemaPayload;
const agentDetails = await this.schemaRepository.getAgentDetailsByOrgId(orgId);
if (!agentDetails) {
throw new NotFoundException(
ResponseMessages.schema.error.agentDetailsNotFound,
{ cause: new Error(), description: ResponseMessages.errorMessages.notFound }
);
throw new NotFoundException(ResponseMessages.schema.error.agentDetailsNotFound, {
cause: new Error(),
description: ResponseMessages.errorMessages.notFound
});
}
const { agentEndPoint } = agentDetails;
const getAgentDetails = await this.schemaRepository.getAgentType(orgId);
Expand All @@ -264,21 +262,217 @@ export class SchemaService extends BaseService {
const { tenantId } = await this.schemaRepository.getAgentDetailsByOrgId(orgId);
url = `${agentEndPoint}${CommonConstants.SHARED_CREATE_POLYGON_W3C_SCHEMA}${tenantId}`;
}

const schemaObject = await this.w3cSchemaBuilder(schemaAttributes, schemaName, description);

if (!schemaObject) {
throw new BadRequestException(ResponseMessages.schema.error.schemaBuilder, {
cause: new Error(),
description: ResponseMessages.errorMessages.badRequest
});
}
const agentSchemaPayload = {
schema:schemaObject,
did,
schemaName
};

const W3cSchemaPayload = {
url,
orgId,
schemaRequestPayload: schemaRequestPayload.schemaPayload
schemaRequestPayload: agentSchemaPayload
};
return this._createW3CSchema(W3cSchemaPayload);
} catch (error) {
this.logger.error(
`[createSchema] - outer Error: ${JSON.stringify(error)}`
);
this.logger.error(`[createSchema] - outer Error: ${JSON.stringify(error)}`);
throw new RpcException(error.error ? error.error.message : error.message);
}
}


// eslint-disable-next-line @typescript-eslint/explicit-function-return-type
private async w3cSchemaBuilder(schemaAttributes, schemaName: string, description: string) {
const schemaAttributeJson = schemaAttributes.map((attribute, index) => ({
[attribute.title]: {
type: attribute.type.toLowerCase(),
order: index,
title: attribute.title
}
}));

// Add the format property to the id key
schemaAttributeJson.unshift({
id: {
type: 'string',
format: 'uri'
}
});

const nestedObject = {};
schemaAttributeJson.forEach((obj) => {
// eslint-disable-next-line prefer-destructuring
const key = Object.keys(obj)[0];
nestedObject[key] = obj[key];
});

const schemaNameObject = {};
schemaNameObject[schemaName] = {
"const": schemaName
};
const date = new Date().toISOString();

const W3CSchema = {
$schema: 'http://json-schema.org/draft-07/schema#',
$id: `${date}-${schemaName}`,
type: 'object',
required: ['@context', 'issuer', 'issuanceDate', 'type', 'credentialSubject'],
properties: {
'@context': {
$ref: '#/definitions/context'
},
type: {
type: 'array',
items: {
anyOf: [
{
$ref: '#/definitions/VerifiableCredential'
},
{
const: `#/definitions/$${schemaName}`
}
]
}
},
credentialSubject: {
$ref: '#/definitions/credentialSubject'
},
id: {
type: 'string',
format: 'uri'
},
issuer: {
$ref: '#/definitions/uriOrId'
},
issuanceDate: {
type: 'string',
format: 'date-time'
},
expirationDate: {
type: 'string',
format: 'date-time'
},
credentialStatus: {
$ref: '#/definitions/credentialStatus'
},
credentialSchema: {
$ref: '#/definitions/credentialSchema'
}
},
definitions: {
context: {
type: 'array',
items: [
{
const: 'https://www.w3.org/2018/credentials/v1'
}
],
additionalItems: {
oneOf: [
{
type: 'string',
format: 'uri'
},
{
type: 'object'
},
{
type: 'array',
items: {
$ref: '#/definitions/context'
}
}
]
},
minItems: 1,
uniqueItems: true
},
credentialSubject: {
type: 'object',
required: ['id'],
additionalProperties: false,
properties: nestedObject
},
VerifiableCredential: {
const: 'VerifiableCredential'
},
credentialSchema: {
oneOf: [
{
$ref: '#/definitions/idAndType'
},
{
type: 'array',
items: {
$ref: '#/definitions/idAndType'
},
minItems: 1,
uniqueItems: true
}
]
},
credentialStatus: {
oneOf: [
{
$ref: '#/definitions/idAndType'
},
{
type: 'array',
items: {
$ref: '#/definitions/idAndType'
},
minItems: 1,
uniqueItems: true
}
]
},
idAndType: {
type: 'object',
required: ['id', 'type'],
properties: {
id: {
type: 'string',
format: 'uri'
},
type: {
type: 'string'
}
}
},
uriOrId: {
oneOf: [
{
type: 'string',
format: 'uri'
},
{
type: 'object',
required: ['id'],
properties: {
id: {
type: 'string',
format: 'uri'
}
}
}
]
},
...schemaNameObject
},
title: schemaName,
description: `${description}`
};
return W3CSchema;
}

async _createSchema(payload: CreateSchemaAgentRedirection): Promise<{
response: string;
}> {
Expand Down
2 changes: 1 addition & 1 deletion apps/verification/src/verification.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ export class VerificationService {
this.emailData.emailFrom = platformConfigData.emailFrom;
this.emailData.emailTo = email;
this.emailData.emailSubject = `${process.env.PLATFORM_NAME} Platform: Verification of Your Credentials`;
this.emailData.emailHtml = await this.outOfBandVerification.outOfBandVerification(email, organizationDetails.name, outOfBandVerificationQrCode);
this.emailData.emailHtml = await this.outOfBandVerification.outOfBandVerification(email, organizationDetails.name, shortenedUrl);
this.emailData.emailAttachments = [
{
filename: 'qrcode.png',
Expand Down
Loading

0 comments on commit baa1f12

Please sign in to comment.