Skip to content

Commit

Permalink
fix: Added validation for schema and cred-def and updated payload for…
Browse files Browse the repository at this point in the history
… create request cred-def (#149)

* fix: Added validation for the schema and cred-def request creation

Signed-off-by: KulkarniShashank <[email protected]>

* Remove commented code

Signed-off-by: KulkarniShashank <[email protected]>

---------

Signed-off-by: KulkarniShashank <[email protected]>
  • Loading branch information
KulkarniShashank authored Oct 13, 2023
1 parent 0cbd12d commit d4d124d
Show file tree
Hide file tree
Showing 4 changed files with 111 additions and 69 deletions.
3 changes: 3 additions & 0 deletions apps/ecosystem/interfaces/ecosystem.interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ export interface CredDefMessage {
schemaId: string;
schema: Record<string, unknown>;
credentialDefinitionRequest: string;
credentialDefinition: Record<string, unknown>;
};
registrationMetadata: Record<string, unknown>;
schemaMetadata: Record<string, unknown>;
Expand Down Expand Up @@ -116,6 +117,8 @@ interface CredentialDefinitionPayload {
tag: string;
issuerId: string;
schemaId: string;
type: string;
value: Record<string, unknown>;
}

export interface submitTransactionPayload {
Expand Down
114 changes: 67 additions & 47 deletions apps/ecosystem/src/ecosystem.repository.ts
Original file line number Diff line number Diff line change
Expand Up @@ -109,19 +109,19 @@ export class EcosystemRepository {
const ecosystemDetails = await this.prisma.ecosystem.findMany({
where: {
ecosystemOrgs: {
some: {
orgId
}
some: {
orgId
}
}
},
include:{
include: {
ecosystemOrgs: {
where: {
orgId
},
include:{
ecosystemRole: true
}
where: {
orgId
},
include: {
ecosystemRole: true
}
}
}
});
Expand Down Expand Up @@ -166,11 +166,11 @@ export class EcosystemRepository {
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
}
}
}


async getEcosystemMembersCount (ecosystemId: string): Promise<number> {
async getEcosystemMembersCount(ecosystemId: string): Promise<number> {
try {
const membersCount = await this.prisma.ecosystem_orgs.count(
{
Expand All @@ -179,24 +179,24 @@ export class EcosystemRepository {
}
}
);
return membersCount;
return membersCount;
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
}
}

async getEcosystemEndorsementsCount (ecosystemId: string): Promise<number> {
async getEcosystemEndorsementsCount(ecosystemId: string): Promise<number> {
try {
const endorsementsCount = await this.prisma.endorsement_transaction.count({
where: {
ecosystemOrgs: {
ecosystemId

}
}
});
return endorsementsCount;
return endorsementsCount;
} catch (error) {
this.logger.error(`error: ${JSON.stringify(error)}`);
throw new InternalServerErrorException(error);
Expand Down Expand Up @@ -385,7 +385,7 @@ export class EcosystemRepository {
},
include: {
ecosystem: true,
ecosystemRole:true
ecosystemRole: true
},
take: pageSize,
skip: (pageNumber - 1) * pageSize,
Expand Down Expand Up @@ -665,6 +665,26 @@ export class EcosystemRepository {
}
}

// eslint-disable-next-line camelcase
async findRecordsByNameAndVersion(name: string, version: string): Promise<endorsement_transaction[]> {
try {
return this.prisma.$queryRaw`SELECT * FROM endorsement_transaction WHERE "requestBody"->>'name' = ${name} AND "requestBody"->>'version' = ${version}`;
} catch (error) {
this.logger.error(`Error in getting ecosystem schema: ${error.message} `);
throw error;
}
}

// eslint-disable-next-line camelcase
async findRecordsByCredDefTag(tag: string): Promise<endorsement_transaction[]> {
try {
return this.prisma.$queryRaw`SELECT * FROM endorsement_transaction WHERE "requestBody"->>'tag' = ${tag}`;
} catch (error) {
this.logger.error(`Error in getting ecosystem credential-definition: ${error.message} `);
throw error;
}
}

async updateTransactionDetails(
endorsementId: string,
schemaTransactionRequest: string
Expand Down Expand Up @@ -732,7 +752,7 @@ export class EcosystemRepository {
throw error;
}
}

// eslint-disable-next-line camelcase
async saveCredDef(credDefResult: saveCredDef): Promise<credential_definition> {
try {
Expand Down Expand Up @@ -771,35 +791,35 @@ export class EcosystemRepository {

async updateEndorsementRequestStatus(ecosystemId: string, endorsementId: string): Promise<object> {
try {
const endorsementTransaction = await this.prisma.endorsement_transaction.findUnique({
where: { id: endorsementId, status: endorsementTransactionStatus.REQUESTED }
});
if (!endorsementTransaction) {
throw new NotFoundException(ResponseMessages.ecosystem.error.EndorsementTransactionNotFoundException);
}
const { ecosystemOrgId } = endorsementTransaction;
const endorsementTransactionEcosystemOrg = await this.prisma.ecosystem_orgs.findUnique({
where: { id: ecosystemOrgId }
});
if (endorsementTransactionEcosystemOrg.ecosystemId === ecosystemId) {
const updatedEndorsementTransaction = await this.prisma.endorsement_transaction.update({
where: { id: endorsementId },
data: {
status: endorsementTransactionStatus.DECLINED
}
});
return updatedEndorsementTransaction;
} else {
throw new NotFoundException(ResponseMessages.ecosystem.error.OrgOrEcosystemNotFoundExceptionForEndorsementTransaction);
}

const endorsementTransaction = await this.prisma.endorsement_transaction.findUnique({
where: { id: endorsementId, status: endorsementTransactionStatus.REQUESTED }
});

if (!endorsementTransaction) {
throw new NotFoundException(ResponseMessages.ecosystem.error.EndorsementTransactionNotFoundException);
}
const { ecosystemOrgId } = endorsementTransaction;

const endorsementTransactionEcosystemOrg = await this.prisma.ecosystem_orgs.findUnique({
where: { id: ecosystemOrgId }
});

if (endorsementTransactionEcosystemOrg.ecosystemId === ecosystemId) {
const updatedEndorsementTransaction = await this.prisma.endorsement_transaction.update({
where: { id: endorsementId },
data: {
status: endorsementTransactionStatus.DECLINED
}
});

return updatedEndorsementTransaction;
} else {
throw new NotFoundException(ResponseMessages.ecosystem.error.OrgOrEcosystemNotFoundExceptionForEndorsementTransaction);
}
} catch (error) {
this.logger.error(`Error in updating endorsement transaction status: ${error.message}`);
throw error;
}
this.logger.error(`Error in updating endorsement transaction status: ${error.message}`);
throw error;
}
}
}
61 changes: 39 additions & 22 deletions apps/ecosystem/src/ecosystem.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ export class EcosystemService {

// eslint-disable-next-line camelcase
async getAllEcosystem(payload: { orgId: string }): Promise<object> {
const getAllEcosystemDetails = await this.ecosystemRepository.getAllEcosystemDetails(payload.orgId);
const getAllEcosystemDetails = await this.ecosystemRepository.getAllEcosystemDetails(payload.orgId);

if (!getAllEcosystemDetails) {
throw new NotFoundException(ResponseMessages.ecosystem.error.update);
}
Expand All @@ -82,15 +82,15 @@ export class EcosystemService {
*
* @returns ecosystem dashboard details
*/
async getEcosystemDashboardDetails(ecosystemId: string): Promise<object> {
try {
return await this.ecosystemRepository.getEcosystemDashboardDetails(ecosystemId);
} catch (error) {
this.logger.error(`In ecosystem dashboard details : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}
async getEcosystemDashboardDetails(ecosystemId: string): Promise<object> {
try {
return await this.ecosystemRepository.getEcosystemDashboardDetails(ecosystemId);
} catch (error) {
this.logger.error(`In ecosystem dashboard details : ${JSON.stringify(error)}`);
throw new RpcException(error.response ? error.response : error);
}

}

/**
* Description: get an ecosystem invitation
* @returns Get sent ecosystem invitation details
Expand Down Expand Up @@ -157,7 +157,7 @@ export class EcosystemService {
*/
async acceptRejectEcosystemInvitations(acceptRejectInvitation: AcceptRejectEcosystemInvitationDto): Promise<string> {
try {

const { orgId, status, invitationId, orgName, orgDid } = acceptRejectInvitation;
const invitation = await this.ecosystemRepository.getEcosystemInvitationById(invitationId);

Expand Down Expand Up @@ -286,6 +286,11 @@ export class EcosystemService {
*/
async requestSchemaEndorsement(requestSchemaPayload: RequestSchemaEndorsement, orgId: number, ecosystemId: string): Promise<object> {
try {
const schemaRequestExist = await this.ecosystemRepository.findRecordsByNameAndVersion(requestSchemaPayload?.name, requestSchemaPayload?.version);

if (0 !== schemaRequestExist.length) {
throw new ConflictException(ResponseMessages.ecosystem.error.schemaAlreadyExist);
}

const ecosystemMemberDetails = await this.ecosystemRepository.getAgentDetails(orgId);
if (!ecosystemMemberDetails) {
Expand Down Expand Up @@ -340,6 +345,13 @@ export class EcosystemService {

async requestCredDeffEndorsement(requestCredDefPayload: RequestCredDeffEndorsement, orgId: number, ecosystemId: string): Promise<object> {
try {

const credDefRequestExist = await this.ecosystemRepository.findRecordsByCredDefTag(requestCredDefPayload?.tag);

if (0 !== credDefRequestExist.length) {
throw new ConflictException(ResponseMessages.ecosystem.error.credDefAlreadyExist);
}

const ecosystemMemberDetails = await this.ecosystemRepository.getAgentDetails(orgId);
if (!ecosystemMemberDetails) {
throw new InternalServerErrorException(ResponseMessages.ecosystem.error.notFound);
Expand Down Expand Up @@ -383,7 +395,8 @@ export class EcosystemService {
throw new InternalServerErrorException(ResponseMessages.ecosystem.error.requestCredDefTransaction);
}

return this.ecosystemRepository.storeTransactionRequest(schemaTransactionResponse, requestCredDefPayload, endorsementTransactionType.CREDENTIAL_DEFINITION);
const requestBody = credDefTransactionRequest.message.credentialDefinitionState.credentialDefinition;
return this.ecosystemRepository.storeTransactionRequest(schemaTransactionResponse, requestBody, endorsementTransactionType.CREDENTIAL_DEFINITION);
} catch (error) {
this.logger.error(`In request cred-def endorsement : ${JSON.stringify(error)}`);

Expand Down Expand Up @@ -562,7 +575,9 @@ export class EcosystemService {
payload.credentialDefinition = {
tag: parsedRequestPayload.operation.tag,
issuerId: ecosystemMemberDetails.orgDid,
schemaId: endorsementTransactionPayload.requestBody['schemaId']
schemaId: endorsementTransactionPayload.requestBody['schemaId'],
type: endorsementTransactionPayload.requestBody['type'],
value: endorsementTransactionPayload.requestBody['value']
};
}

Expand Down Expand Up @@ -602,6 +617,7 @@ export class EcosystemService {
}
return saveSchemaDetails;
} else if (endorsementTransactionPayload.type === endorsementTransactionType.CREDENTIAL_DEFINITION) {

const schemaDetails = await this.ecosystemRepository.getSchemaDetailsById(endorsementTransactionPayload.requestBody['schemaId']);
const saveCredentialDefinition: saveCredDef = {
schemaLedgerId: endorsementTransactionPayload.requestBody['schemaId'],
Expand All @@ -612,6 +628,7 @@ export class EcosystemService {
orgId: ecosystemMemberDetails.orgId,
schemaId: schemaDetails.id
};

const saveCredDefDetails = await this.ecosystemRepository.saveCredDef(saveCredentialDefinition);
if (!saveCredDefDetails) {
throw new InternalServerErrorException(ResponseMessages.ecosystem.error.saveCredDef);
Expand Down Expand Up @@ -752,12 +769,12 @@ export class EcosystemService {
]
};

const ecosystemOrgData = await this.ecosystemRepository.fetchEcosystemOrg(queryEcoOrgs);
const ecosystemOrgData = await this.ecosystemRepository.fetchEcosystemOrg(queryEcoOrgs);

if (ecosystemOrgData['ecosystemRole']['name'] !== EcosystemRoles.ECOSYSTEM_LEAD) {
query.ecosystemOrgs['orgId'] = orgId;
}

if (type) {
query['type'] = type;
}
Expand All @@ -770,13 +787,13 @@ export class EcosystemService {
}


/**
*
* @param ecosystemId
* @param endorsementId
* @param orgId
* @returns EndorsementTransactionRequest Status message
*/
/**
*
* @param ecosystemId
* @param endorsementId
* @param orgId
* @returns EndorsementTransactionRequest Status message
*/

async declineEndorsementRequestByLead(ecosystemId:string, endorsementId:string): Promise<object> {
try {
Expand Down
2 changes: 2 additions & 0 deletions libs/common/src/response-messages/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -214,6 +214,8 @@ export const ResponseMessages = {
requestCredDefTransaction: 'Error while submitting transaction',
notFound: 'Organization not found',
leadNotFound: 'Lead details not found',
schemaAlreadyExist: 'Schema name and schema version already exist',
credDefAlreadyExist: 'Credential definition already exist',
saveSchema: 'Error while storing the schema details',
saveCredDef: 'Error while storing the credential-definition details',
invalidOrgId: 'Invalid organization Id',
Expand Down

0 comments on commit d4d124d

Please sign in to comment.