Skip to content

Commit

Permalink
fix
Browse files Browse the repository at this point in the history
Signed-off-by: Stepan Kiryakov <[email protected]>
  • Loading branch information
Stepan-Kirjakov committed Dec 30, 2024
1 parent 74e3751 commit 9d42d7e
Show file tree
Hide file tree
Showing 7 changed files with 133 additions and 11 deletions.
108 changes: 106 additions & 2 deletions common/src/import-export/policy-label.ts
Original file line number Diff line number Diff line change
@@ -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,
Expand All @@ -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
Expand Down Expand Up @@ -276,4 +281,103 @@ export class PolicyLabelImportExport {
return '';
}
}

/**
* Load policy schemas
* @param policy policy
* @returns policy schemas
*/
public static async getPolicySchemas(policy: Policy): Promise<SchemaCollection[]> {
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<string, string>();
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<string, string>,
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<string, string>();
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);
}
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,7 @@

.field-value {
width: 100%;
height: 40px;
border-radius: 8px;
border: 1px solid #E1E7EF;
background: #F9FAFC;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -150,6 +150,7 @@

.field-value {
width: 100%;
height: 40px;
border-radius: 8px;
border: 1px solid #E1E7EF;
background: #F9FAFC;
Expand Down
1 change: 0 additions & 1 deletion frontend/src/app/services/auth.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,6 @@ export class AuthService {
}

public balance(): Observable<any> {
return of('0');
return this.http.get<any>(`${this.url}/balance`);
}

Expand Down
3 changes: 3 additions & 0 deletions guardian-service/src/api/policy-labels.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -433,6 +433,8 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise<void> {
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;

Expand All @@ -446,6 +448,7 @@ export async function policyLabelsAPI(logger: PinoLogger): Promise<void> {
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);

Expand Down
15 changes: 11 additions & 4 deletions interfaces/src/validators/label-validator/item-rule-validator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down

0 comments on commit 9d42d7e

Please sign in to comment.