From fc420ad8a6a01a7fd18ce7ba99dea3ee13291e17 Mon Sep 17 00:00:00 2001 From: Dariy Miseldzhani Date: Sat, 21 Dec 2024 03:09:28 +0100 Subject: [PATCH] Simplify logic and improve readability. --- api-gateway/src/api/service/contract.ts | 17 +- api-gateway/src/helpers/guardians.ts | 11 +- .../messages-report-block.component.ts | 51 ++---- .../report-block/report-block.component.html | 159 +++++++++--------- .../report-block/report-block.component.scss | 29 ---- .../report-block/report-block.component.ts | 22 ++- frontend/src/app/services/contract.service.ts | 9 +- guardian-service/src/api/contract.service.ts | 88 +--------- interfaces/src/type/messages/workers.type.ts | 4 +- 9 files changed, 116 insertions(+), 274 deletions(-) diff --git a/api-gateway/src/api/service/contract.ts b/api-gateway/src/api/service/contract.ts index 828b5aaab1..2fad5fae62 100644 --- a/api-gateway/src/api/service/contract.ts +++ b/api-gateway/src/api/service/contract.ts @@ -1740,19 +1740,6 @@ export class ContractsApi { required: true, example: '0.0.0000000', }) - @ApiQuery({ - name: 'pageIndex', - type: Number, - description: 'The number of pages to skip before starting to collect the result set', - required: false, - example: 0, - }) - @ApiQuery({ - name: 'pageSize', - type: Number, - description: 'The numbers of items to return', - example: 20, - }) @ApiOkResponse({ description: 'Successful operation.', isArray: true, @@ -1774,13 +1761,11 @@ export class ContractsApi { @AuthUser() user: IAuthUser, @Response() res: any, @Query('contractTopicId') contractTopicId: string, - @Query('pageIndex') pageIndex?: number, - @Query('pageSize') pageSize?: number, ): Promise { try { const owner = new EntityOwner(user); const guardians = new Guardians(); - const [vcs, count] = await guardians.getRetireVCsFromIndexer(owner, contractTopicId, pageIndex, pageSize); + const [vcs, count] = await guardians.getRetireVCsFromIndexer(owner, contractTopicId); return res.header('X-Total-Count', count).send(vcs); } catch (error) { await InternalException(error, this.logger); diff --git a/api-gateway/src/helpers/guardians.ts b/api-gateway/src/helpers/guardians.ts index b344b2fa5c..0e8508011f 100644 --- a/api-gateway/src/helpers/guardians.ts +++ b/api-gateway/src/helpers/guardians.ts @@ -1851,21 +1851,16 @@ export class Guardians extends NatsService { /** * Get retire VCs from Indexer * @param owner - * @param pageIndex - * @param pageSize + * @param contractTopicId * @returns Retire VCs from Indexer and count */ public async getRetireVCsFromIndexer( owner: IOwner, - contractTopicId: string, - pageIndex?: any, - pageSize?: any + contractTopicId: string ): Promise<[IRetirementMessage[], number]> { return await this.sendMessage(ContractAPI.GET_RETIRE_VCS_FROM_INDEXER, { owner, - contractTopicId, - pageIndex, - pageSize, + contractTopicId }); } diff --git a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/messages-report-block/messages-report-block.component.ts b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/messages-report-block/messages-report-block.component.ts index 3ba55a54c4..cf07f7a590 100644 --- a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/messages-report-block/messages-report-block.component.ts +++ b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/messages-report-block/messages-report-block.component.ts @@ -264,6 +264,14 @@ export class MessagesReportBlockComponent implements OnInit { ); private lines!: Line[] | null; + gridSize: number = 0; + + mintTokenId: string; + mintTokenSerials: string[] = []; + groupedByContractRetirements: any = []; + indexerAvailable: boolean = false; + retirementMessages: any[] = []; + constructor( private element: ElementRef, private fb: UntypedFormBuilder, @@ -374,16 +382,13 @@ export class MessagesReportBlockComponent implements OnInit { }, 100); } - - gridSize: number = 0; - - mintTokenId: string; - mintTokenSerials: string[] = []; - groupedByContractRetirements: any = []; - indexerAvailable: boolean = false; - retirementMessages: any[] = []; - private loadRetirementMessages() { + this._messages2.forEach(message => { + if (message.__ifMintMessage) { + this.mintTokenId = message.__tokenId; + } + }); + this.contractService .getContracts({ type: ContractType.RETIRE @@ -415,25 +420,6 @@ export class MessagesReportBlockComponent implements OnInit { this.loading = false; const retires = results.map((item: any) => item.body) - // const tokenRetires = retires.map((retirements: IRetirementMessage[]) => { - // const ret = retirements.filter((item: IRetirementMessage) => item.documents[0].credentialSubject.some((subject: any) => - // subject.tokens.some((token: any) => - // token.tokenId === this.mintTokenId - // && token.serials.some((serial: string) => this.mintTokenSerials.includes(serial) - // )))); - // return ret - // }); - - // this.groupedByContractRetirements = Array.from( - // new Map(allRetireMessages - // .map((item: any) => [item.documents[0].credentialSubject[0].contractId, []]) - // )).map(([contractId, documents]) => ({ - // contractId, - // selectedItemIndex: 0, - // __ifRetireMessage: true, - // documents: allRetireMessages.filter((item: any) => item.documents[0].credentialSubject[0].contractId === contractId) - // })) - let allRetireMessages: any = []; retires.forEach((retirements: any[]) => { retirements.forEach((item: any) => { @@ -465,6 +451,7 @@ export class MessagesReportBlockComponent implements OnInit { lastOrderMessageTopic2++; }); + // Todo: Need filtration by serials and token user this.retirementMessages = [...allRetireMessages]; this._gridTemplateColumns1 = 'repeat(' + (this.gridSize + this.retirementMessages.length + 1) + ', 230px)'; @@ -498,12 +485,6 @@ export class MessagesReportBlockComponent implements OnInit { } } - this._messages2.forEach(message => { - if (message.__ifMintMessage) { - this.mintTokenId = message.__tokenId; - } - }); - this.gridSize = 0; this._messages2.sort((a, b) => a.__order > b.__order ? 1 : -1); for (let index = 0; index < this._messages2.length; index++) { @@ -925,7 +906,7 @@ export class MessagesReportBlockComponent implements OnInit { styleClass: 'guardian-dialog', data: { row: null, - document: message.document || message.documents[0], + document: message.document || message.documents?.[0], title: 'VC Document', type: 'VC', viewDocument: true, diff --git a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.html b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.html index 4cbfa6d1bd..ee11404055 100644 --- a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.html +++ b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.html @@ -394,94 +394,95 @@

Policy Overview

- - -
-
-
-
-
-
- {{ getRetirementGroupVC(group).issuanceDate | date : 'YYYY-MM-dd, hh:mm:ss' }} + + +
+
+
+
+
+
+ {{ selectedVC.issuanceDate | date : 'YYYY-MM-dd, hh:mm:ss' }} +
+
+ +
-
- +
+
+

Token {{ selectedVC.credentialSubject[0]?.tokens[0].tokenId }}

+

Instance ID {{ selectedVC.credentialSubject[0]?.tokens[0].serials.join(', ') }}

+

Amount {{ selectedVC.credentialSubject[0]?.tokens[0].count }}

+ +

Transaction + {{selectedVC.timestamp}} +

+

Transaction -

+ +

User ID {{ selectedVC.credentialSubject[0]?.user }}

+
+
+

Token {{ selectedVC.credentialSubject[0]?.tokens[1].tokenId }}

+

Instance ID {{ selectedVC.credentialSubject[0]?.tokens[1].serials.join(', ') }}

+

Amount {{ selectedVC.credentialSubject[0]?.tokens[1].count }}

+
-
-
-
-

Token {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].tokenId }}

-

Instance ID {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].serials.join(', ') }}

-

Amount {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].count }}

- -

Transaction - {{getRetirementGroupVC(group).timestamp}} -

-

Transaction -

- -

User ID {{ getRetirementGroupVC(group).credentialSubject[0]?.user }}

+ +
+ chevron_left + + {{ (group.selectedItemIndex + 1) }} of {{ group.documents.length }} + + chevron_right
-
-

Token {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[1].tokenId }}

-

Instance ID {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[1].serials.join(', ') }}

-

Amount {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[1].count }}

-
-
- -
- chevron_left - - {{ (group.selectedItemIndex + 1) }} of {{ group.documents.length }} - - chevron_right
-
- - -
-
-
-
- {{ getRetirementGroupVC(group).issuanceDate | date : 'YYYY-MM-dd, hh:mm:ss' }} + + +
+
+
+
+ {{ selectedVC.issuanceDate | date : 'YYYY-MM-dd, hh:mm:ss' }} +
+
+ +
-
- +
+

Token {{ selectedVC.credentialSubject[0]?.tokens[0].tokenId }}

+

Instance ID {{ selectedVC.credentialSubject[0]?.tokens[0].serials.join(', ') }}

+

Amount {{ selectedVC.credentialSubject[0]?.tokens[0].count }}

+

Transaction {{ selectedVC.timestamp }}

+

User ID {{ selectedVC.credentialSubject[0]?.user }}

+
+ +
+ chevron_left + + {{ (group.selectedItemIndex + 1) }} of {{ group.documents.length }} + + chevron_right
-
-
-

Token {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].tokenId }}

-

Instance ID {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].serials.join(', ') }}

-

Amount {{ getRetirementGroupVC(group).credentialSubject[0]?.tokens[0].count }}

-

Transaction {{ getRetirementGroupVC(group).timestamp }}

-

User ID {{ getRetirementGroupVC(group).credentialSubject[0]?.user }}

-
- -
- chevron_left - - {{ (group.selectedItemIndex + 1) }} of {{ group.documents.length }} - - chevron_right
-
-
+ +
diff --git a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.scss b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.scss index ff4b18f265..d58fa479b4 100644 --- a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.scss +++ b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.scss @@ -784,10 +784,6 @@ h3 { column-gap: 16px } - - - - .item-type-RETIRE { .chain-top-block { padding-bottom: 14px; @@ -807,31 +803,6 @@ h3 { } } -.retirement-container { - padding: 24px; - border-radius: 8px; - border: 1px solid var(--grey-grey-3, #E1E7EF); - background: var(--grey-white, #FFF); -} - -.retirement-items-wrapper { - display: flex; - gap: 16px; -} - -.retirement-item { - display: grid; - grid-template-columns: repeat(2, 1fr); - column-gap: 8px; - max-width: 320px; - margin-top: 16px; - padding: 8px; - border-radius: 4px; - border: 1px solid var(--grey-grey-3, #E1E7EF); - background: var(--grey-white, #FFF); -} - - .retirements-container { position: relative; display: flex; diff --git a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.ts b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.ts index 941014b8bb..7afbde9b35 100644 --- a/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.ts +++ b/frontend/src/app/modules/policy-engine/policy-viewer/blocks/report-block/report-block.component.ts @@ -61,6 +61,12 @@ export class ReportBlockComponent implements OnInit { value: ['', Validators.required], }); + vpDocument: any; + mintTokenId: string; + mintTokenSerials: string[] = []; + groupedByContractRetirements: any = []; + indexerAvailable: boolean = false; + constructor( private policyEngineService: PolicyEngineService, private wsService: WebSocketService, @@ -103,13 +109,7 @@ export class ReportBlockComponent implements OnInit { } } - // Testing - vpDocument: any; - mintTokenId: string; - mintTokenSerials: string[] = []; - groupedByContractRetirements: any = []; - indexerAvailable: boolean = false; - private loadRetireData() { + loadRetireData() { this.loading = true; this.contractService @@ -179,7 +179,7 @@ export class ReportBlockComponent implements OnInit { this.groupedByContractRetirements = Array.from( new Map(tokenRetirementDocuments .map((item: any) => [item.credentialSubject[0].contractId, []]) - )).map(([contractId, documents]) => ({ + )).map(([contractId]) => ({ contractId, selectedItemIndex: 0, documents: tokenRetirementDocuments.filter((item: any) => item.credentialSubject[0].contractId === contractId) @@ -202,7 +202,7 @@ export class ReportBlockComponent implements OnInit { this.groupedByContractRetirements = Array.from( new Map(tokenRetirementDocuments .map((item: any) => [item.credentialSubject[0].contractId, []]) - )).map(([contractId, documents]) => ({ + )).map(([contractId]) => ({ contractId, selectedItemIndex: 0, documents: tokenRetirementDocuments.filter((item: any) => item.credentialSubject[0].contractId === contractId) @@ -223,7 +223,7 @@ export class ReportBlockComponent implements OnInit { ); } - getRetirementGroupVC(group: any): any { + getSelectedRetirementVC(group: any): any { return group.documents[group.selectedItemIndex]; } @@ -239,8 +239,6 @@ export class ReportBlockComponent implements OnInit { group.selectedItemIndex = (group.selectedItemIndex - 1) >= 0 ? (group.selectedItemIndex - 1) : (group.documents.length - 1); } - // ... - loadData() { this.loading = true; if (this.static) { diff --git a/frontend/src/app/services/contract.service.ts b/frontend/src/app/services/contract.service.ts index 0f85024ee8..95a3992ee8 100644 --- a/frontend/src/app/services/contract.service.ts +++ b/frontend/src/app/services/contract.service.ts @@ -16,7 +16,7 @@ import { export class ContractService { private readonly url: string = `${API_BASE_URL}/contracts`; - constructor(private http: HttpClient) {} + constructor(private http: HttpClient) { } //#region Common contract endpoints public getContracts(params: { @@ -320,14 +320,9 @@ export class ContractService { } public getRetireVCsFromIndexer( - contractTopicId: string, - pageIndex?: number, - pageSize?: number + contractTopicId: string ): Observable> { let url = `${this.url}/retireIndexer?contractTopicId=${contractTopicId}`; - if (pageIndex && pageSize) { - url += `?pageIndex=${pageIndex}&pageSize=${pageSize}` - } return this.http.get(url, { observe: 'response', }); diff --git a/guardian-service/src/api/contract.service.ts b/guardian-service/src/api/contract.service.ts index afc470fe03..83e56fae66 100644 --- a/guardian-service/src/api/contract.service.ts +++ b/guardian-service/src/api/contract.service.ts @@ -3411,109 +3411,25 @@ export async function contractAPI( ApiResponse(ContractAPI.GET_RETIRE_VCS_FROM_INDEXER, async (msg: { owner: IOwner, - contractTopicId: string, - pageIndex?: any, - pageSize?: any + contractTopicId: string }) => { try { if (!msg) { return new MessageError('Invalid get contract parameters'); } - const { owner, contractTopicId, pageIndex, pageSize } = msg; + const { owner, contractTopicId } = msg; if (!owner.creator) { throw new Error('Owner is required'); } - const otherOptions: any = {}; - const _pageSize = parseInt(pageSize, 10); - const _pageIndex = parseInt(pageIndex, 10); - if (Number.isInteger(_pageSize) && Number.isInteger(_pageIndex)) { - otherOptions.orderBy = { createDate: 'DESC' }; - otherOptions.limit = Math.min(100, _pageSize); - otherOptions.offset = _pageIndex * _pageSize; - } else { - otherOptions.orderBy = { createDate: 'DESC' }; - otherOptions.limit = 100; - } - - const users = new Users(); - const user = await users.getUserById(owner.creator); - - const filters: any = { - owner: owner.owner, - type: SchemaEntity.RETIRE_TOKEN, - }; - if (user.role === UserRole.USER) { - filters['document.credentialSubject.user'] = - user.hederaAccountId; - } - - // Get Retirements from Indexer const messages = await new Workers().addNonRetryableTask({ type: WorkerTaskType.ANALYTICS_GET_RETIRE_DOCUMENTS, data: { payload: { options: { topicId: contractTopicId } } } }, 2); - - // Save to Guardian DB - // const filtersOld: any = { - // owner: owner.owner, - // type: SchemaEntity.RETIRE_TOKEN, - // }; - // if (user.role === UserRole.USER) { - // filters['document.credentialSubject.user'] = - // user.hederaAccountId; - // } - - // const oldRetirements = await dataBaseServer.findAndCount(VcDocument, filtersOld) // find old Retirement VCs - // console.log(JSON.stringify(oldRetirements, null, 4)); - - // retirements.forEach(retirement => { - // oldRetirements.forEach(oldRetirement => { - - // }); - - // console.log(retirement); - - // }); - - // const vcMessage = new VCMessage(MessageAction.CreateVC); - // console.log(vcMessage.hash); - - // vcMessage.setDocument(vcObject); - - // await dataBaseServer.save(VcDocumentCollection, { - // hash: vcMessage.hash, - // owner: owner.creator, - // document: vcMessage.document, - // type: schemaObject?.entity, - // documentFields: ['credentialSubject.0.user'], - // }); - - // await saveRetireVC( - // // contractRepository, - // dataBaseServer, - // pool.contractId, - // srUser, - // sr.hederaAccountId, - // srKey, - // root.hederaAccountId, - // tokens.map((token) => { - // const newToken: any = { - // ...token, - // }; - // const poolToken = pool.tokens.find( - // // tslint:disable-next-line:no-shadowed-variable - // (poolToken) => (poolToken.token = token.token) - // ); - // newToken.decimals = poolToken.decimals; - // return newToken; - // }) - // ); - // ... return new MessageResponse([messages, messages.length]); } catch (error) { diff --git a/interfaces/src/type/messages/workers.type.ts b/interfaces/src/type/messages/workers.type.ts index f1831c3055..68f623c540 100644 --- a/interfaces/src/type/messages/workers.type.ts +++ b/interfaces/src/type/messages/workers.type.ts @@ -37,9 +37,9 @@ export enum WorkerTaskType { GET_TOKEN_INFO = 'get-token-info', GET_CONTRACT_EVENTS = 'get-contract-events', GET_TRANSACTIONS = 'get-transaction', - ANALYTICS_SEARCH_POLICIES= 'analytics-search-policies', + ANALYTICS_SEARCH_POLICIES = 'analytics-search-policies', ANALYTICS_GET_INDEXER_AVAILABILITY = "analytics-get-indexer-availability", - ANALYTICS_GET_RETIRE_DOCUMENTS= 'analytics-get-retire-documents' + ANALYTICS_GET_RETIRE_DOCUMENTS = 'analytics-get-retire-documents' } /**