From 9d42d7e271654a167a5d353de2d712f8587793f9 Mon Sep 17 00:00:00 2001 From: Stepan Kiryakov Date: Mon, 30 Dec 2024 16:51:06 +0400 Subject: [PATCH] fix Signed-off-by: Stepan Kiryakov --- common/src/import-export/policy-label.ts | 108 +++++++++++++++++- ...abel-document-configuration.component.scss | 1 + .../policy-label-document-view.component.scss | 1 + frontend/src/app/services/auth.service.ts | 1 - .../src/api/policy-labels.service.ts | 3 + .../label-validator/item-rule-validator.ts | 15 ++- .../item-statistic-validator.ts | 15 ++- 7 files changed, 133 insertions(+), 11 deletions(-) diff --git a/common/src/import-export/policy-label.ts b/common/src/import-export/policy-label.ts index 0948f70943..0e0cb2b29f 100644 --- a/common/src/import-export/policy-label.ts +++ b/common/src/import-export/policy-label.ts @@ -1,5 +1,5 @@ import JSZip from 'jszip'; -import { PolicyLabel } from '../entity/index.js'; +import { PolicyLabel, Policy, Schema as SchemaCollection } from '../entity/index.js'; import { IPolicyLabelConfig, INavItemConfig, @@ -11,9 +11,14 @@ import { IGroupItemConfig, INavLabelImportConfig, INavStatisticImportConfig, - IStatisticConfig + IStatisticConfig, + SchemaEntity, + SchemaStatus, + Schema } from '@guardian/interfaces'; import { PolicyStatisticImportExport } from './policy-statistic.js'; +import { PolicyImportExport } from './policy.js'; +import { DatabaseServer } from '../database-modules/index.js'; /** * PolicyLabel components @@ -276,4 +281,103 @@ export class PolicyLabelImportExport { return ''; } } + + /** + * Load policy schemas + * @param policy policy + * @returns policy schemas + */ + public static async getPolicySchemas(policy: Policy): Promise { + const { schemas, toolSchemas } = await PolicyImportExport.loadAllSchemas(policy); + const systemSchemas = await DatabaseServer.getSchemas({ + topicId: policy.topicId, + entity: { $in: [SchemaEntity.MINT_TOKEN, SchemaEntity.MINT_NFTOKEN] } + }); + + const all = [] + .concat(schemas, toolSchemas, systemSchemas) + .filter((s) => s.status === SchemaStatus.PUBLISHED && s.entity !== 'EVC'); + return all; + } + + /** + * Update schema uuid + * @param schemas policy schemas + * @param data config + * @returns new config + */ + public static updateSchemas( + schemas: SchemaCollection[], + data?: IPolicyLabelConfig + ): IPolicyLabelConfig | undefined { + if (!data) { + return; + } + + const fieldMap = new Map(); + const schemaObjects = schemas.map((s) => new Schema(s)); + for (const schema of schemaObjects) { + const allFields = schema.getFields(); + for (const field of allFields) { + const key = `${schema.name}|${field.path}|${field.description}|${field.type}|${field.isArray}|${field.isRef}`; + fieldMap.set(key, schema.iri); + } + } + + if (Array.isArray(data?.children)) { + for (const item of data.children) { + PolicyLabelImportExport._updateSchemas(fieldMap, item); + } + } + + return data; + } + + private static _updateSchemas( + fieldMap: Map, + data: INavItemConfig + ): void { + if (!data) { + return; + } + if (data.type === NavItemType.Group) { + if (Array.isArray(data.children)) { + for (const item of data.children) { + PolicyLabelImportExport._updateSchemas(fieldMap, item); + } + } + } + if (data.type === NavItemType.Label) { + if (Array.isArray(data.config?.children)) { + for (const item of data.config.children) { + PolicyLabelImportExport._updateSchemas(fieldMap, item); + } + } + } + if (data.type === NavItemType.Rules || data.type === NavItemType.Statistic) { + const config = data.config; + const variables = config.variables; + const rules = config.rules; + + const schemaMap = new Map(); + if (Array.isArray(variables)) { + for (const variable of variables) { + const key = `${variable.schemaName}|${variable.path}|${variable.fieldDescription}|${variable.fieldType}|${variable.fieldArray}|${variable.fieldRef}`; + schemaMap.set(variable.schemaId, fieldMap.get(key)); + } + } + + if (Array.isArray(variables)) { + for (const variable of variables) { + variable.schemaId = schemaMap.get(variable.schemaId); + } + } + + if (Array.isArray(rules)) { + for (const rule of rules) { + rule.schemaId = schemaMap.get(rule.schemaId); + } + } + } + } } diff --git a/frontend/src/app/modules/statistics/policy-labels/policy-label-document-configuration/policy-label-document-configuration.component.scss b/frontend/src/app/modules/statistics/policy-labels/policy-label-document-configuration/policy-label-document-configuration.component.scss index 8a00ff309f..e5000e3ed3 100644 --- a/frontend/src/app/modules/statistics/policy-labels/policy-label-document-configuration/policy-label-document-configuration.component.scss +++ b/frontend/src/app/modules/statistics/policy-labels/policy-label-document-configuration/policy-label-document-configuration.component.scss @@ -303,6 +303,7 @@ .field-value { width: 100%; + height: 40px; border-radius: 8px; border: 1px solid #E1E7EF; background: #F9FAFC; diff --git a/frontend/src/app/modules/statistics/policy-labels/policy-label-document-view/policy-label-document-view.component.scss b/frontend/src/app/modules/statistics/policy-labels/policy-label-document-view/policy-label-document-view.component.scss index d91f5b729e..8fee50754a 100644 --- a/frontend/src/app/modules/statistics/policy-labels/policy-label-document-view/policy-label-document-view.component.scss +++ b/frontend/src/app/modules/statistics/policy-labels/policy-label-document-view/policy-label-document-view.component.scss @@ -150,6 +150,7 @@ .field-value { width: 100%; + height: 40px; border-radius: 8px; border: 1px solid #E1E7EF; background: #F9FAFC; diff --git a/frontend/src/app/services/auth.service.ts b/frontend/src/app/services/auth.service.ts index 392ec18b22..854b5f58c8 100644 --- a/frontend/src/app/services/auth.service.ts +++ b/frontend/src/app/services/auth.service.ts @@ -108,7 +108,6 @@ export class AuthService { } public balance(): Observable { - return of('0'); return this.http.get(`${this.url}/balance`); } diff --git a/guardian-service/src/api/policy-labels.service.ts b/guardian-service/src/api/policy-labels.service.ts index b0d68de302..a9c7f05c9d 100644 --- a/guardian-service/src/api/policy-labels.service.ts +++ b/guardian-service/src/api/policy-labels.service.ts @@ -433,6 +433,8 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise { return new MessageError('Item does not exist.'); } + const schemas = await PolicyLabelImportExport.getPolicySchemas(policy); + const preview = await PolicyLabelImportExport.parseZipFile(Buffer.from(zip.data)); const { label } = preview; @@ -446,6 +448,7 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise { label.policyTopicId = policy.topicId; label.policyInstanceTopicId = policy.instanceTopicId; label.status = EntityStatus.DRAFT; + label.config = PolicyLabelImportExport.updateSchemas(schemas, label.config); label.config = PolicyLabelImportExport.validateConfig(label.config); const row = await DatabaseServer.createPolicyLabel(label); diff --git a/interfaces/src/validators/label-validator/item-rule-validator.ts b/interfaces/src/validators/label-validator/item-rule-validator.ts index 440ef4648a..a756619f65 100644 --- a/interfaces/src/validators/label-validator/item-rule-validator.ts +++ b/interfaces/src/validators/label-validator/item-rule-validator.ts @@ -293,15 +293,22 @@ export class RuleItemValidator { status: this.status }; for (const field of this.variables) { - if (field.value !== undefined) { - document[field.id] = field.getValue(); + const value = field.getValue(); + if (value !== undefined) { + document[field.id] = value; } } for (const score of this.scores) { - document[score.id] = score.getValue(); + const value = score.getValue(); + if (value !== undefined) { + document[score.id] = value; + } } for (const formula of this.formulas) { - document[formula.id] = formula.getValue(); + const value = formula.getValue(); + if (value !== undefined) { + document[formula.id] = value; + } } return document; } diff --git a/interfaces/src/validators/label-validator/item-statistic-validator.ts b/interfaces/src/validators/label-validator/item-statistic-validator.ts index d79f860341..0b1fdf02d9 100644 --- a/interfaces/src/validators/label-validator/item-statistic-validator.ts +++ b/interfaces/src/validators/label-validator/item-statistic-validator.ts @@ -284,15 +284,22 @@ export class StatisticItemValidator { public getResult(): any { const document: any = {}; for (const field of this.variables) { - if (field.value !== undefined) { - document[field.id] = field.getValue(); + const value = field.getValue(); + if (value !== undefined) { + document[field.id] = value; } } for (const score of this.scores) { - document[score.id] = score.getValue(); + const value = score.getValue(); + if (value !== undefined) { + document[score.id] = value; + } } for (const formula of this.formulas) { - document[formula.id] = formula.getValue(); + const value = formula.getValue(); + if (value !== undefined) { + document[formula.id] = value; + } } return document; }