Skip to content

Commit

Permalink
fix: added the validation for the delete wallet and first check on th…
Browse files Browse the repository at this point in the history
…e record for delete wallet

Signed-off-by: KulkarniShashank <[email protected]>
  • Loading branch information
KulkarniShashank committed Sep 11, 2024
1 parent bb04236 commit 0de20e4
Show file tree
Hide file tree
Showing 2 changed files with 405 additions and 401 deletions.
117 changes: 60 additions & 57 deletions apps/agent-service/src/agent-service.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ import { ledgerName } from '@credebl/common/cast.helper';
import { InvitationMessage } from '@credebl/common/interfaces/agent-service.interface';
import * as CryptoJS from 'crypto-js';
import { UserActivityRepository } from 'libs/user-activity/repositories';
import { PrismaService } from '@credebl/prisma-service';

@Injectable()
@WebSocketGateway()
Expand All @@ -82,6 +83,7 @@ export class AgentServiceService {

constructor(
private readonly agentServiceRepository: AgentServiceRepository,
private readonly prisma: PrismaService,
private readonly commonService: CommonService,
private readonly connectionService: ConnectionService,
@Inject('NATS_CLIENT') private readonly agentServiceProxy: ClientProxy,
Expand Down Expand Up @@ -1729,76 +1731,77 @@ export class AgentServiceService {

async deleteWallet(orgId: string, user: user): Promise<object> {
try {
// Retrieve the API key and agent information
const [getApiKeyResult, orgAgentResult] = await Promise.allSettled([
this.getOrgAgentApiKey(orgId),
this.agentServiceRepository.getAgentApiKey(orgId)
]);

if (getApiKeyResult.status === 'rejected') {
throw new InternalServerErrorException(`Failed to get API key: ${getApiKeyResult.reason}`);
}

if (orgAgentResult.status === 'rejected') {
throw new InternalServerErrorException(`Failed to get agent information: ${orgAgentResult.reason}`);
}
// Retrieve the API key and agent information
const [getApiKeyResult, orgAgentResult] = await Promise.allSettled([
this.getOrgAgentApiKey(orgId),
this.agentServiceRepository.getAgentApiKey(orgId)
]);

if (getApiKeyResult.status === 'rejected') {
throw new InternalServerErrorException(`Failed to get API key: ${getApiKeyResult.reason}`);
}

const getApiKey = getApiKeyResult?.value;
const orgAgent = orgAgentResult?.value;
if (orgAgentResult.status === 'rejected') {
throw new InternalServerErrorException(`Failed to get agent information: ${orgAgentResult.reason}`);
}

const orgAgentTypeResult = await this.agentServiceRepository.getOrgAgentType(orgAgent.orgAgentTypeId);
const getApiKey = getApiKeyResult?.value;
const orgAgent = orgAgentResult?.value;

if (!orgAgentTypeResult) {
throw new NotFoundException(ResponseMessages.agent.error.orgAgentNotFound);
}
const orgAgentTypeResult = await this.agentServiceRepository.getOrgAgentType(orgAgent.orgAgentTypeId);

// Determine the URL based on the agent type
const url =
orgAgentTypeResult.agent === OrgAgentType.SHARED
? `${orgAgent.agentEndPoint}${CommonConstants.URL_SHAGENT_DELETE_SUB_WALLET}`.replace('#', orgAgent?.tenantId)
: `${orgAgent.agentEndPoint}${CommonConstants.URL_DELETE_WALLET}`;
if (!orgAgentTypeResult) {
throw new NotFoundException(ResponseMessages.agent.error.orgAgentNotFound);
}

// Make the HTTP DELETE request
const deleteWallet = await this.commonService
.httpDelete(url, {
headers: { authorization: getApiKey }
})
.then(async (response) => response);
// Determine the URL based on the agent type
const url =
orgAgentTypeResult.agent === OrgAgentType.SHARED
? `${orgAgent.agentEndPoint}${CommonConstants.URL_SHAGENT_DELETE_SUB_WALLET}`.replace('#', orgAgent?.tenantId)
: `${orgAgent.agentEndPoint}${CommonConstants.URL_DELETE_WALLET}`;

if (deleteWallet.status === HttpStatus.NO_CONTENT) {
const {orgDid, agentInvitation, deleteOrgAgent} = await this.agentServiceRepository.deleteOrgAgentByOrg(orgId);
// Perform the deletion in a transaction
return await this.prisma.$transaction(async (prisma) => {
// Delete org agent and related records
const { orgDid, agentInvitation, deleteOrgAgent } = await this.agentServiceRepository.deleteOrgAgentByOrg(orgId);

this.logger.log(`orgDid :::: ${JSON.stringify(orgDid)}`);
this.logger.log(`agentInvitation :::: ${JSON.stringify(agentInvitation)}`);
this.logger.log(`deleteOrgAgent :::: ${JSON.stringify(deleteOrgAgent)}`);
// Make the HTTP DELETE request
const deleteWallet = await this.commonService.httpDelete(url, {
headers: { authorization: getApiKey }
});

const deletions = [
{ records: orgDid.count, tableName: 'org_dids' },
{ records: agentInvitation.count, tableName: 'agent_invitations' },
{ records: deleteOrgAgent ? 1 : 0, tableName: 'org_agents' }
];
if (deleteWallet.status !== HttpStatus.NO_CONTENT) {
throw new InternalServerErrorException(ResponseMessages.agent.error.walletNotDeleted);
}

const logDeletionActivity = async (records, tableName): Promise<void> => {
if (records) {
const txnMetadata = {
deletedRecordsCount: records,
deletedRecordInTable: tableName
const deletions = [
{ records: orgDid.count, tableName: 'org_dids' },
{ records: agentInvitation.count, tableName: 'agent_invitations' },
{ records: deleteOrgAgent ? 1 : 0, tableName: 'org_agents' }
];

const logDeletionActivity = async (records, tableName): Promise<void> => {
if (records) {
const txnMetadata = {
deletedRecordsCount: records,
deletedRecordInTable: tableName
};
const recordType = RecordType.WALLET;
await this.userActivityRepository._orgDeletedActivity(orgId, user, txnMetadata, recordType);
}
};
const recordType = RecordType.WALLET;
await this.userActivityRepository._orgDeletedActivity(orgId, user, txnMetadata, recordType);
}
};

for (const { records, tableName } of deletions) {
await logDeletionActivity(records, tableName);
}
return deleteOrgAgent;
}
for (const { records, tableName } of deletions) {
await logDeletionActivity(records, tableName);
}

return deleteOrgAgent;
});
} catch (error) {
this.logger.error(`Error in delete wallet in agent service: ${JSON.stringify(error.message)}`);
throw new RpcException(error.response ? error.response : error);
this.logger.error(`Error in delete wallet in agent service: ${JSON.stringify(error.message)}`);
throw new RpcException(error.response ? error.response : error);
}
}
}

async receiveInvitationUrl(receiveInvitationUrl: IReceiveInvitationUrl, url: string, orgId: string): Promise<string> {
try {
Expand Down
Loading

0 comments on commit 0de20e4

Please sign in to comment.