Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update custom logic block #3918

Merged
merged 1 commit into from
Jul 22, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 14 additions & 6 deletions guardian-service/src/policy-engine/block-about.ts
Original file line number Diff line number Diff line change
Expand Up @@ -184,12 +184,20 @@ export const BlockAbout = {
'ErrorEvent'
],
'defaultEvent': true,
'properties': [{
'name': 'unsigned',
'label': 'Unsigned VC',
'title': 'Unsigned document',
'type': 'Checkbox'
}]
'properties': [
{
'name': 'unsigned',
'label': 'Unsigned VC',
'title': 'Unsigned document',
'type': 'Checkbox'
},
{
'name': 'passOriginal',
'label': 'Pass original',
'title': 'Pass original document',
'type': 'Checkbox'
}
]
},
'documentsSourceAddon': {
'label': 'Source',
Expand Down
68 changes: 45 additions & 23 deletions policy-service/src/policy-engine/blocks/custom-logic-block.ts
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,20 @@ interface IMetadata {
PolicyOutputEventType.ErrorEvent
],
defaultEvent: true,
properties: [{
name: 'unsigned',
label: 'Unsigned VC',
title: 'Unsigned document',
type: PropertyType.Checkbox
}]
properties: [
{
name: 'unsigned',
label: 'Unsigned VC',
title: 'Unsigned document',
type: PropertyType.Checkbox
},
{
name: 'passOriginal',
label: 'Pass original',
title: 'Pass original document',
type: PropertyType.Checkbox
}
]
},
variables: [
{ path: 'options.outputSchema', alias: 'schema', type: 'Schema' }
Expand Down Expand Up @@ -84,17 +92,19 @@ export class CustomLogicBlock {
const ref = PolicyComponentsUtils.GetBlockRef<IPolicyCalculateBlock>(this);

try {
const documents = await this.execute(event.data, event.user);
if (!documents) {
return;
const triggerEvents = (documents: IPolicyDocument | IPolicyDocument[]) => {
if (!documents) {
return;
}
event.data.data = documents;
ref.triggerEvents(PolicyOutputEventType.RunEvent, event.user, event.data);
ref.triggerEvents(PolicyOutputEventType.ReleaseEvent, event.user, null);
ref.triggerEvents(PolicyOutputEventType.RefreshEvent, event.user, event.data);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Run, ref, event?.user, {
documents: ExternalDocuments(event?.data?.data)
}));
}
event.data.data = documents;
ref.triggerEvents(PolicyOutputEventType.RunEvent, event.user, event.data);
ref.triggerEvents(PolicyOutputEventType.ReleaseEvent, event.user, null);
ref.triggerEvents(PolicyOutputEventType.RefreshEvent, event.user, event.data);
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Run, ref, event?.user, {
documents: ExternalDocuments(event?.data?.data)
}));
await this.execute(event.data, event.user, triggerEvents);
} catch (error) {
ref.error(PolicyUtils.getErrorMessage(error));
}
Expand Down Expand Up @@ -125,7 +135,7 @@ export class CustomLogicBlock {
* @param state
* @param user
*/
execute(state: IPolicyEventState, user: PolicyUser): Promise<IPolicyDocument | IPolicyDocument[]> {
execute(state: IPolicyEventState, user: PolicyUser, triggerEvents: (documents: IPolicyDocument | IPolicyDocument[]) => void): Promise<IPolicyDocument | IPolicyDocument[]> {
return new Promise<IPolicyDocument | IPolicyDocument[]>(async (resolve, reject) => {
try {
const ref = PolicyComponentsUtils.GetBlockRef<IPolicyCalculateBlock>(this);
Expand All @@ -143,12 +153,18 @@ export class CustomLogicBlock {
metadata = await this.aggregateMetadata(documents, user, ref);
}

const done = async (result: any | any[]) => {
const done = async (result: any | any[], final: boolean) => {
if (!result) {
resolve(null);
triggerEvents(null);
if (final) {
resolve(null);
}
return;
}
const processing = async (json: any): Promise<IPolicyDocument> => {
if (ref.options.passOriginal) {
return json;
}
if (ref.options.unsigned) {
return await this.createUnsignedDocument(json, ref);
} else {
Expand All @@ -160,10 +176,16 @@ export class CustomLogicBlock {
for (const r of result) {
items.push(await processing(r))
}
resolve(items);
triggerEvents(items);
if (final) {
resolve(items);
}
return;
} else {
resolve(await processing(result));
triggerEvents(await processing(result));
if (final) {
resolve(await processing(result));
}
return;
}
}
Expand Down Expand Up @@ -200,9 +222,9 @@ export class CustomLogicBlock {
worker.on('error', (error) => {
reject(error);
});
worker.on('message', async (result) => {
worker.on('message', async (data) => {
try {
await done(result);
await done(data.result, data.final);
} catch (error) {
reject(error);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ import * as formulajs from '@formulajs/formulajs'
* Execute function
*/
function execute(): void {
const done = (result) => {
parentPort.postMessage(result);
const done = (result, final = true) => {
parentPort.postMessage({result, final});
}

const { execFunc, user, documents, artifacts, sources } = workerData;
Expand Down
Loading