Skip to content

Commit

Permalink
Merge pull request #2095 from hashgraph/feature/2091
Browse files Browse the repository at this point in the history
Feature/2091
  • Loading branch information
artembuslaev authored May 3, 2023
2 parents f7bbeb8 + 7ccc7d0 commit f2041dc
Show file tree
Hide file tree
Showing 20 changed files with 224 additions and 82 deletions.
20 changes: 18 additions & 2 deletions common/src/database-modules/database-server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -242,9 +242,25 @@ export class DatabaseServer {
* @param criteria
* @param row
*/
private async update<T extends BaseEntity>(entityClass: new () => T, criteria: any, row: T): Promise<T> {
private async update<T extends BaseEntity>(
entityClass: new () => T,
criteria: any,
row: any
): Promise<T> {
if (this.dryRun) {
return await new DataBaseHelper(DryRun).update(row, criteria) as any;
if (Array.isArray(row)) {
for (const i of row) {
i.dryRunId = this.dryRun;
i.dryRunClass = this.classMap.get(entityClass);
}
} else {
row.dryRunId = this.dryRun;
row.dryRunClass = this.classMap.get(entityClass);
}
return (await new DataBaseHelper(DryRun).update(
row,
criteria
)) as any;
} else {
return await new DataBaseHelper(entityClass).update(row, criteria);
}
Expand Down
6 changes: 6 additions & 0 deletions common/src/entity/aggregate-documents.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,12 @@ export class AggregateVC extends BaseEntity {
@Property({ nullable: true })
messageIds?: string[];

/**
* Source document identifier
*/
@Property({ nullable: true })
sourceDocumentId?: ObjectId;

/**
* Create document
*/
Expand Down
6 changes: 6 additions & 0 deletions common/src/entity/dry-run.ts
Original file line number Diff line number Diff line change
Expand Up @@ -545,6 +545,12 @@ export class DryRun extends BaseEntity {
@Property({ nullable: true })
uri?: string;

/**
* Source document identifier
*/
@Property({ nullable: true })
sourceDocumentId?: ObjectId;

/**
* Default document values
*/
Expand Down
4 changes: 3 additions & 1 deletion common/src/helpers/db-helper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -130,7 +130,9 @@ export class DataBaseHelper<T extends BaseEntity> {
}
return arrResult;
}
entity._id = new ObjectId(ObjectId.generate());
if (!entity._id) {
entity._id = new ObjectId(ObjectId.generate());
}
return this._em.fork().create(this.entityClass, entity);
}

Expand Down
6 changes: 3 additions & 3 deletions guardian-service/src/policy-engine/block-about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -408,8 +408,8 @@ export const BlockAbout = {
'revokeBlock': {
'label': 'Revoke Document',
'title': 'Add \'Revoke\' Block',
'post': true,
'get': true,
'post': false,
'get': false,
'children': 'None',
'control': 'Server',
'input': [
Expand Down Expand Up @@ -890,4 +890,4 @@ export const BlockAbout = {
/**
* Block About (String)
*/
export const BlockAboutString = JSON.stringify(BlockAbout);
export const BlockAboutString = JSON.stringify(BlockAbout);
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ export class PolicyComponentsUtils {
/**
* Block update function
*/
public static BlockUpdateFn: (uuid: string, state: any, user: IPolicyUser, tag?: string) => Promise<void>;
public static BlockUpdateFn: (uuid: string, user: IPolicyUser) => Promise<void>;
/**
* Block error function
*/
Expand Down
57 changes: 52 additions & 5 deletions guardian-service/src/policy-engine/policy-converter-utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ export class PolicyConverterUtils {
/**
* Base version
*/
public static readonly VERSION = '1.5.0';
public static readonly VERSION = '1.5.1';

/**
* Policy converter
Expand All @@ -20,8 +20,8 @@ export class PolicyConverterUtils {
if (policy.codeVersion === PolicyConverterUtils.VERSION) {
return policy;
}

policy.config = PolicyConverterUtils.BlockConverter(policy.config, policy.codeVersion);
const root = policy.config;
policy.config = PolicyConverterUtils.BlockConverter(root, root, policy.codeVersion);
policy.codeVersion = PolicyConverterUtils.VERSION;
return policy;
}
Expand Down Expand Up @@ -68,6 +68,7 @@ export class PolicyConverterUtils {
* @private
*/
private static BlockConverter(
root: any,
block: any,
policyVersion: string,
parent?: any,
Expand All @@ -90,10 +91,13 @@ export class PolicyConverterUtils {
if (PolicyConverterUtils.versionCompare('1.5.0', policyVersion) > 0) {
block = PolicyConverterUtils.v1_5_0(block, parent, index, next, prev);
}
if (PolicyConverterUtils.versionCompare('1.5.1', policyVersion) > 0) {
block = PolicyConverterUtils.v1_5_1(root, block, parent, index, next, prev);
}
if (block.children && block.children.length) {
for (let i = 0; i < block.children.length; i++) {
block.children[i] = PolicyConverterUtils.BlockConverter(
block.children[i], policyVersion, block, i, block.children[i + 1], block.children[i - 1]
root, block.children[i], policyVersion, block, i, block.children[i + 1], block.children[i - 1]
);
}
}
Expand Down Expand Up @@ -317,7 +321,7 @@ export class PolicyConverterUtils {
}

/**
* Create 1.3.0 version
* Create 1.5.0 version
* @param block
* @param parent
* @param index
Expand Down Expand Up @@ -359,4 +363,47 @@ export class PolicyConverterUtils {
}
return block;
}

/**
* Create 1.5.1 version
* @param block
* @param parent
* @param index
* @param next
* @param prev
* @private
*/
private static v1_5_1(
root: any,
block: any,
parent?: any,
index?: any,
next?: any,
prev?: any
): any {
if (block.blockType !== 'revokeBlock') {
return block;
}
const clearOldRevokeColumns = (rootBlock: any, tag: string) => {
const stack = [rootBlock];
while (stack.length > 0) {
const currentBlock = stack.pop();
if (
currentBlock?.blockType ===
'interfaceDocumentsSourceBlock' &&
Array.isArray(currentBlock.uiMetaData?.fields)
) {
currentBlock.uiMetaData.fields =
currentBlock.uiMetaData.fields.filter(
(field) => field.bindBlock !== tag
);
}
if (Array.isArray(currentBlock?.children)) {
stack.push(...currentBlock.children);
}
}
};
clearOldRevokeColumns(root, block.tag);
return block;
}
}
5 changes: 2 additions & 3 deletions guardian-service/src/policy-engine/policy-engine.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -107,14 +107,13 @@ export class PolicyEngineService {
* @param uuid {string} - id of block
* @param user {IPolicyUser} - short user object
*/
private async stateChangeCb(blocks: string[], state: any, user: IPolicyUser) {
private async stateChangeCb(blocks: string[], user: IPolicyUser) {
if (!user || !user.did) {
return;
}

await this.channel.publish('update-block', {
blocks,
state,
user
});
}
Expand Down Expand Up @@ -199,7 +198,7 @@ export class PolicyEngineService {

switch (type) {
case 'update':
PolicyComponentsUtils.BlockUpdateFn(args[0], args[1], args[2], args[3]);
PolicyComponentsUtils.BlockUpdateFn(args[0], args[1]);
break;

case 'error':
Expand Down
47 changes: 35 additions & 12 deletions policy-service/src/policy-engine/blocks/aggregate-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,7 @@ export class AggregateBlock {
const groupByUser = !disableUserGrouping;

const map = new Map<string, AggregateVC[]>();
const removeMsp: AggregateVC[] = [];
let removeMsp: AggregateVC[] = [];

for (const element of rawEntities) {
const id = PolicyUtils.getScopeId(element);
Expand Down Expand Up @@ -155,9 +155,7 @@ export class AggregateBlock {
}
}

if (removeMsp.length) {
await ref.databaseServer.removeAggregateDocuments(removeMsp);
}
removeMsp = await this.removeDocuments(ref, removeMsp);

for (const [key, documents] of map) {
await this.sendCronDocuments(
Expand All @@ -174,10 +172,7 @@ export class AggregateBlock {
* @param documents Documents
*/
private async sendCronDocuments(ref: AnyBlockType, userId: string, documents: AggregateVC[]) {
if (documents.length) {
await ref.databaseServer.removeAggregateDocuments(documents);
}

documents = await this.removeDocuments(ref, documents);
if (documents.length || ref.options.emptyData) {
const state = { data: documents };
const user = PolicyUtils.getPolicyUserById(ref, userId);
Expand Down Expand Up @@ -264,7 +259,7 @@ export class AggregateBlock {
}
}

const rawEntities = await ref.databaseServer.getAggregateDocuments(ref.policyId, ref.uuid, filters);
let rawEntities = await ref.databaseServer.getAggregateDocuments(ref.policyId, ref.uuid, filters);

const scopes: any[] = [];
for (const doc of rawEntities) {
Expand All @@ -281,7 +276,7 @@ export class AggregateBlock {

if (result === true) {
const user = PolicyUtils.getPolicyUser(ref, document.owner, document.group);
await ref.databaseServer.removeAggregateDocuments(rawEntities);
rawEntities = await this.removeDocuments(ref, rawEntities);
const state = { data: rawEntities };
ref.triggerEvents(PolicyOutputEventType.RunEvent, user, state);
ref.triggerEvents(PolicyOutputEventType.RefreshEvent, user, state);
Expand All @@ -296,11 +291,39 @@ export class AggregateBlock {
* @param ref
* @param doc
*/
async saveDocuments(ref: AnyBlockType, doc: IPolicyDocument): Promise<void> {
const item = PolicyUtils.cloneVC(ref, doc);
private async saveDocuments(
ref: AnyBlockType,
doc: IPolicyDocument
): Promise<void> {
const item: any = PolicyUtils.cloneVC(ref, doc);
item.sourceDocumentId = item._id;
delete item._id;
delete item.id;
await ref.databaseServer.createAggregateDocuments(item, ref.uuid);
}

/**
* Remove documents
* @param ref
* @param documents
*/
private async removeDocuments(
ref: AnyBlockType,
documents: AggregateVC[]
): Promise<AggregateVC[]> {
if (documents.length) {
await ref.databaseServer.removeAggregateDocuments(documents);
documents
.filter((document) => document.sourceDocumentId)
.forEach((document: any) => {
document._id = document.sourceDocumentId;
document.id = document.sourceDocumentId.toString();
delete document.sourceDocumentId;
});
}
return documents;
}

/**
* Run action callback
* @event PolicyInputEventType.RunEvent
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ export class DocumentsSourceAddon {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ export class InterfaceDocumentsSource {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ export class MultiSignBlock {
await this.updateThreshold(users, sourceDoc, documentId, user);

ref.triggerEvents(PolicyOutputEventType.RefreshEvent, user, null);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, {
documents: ExternalDocuments(document)
}));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class PaginationAddon {
this.state = oldState;

const ref = PolicyComponentsUtils.GetBlockRef(this);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);

PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, data));
}
Expand Down
2 changes: 1 addition & 1 deletion policy-service/src/policy-engine/blocks/policy-roles.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,7 +427,7 @@ export class PolicyRolesBlock {

await PolicyComponentsUtils.UpdateUserInfoFn(user, ref.policyInstance);

PolicyComponentsUtils.BlockUpdateFn(ref.parent, {}, user, ref.tag);
PolicyComponentsUtils.BlockUpdateFn(ref.parent, user);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Set, ref, user, {
group: group.uuid,
role: group.role
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { ActionCallback, BasicBlock } from '@policy-engine/helpers/decorators';
import { ActionCallback, SetRelationshipsBlock as SetRelationships } from '@policy-engine/helpers/decorators';
import { PolicyComponentsUtils } from '@policy-engine/policy-components-utils';
import { IPolicyDocument, IPolicyEventState, IPolicyRequestBlock } from '@policy-engine/policy-engine.interface';
import { IPolicyEvent, PolicyInputEventType, PolicyOutputEventType } from '@policy-engine/interfaces';
Expand All @@ -8,7 +8,7 @@ import { ExternalDocuments, ExternalEvent, ExternalEventType } from '@policy-eng
/**
* Set document relationships action
*/
@BasicBlock({
@SetRelationships({
blockType: 'setRelationshipsBlock',
commonBlock: false,
about: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -415,7 +415,7 @@ export function BasicBlock<T>(options: Partial<PolicyBlockDecoratorOptions>) {
users[user.did] = user;
}
for (const item of Object.values(users)) {
PolicyComponentsUtils.BlockUpdateFn(this as any, state, item, tag);
PolicyComponentsUtils.BlockUpdateFn(this as any, item);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ export function DataSourceAddon(options: Partial<PolicyBlockDecoratorOptions>) {
}
}

PolicyComponentsUtils.BlockUpdateFn(parentBlock, {}, args[0], this.tag);
PolicyComponentsUtils.BlockUpdateFn(parentBlock, args[0]);
return result;
}

Expand Down
Loading

0 comments on commit f2041dc

Please sign in to comment.