Skip to content

Commit 089cb36

Browse files
author
Ihar
committed
feat: base custom type messages[5018]
1 parent 9f20093 commit 089cb36

File tree

2 files changed

+131
-1
lines changed

2 files changed

+131
-1
lines changed

guardian-service/src/policy-engine/block-about.ts

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1597,6 +1597,36 @@ export const BlockAbout = {
15971597
'label': 'Schema',
15981598
'title': 'Expected schema',
15991599
'type': 'Schemas'
1600+
},
1601+
{
1602+
'name': 'messageTypes',
1603+
'label': 'Message types',
1604+
'title': 'Message type mappings',
1605+
'type': 'Array',
1606+
'items': {
1607+
'label': 'Message type',
1608+
'value': '@filterField @filterValue @messageType',
1609+
'properties': [
1610+
{
1611+
'name': 'filterField',
1612+
'label': 'Filter field',
1613+
'title': 'Filter field (VC path or @message.<field>)',
1614+
'type': 'Input'
1615+
},
1616+
{
1617+
'name': 'filterValue',
1618+
'label': 'Filter value',
1619+
'title': 'Filter value',
1620+
'type': 'Input'
1621+
},
1622+
{
1623+
'name': 'messageType',
1624+
'label': 'Message type',
1625+
'title': 'Message type name used in Events tab',
1626+
'type': 'Input'
1627+
}
1628+
]
1629+
}
16001630
}
16011631
]
16021632
},

policy-service/src/policy-engine/blocks/global-events-reader-block.ts

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,36 @@ interface GlobalVsNotification {
8080
label: 'Schema',
8181
title: 'Expected schema',
8282
type: PropertyType.Schemas
83+
},
84+
{
85+
name: 'messageTypes',
86+
label: 'Message types',
87+
title: 'Message type mappings',
88+
type: PropertyType.Array,
89+
items: {
90+
label: 'Message type',
91+
value: '@filterField @filterValue @messageType',
92+
properties: [
93+
{
94+
name: 'filterField',
95+
label: 'Filter field',
96+
title: 'Filter field (VC path or @message.<field>)',
97+
type: PropertyType.Input
98+
},
99+
{
100+
name: 'filterValue',
101+
label: 'Filter value',
102+
title: 'Filter value',
103+
type: PropertyType.Input
104+
},
105+
{
106+
name: 'messageType',
107+
label: 'Message type',
108+
title: 'Message type name used in Events tab',
109+
type: PropertyType.Input
110+
}
111+
]
112+
}
83113
}
84114
]
85115
},
@@ -457,6 +487,30 @@ export class GlobalEventsReaderBlock {
457487
};
458488
}
459489

490+
private getFieldValue(source: any, path: string): any {
491+
if (!source || typeof source !== 'object') {
492+
return undefined;
493+
}
494+
495+
if (!path || typeof path !== 'string') {
496+
return undefined;
497+
}
498+
499+
const segments = path.split('.');
500+
501+
let current: any = source;
502+
503+
for (const segment of segments) {
504+
if (current == null) {
505+
return undefined;
506+
}
507+
508+
current = current[segment];
509+
}
510+
511+
return current;
512+
}
513+
460514
private async processNotification(
461515
ref: AnyBlockType,
462516
user: PolicyUser,
@@ -678,9 +732,55 @@ export class GlobalEventsReaderBlock {
678732
throw new BlockActionError(error, ref.blockType, ref.uuid);
679733
}
680734

681-
ref.triggerEvents(PolicyOutputEventType.RunEvent, user, state);
735+
const options: any = ref.options || {};
736+
const messageTypesConfig: any[] = Array.isArray(options.messageTypes)
737+
? options.messageTypes
738+
: [];
739+
740+
let matchedMessageType: boolean = false;
741+
742+
for (const configItem of messageTypesConfig) {
743+
if (!configItem) {
744+
continue;
745+
}
746+
747+
const filterField: string | undefined = configItem.filterField;
748+
const filterValue: string | undefined = configItem.filterValue;
749+
const messageType: string | undefined = configItem.messageType;
750+
751+
if (!messageType) {
752+
continue;
753+
}
754+
755+
let actualValue: any = undefined;
756+
757+
if (filterField && typeof filterField === 'string') {
758+
if (filterField.startsWith('@message.')) {
759+
// read from payload (notification meta)
760+
const messagePath: string = filterField.slice('@message.'.length);
761+
actualValue = this.getFieldValue(payload as any, messagePath);
762+
} else {
763+
// read from VC document
764+
actualValue = this.getFieldValue(document, filterField);
765+
}
766+
}
767+
768+
const isMatch: boolean = !filterField || actualValue === filterValue;
769+
770+
if (isMatch) {
771+
ref.triggerEvents(messageType, user, state);
772+
matchedMessageType = true;
773+
}
774+
}
775+
776+
if (!matchedMessageType) {
777+
// fallback to default behavior
778+
ref.triggerEvents(PolicyOutputEventType.RunEvent, user, state);
779+
}
780+
682781
ref.triggerEvents(PolicyOutputEventType.ReleaseEvent, user, null);
683782
ref.triggerEvents(PolicyOutputEventType.RefreshEvent, user, state);
783+
684784
PolicyComponentsUtils.ExternalEventFn(new ExternalEvent(ExternalEventType.Run, ref, user, {
685785
documents: ExternalDocuments(policyDocument)
686786
}));

0 commit comments

Comments
 (0)