Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <stepan.kiryakov@envisionblockchain.com>
  • Loading branch information
Stepan-Kirjakov committed Feb 17, 2025
1 parent 79e2cb5 commit 3457463
Show file tree
Hide file tree
Showing 11 changed files with 131 additions and 85 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@
<div class="guardian-loading-image"></div>
</div>
<div>
<div class="form-input-container">
<div class="guardian-input-container">
<label class="form-label" htmlFor="version">* Version</label>
<input [formControl]="versionControl" id="version" pInputText type="text"/>
</div>
Expand All @@ -31,7 +31,6 @@
0 test data file[s]
</div>
</ng-template>

</div>
</div>
</div>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,10 @@
}

.dialog-header {
.header-text {
min-width: 190px;
}

.header-item {
padding: 6px 12px;
margin: 0px 16px;
Expand All @@ -13,6 +17,9 @@
background: var(--guardian-dry-run-background);
user-select: none;
cursor: default;
max-width: 275px;
overflow: hidden;
text-overflow: ellipsis;
}
}

Expand All @@ -26,6 +33,10 @@
margin-right: 0px !important;
}

.guardian-input-container {
margin-bottom: 24px;
}

.form-input-container {
display: flex;
flex-direction: column;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -990,7 +990,7 @@

.cell-description {
min-width: 150px;
max-width: calc(100vw - 970px);
max-width: calc(100vw - 1080px);
}

.submenu-arrow {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,7 @@
<app-document-view
[policyId]="policyId"
[document]="item"
[formulas]="formulas"
[dryRun]="dryRun"
[hide-fields]="hideFields"
type="VC">
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit, } from '@angular/core';
import { ChangeDetectionStrategy, ChangeDetectorRef, Component, Input, OnInit, SimpleChanges, } from '@angular/core';
import { DocumentValidators, Schema, SchemaRuleValidateResult } from '@guardian/interfaces';
import { forkJoin, Subject } from 'rxjs';
import { takeUntil } from 'rxjs/operators';
Expand All @@ -19,6 +19,7 @@ import { FormulasTree } from '../../formulas/models/formula-tree';
export class DocumentViewComponent implements OnInit {
@Input('getByUser') getByUser: boolean = false;
@Input('document') document: any;
@Input('formulas') formulas: FormulasTree | null;
@Input('hide-fields') hideFields!: { [x: string]: boolean };
@Input('type') type!: 'VC' | 'VP';
@Input('schema') schema!: any;
Expand All @@ -39,7 +40,6 @@ export class DocumentViewComponent implements OnInit {
public schemaMap: { [x: string]: Schema | null } = {};
public rules: DocumentValidators;
public rulesResults: SchemaRuleValidateResult;
public formulas: FormulasTree | null;
public formulasResults: any | null;

private destroy$: Subject<boolean> = new Subject<boolean>();
Expand Down Expand Up @@ -86,95 +86,112 @@ export class DocumentViewComponent implements OnInit {
this.subjects.push(this.document.verifiableCredential);
}
}
if (this.type === 'VC') {
this.loadSchema();
}
this.loadData();
}

ngOnChanges(changes: SimpleChanges): void {
this.formulasResults = this.formulas?.getFields(this.schemaId);
}

ngOnDestroy() {
this.destroy$.next(true);
this.destroy$.unsubscribe();
}

private loadSchema() {
for (const credentialSubject of this.subjects) {
const type: string = credentialSubject.type;
if (!this.schemaMap[type]) {
this.schemaMap[type] = null;
}
if (!this.schemaId) {
this.schemaId = `#${type}`;
private loadData() {
const requests: any = {};

//Load Schemas
if (this.type === 'VC') {
const schemas: any[] = [];
for (const credentialSubject of this.subjects) {
const type: string = credentialSubject.type;
if (!this.schemaMap[type]) {
this.schemaMap[type] = null;
}
if (!this.schemaId) {
this.schemaId = `#${type}`;
}
}
}
const requests: any[] = [];
for (const [type, schema] of Object.entries(this.schemaMap)) {
if (!schema) {
if (this.getByUser) {
requests.push(
this.schemaService
.getSchemasByTypeAndUser(type)
.pipe(takeUntil(this.destroy$))
)
} else {
requests.push(
this.schemaService
.getSchemasByType(type)
.pipe(takeUntil(this.destroy$))
)
for (const [type, schema] of Object.entries(this.schemaMap)) {
if (!schema) {
if (this.getByUser) {
schemas.push(
this.schemaService
.getSchemasByTypeAndUser(type)
.pipe(takeUntil(this.destroy$))
)
} else {
schemas.push(
this.schemaService
.getSchemasByType(type)
.pipe(takeUntil(this.destroy$))
)
}
}
}
for (let i = 0; i < schemas.length; i++) {
requests[i] = schemas[i];
}
}

requests.push(
this.schemaRulesService
//Load Rules
if (this.type === 'VC') {
requests.rules = this.schemaRulesService
.getSchemaRuleData({
policyId: this.policyId,
schemaId: this.schemaId,
documentId: this.documentId
})
.pipe(takeUntil(this.destroy$))
)
.pipe(takeUntil(this.destroy$));
}

requests.push(
this.formulasService
//Load Formulas
if (this.documentId) {
requests.formulas = this.formulasService
.getFormulasData({
policyId: this.policyId,
schemaId: this.schemaId,
documentId: this.documentId
})
.pipe(takeUntil(this.destroy$))
)
.pipe(takeUntil(this.destroy$));
}


this.loading = true;
forkJoin(requests).subscribe((results: any[]) => {
const formulas = results.pop();
const rules = results.pop();
forkJoin(requests).subscribe((results: any) => {
//Load Rules
if (results.rules) {
const rules = results.rules;
this.rules = new DocumentValidators(rules);
this.rulesResults = this.rules.validateVC(this.schemaId, this.document);
delete results.rules;
}

for (const result of results) {
if (result) {
//Load Formulas
if (results.formulas) {
const formulas = results.formulas;
this.formulas = FormulasTree.from(formulas);
this.formulas?.setDocuments(this.document);
this.formulasResults = this.formulas?.getFields(this.schemaId);
delete results.formulas;
}

//Load Schemas
for (const schema of Object.values<any>(results)) {
if (schema) {
try {
let type = (result.iri || '');
let type = (schema.iri || '');
if (type.startsWith('#')) {
type = type.substr(1);
}
this.schemaMap[type] = new Schema(result);
this.schemaMap[type] = new Schema(schema);
} catch (error) {
console.error(error);
}
}
}

this.rules = new DocumentValidators(rules);
this.rulesResults = this.rules.validateVC(this.schemaId, this.document);

if (formulas && !formulas.document) {
formulas.document = { document: this.document };
}

this.formulas = FormulasTree.from(formulas);
this.formulas?.setDocuments(this.document);
this.formulasResults = this.formulas?.getFields(this.schemaId);

setTimeout(() => {
this.loading = false;
this.ref.detectChanges();
Expand All @@ -183,6 +200,7 @@ export class DocumentViewComponent implements OnInit {
this.loading = false;
this.ref.detectChanges();
});

}

getItemsPage(items: any[]) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,6 @@ export class VCViewerDialog {
getByUser
} = this.dialogConfig.data;


this.policyId = row?.policyId;
this.documentId = row?.id;
this.schemaId = row?.schema;
Expand Down
30 changes: 19 additions & 11 deletions guardian-service/src/api/helpers/formulas-helpers.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,31 @@
import { DatabaseServer, Formula, FormulaImportExport, FormulaMessage, MessageAction, MessageServer, TopicConfig, VcDocument } from '@guardian/common';
import { DatabaseServer, Formula, FormulaImportExport, FormulaMessage, MessageAction, MessageServer, TopicConfig, VcDocument, VpDocument } from '@guardian/common';
import { EntityStatus, IOwner, IRootConfig } from '@guardian/interfaces';
import { INotifier } from '../../helpers/notifier.js';

async function findRelationships(target: VcDocument): Promise<VcDocument[]> {
type IDocument = VcDocument | VpDocument;

async function findRelationships(target: IDocument): Promise<IDocument[]> {
if (!target) {
return [];
}

const prevRelationships = new Map<string, VcDocument>();
const prevRelationships = new Map<string, IDocument>();
prevRelationships.set(target.messageId, target);

await addRelationships(target, prevRelationships);

return Array.from(prevRelationships.values());
}

async function addRelationships(doc: VcDocument, relationships: Map<string, VcDocument>) {
async function addRelationships(doc: IDocument, relationships: Map<string, IDocument>) {
if (doc && doc.relationships) {
for (const id of doc.relationships) {
await addRelationship(id, relationships);
}
}
}

async function addRelationship(messageId: string, relationships: Map<string, VcDocument>) {
async function addRelationship(messageId: string, relationships: Map<string, IDocument>) {
if (!messageId || relationships.has(messageId)) {
return;
}
Expand All @@ -48,18 +50,24 @@ export async function getFormulasData(
const { policyId, documentId, parentId } = option;

const result: {
document: VcDocument | null,
relationships: VcDocument[]
document: IDocument | null,
relationships: IDocument[]
} = {
document: null,
relationships: []
}

if (documentId) {
const doc = await DatabaseServer.getVCById(documentId);
if (doc) {
result.document = doc;
result.relationships = await findRelationships(doc);
const vc = await DatabaseServer.getVCById(documentId);
if (vc) {
result.document = vc;
result.relationships = await findRelationships(vc);
} else {
const vp = await DatabaseServer.getVPById(documentId);
if (vp) {
result.document = vp;
result.relationships = await findRelationships(vp);
}
}
}

Expand Down
16 changes: 12 additions & 4 deletions guardian-service/src/api/helpers/schema-publish-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ export async function saveSchemas(
export async function publishDefsSchemas(
defs: any,
user: IOwner,
root: IRootConfig
root: IRootConfig,
schemaMap: Map<string, string> | null
) {
if (!defs) {
return;
Expand All @@ -167,7 +168,7 @@ export async function publishDefsSchemas(
});
if (schema && schema.status !== SchemaStatus.PUBLISHED) {
schema = await incrementSchemaVersion(schema.iri, user);
await findAndPublishSchema(schema.id, schema.version, user, root, emptyNotifier());
await findAndPublishSchema(schema.id, schema.version, user, root, emptyNotifier(), schemaMap);
}
}
}
Expand All @@ -185,7 +186,8 @@ export async function findAndPublishSchema(
version: string,
user: IOwner,
root: IRootConfig,
notifier: INotifier
notifier: INotifier,
schemaMap: Map<string, string> | null
): Promise<SchemaCollection> {
notifier.start('Load schema');

Expand All @@ -201,7 +203,7 @@ export async function findAndPublishSchema(

notifier.completedAndStart('Publishing related schemas');
const oldSchemaIri = item.iri;
await publishDefsSchemas(item.document?.$defs, user, root);
await publishDefsSchemas(item.document?.$defs, user, root, schemaMap);
item = await DatabaseServer.getSchema(id);

notifier.completedAndStart('Resolve topic');
Expand All @@ -220,6 +222,12 @@ export async function findAndPublishSchema(
await updateSchemaDocument(item);
await updateSchemaDefs(item.iri, oldSchemaIri);
notifier.completed();

if (schemaMap) {
const newSchemaIri = item.iri;
schemaMap.set(oldSchemaIri, newSchemaIri);
}

return item;
}

Expand Down
Loading

0 comments on commit 3457463

Please sign in to comment.