@fibery/expression-utils 9.2.0 → 9.3.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/contextVariables.js +86 -0
- package/lib/expression-utils.js +87 -4
- package/lib/utils.js +1 -4
- package/package.json +3 -3
- package/types.d.ts +23 -3
package/lib/contextVariables.js
CHANGED
|
@@ -4,6 +4,89 @@ function _interopDefaultLegacy (e) { return e && typeof e === 'object' && 'defau
|
|
|
4
4
|
|
|
5
5
|
var ___default = /*#__PURE__*/_interopDefaultLegacy(_);
|
|
6
6
|
|
|
7
|
+
const getAutomationButtonContextVariables = (typeId, typeName, fiberySchema) => {
|
|
8
|
+
if (!fiberySchema) {
|
|
9
|
+
return [];
|
|
10
|
+
}
|
|
11
|
+
const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
|
|
12
|
+
const typeObject = fiberySchema.typeObjectsById[typeId];
|
|
13
|
+
return [{
|
|
14
|
+
id: "triggeredEntity",
|
|
15
|
+
title: `Step 1 ${typeName}`,
|
|
16
|
+
typeObject,
|
|
17
|
+
description: `The ${typeName} for which the Button was clicked`
|
|
18
|
+
}, {
|
|
19
|
+
id: "currentUser",
|
|
20
|
+
title: `User who clicked Button`,
|
|
21
|
+
typeObject: userTypeObject,
|
|
22
|
+
description: `The User who triggered this automation by clicking a Button.`
|
|
23
|
+
}];
|
|
24
|
+
};
|
|
25
|
+
const getTriggerCollectionFieldObject = (typeId, collectionFieldId, fiberySchema) => {
|
|
26
|
+
if (!fiberySchema) {
|
|
27
|
+
return undefined;
|
|
28
|
+
}
|
|
29
|
+
if (!Object.hasOwn(fiberySchema.typeObjectsById, typeId)) {
|
|
30
|
+
return undefined;
|
|
31
|
+
}
|
|
32
|
+
if (!Object.hasOwn(fiberySchema.typeObjectsById[typeId].fieldObjectsById, collectionFieldId)) {
|
|
33
|
+
return undefined;
|
|
34
|
+
}
|
|
35
|
+
return fiberySchema.typeObjectsById[typeId].fieldObjectsById[collectionFieldId];
|
|
36
|
+
};
|
|
37
|
+
const getTriggerContextVariables = (trigger, typeId, fiberySchema) => {
|
|
38
|
+
if (!fiberySchema) {
|
|
39
|
+
return [];
|
|
40
|
+
}
|
|
41
|
+
const typeObject = fiberySchema.typeObjectsById[typeId];
|
|
42
|
+
const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
|
|
43
|
+
const triggeredEntityVar = {
|
|
44
|
+
id: "triggeredEntity",
|
|
45
|
+
title: `Step 1 ${typeObject.title}`,
|
|
46
|
+
typeObject,
|
|
47
|
+
description: `The ${typeObject.title} from step 1 of this Rule`
|
|
48
|
+
};
|
|
49
|
+
const currentUserVar = {
|
|
50
|
+
id: "currentUser",
|
|
51
|
+
title: `User who triggered Rule`,
|
|
52
|
+
typeObject: userTypeObject,
|
|
53
|
+
description: `The User who made the change which triggered this automation. Empty in case the change was made by a formula or an integration.`
|
|
54
|
+
};
|
|
55
|
+
if (trigger.trigger === "time-based") {
|
|
56
|
+
return [triggeredEntityVar];
|
|
57
|
+
} else if (trigger.trigger === "created") {
|
|
58
|
+
return typeObject.syncSource !== null ? [triggeredEntityVar] : [triggeredEntityVar, currentUserVar];
|
|
59
|
+
} else if (trigger.trigger === "updated") {
|
|
60
|
+
const disableCurrentUserVar = trigger.args.updatedField && ___default["default"].isArray(trigger.args.updatedField) && trigger.args.updatedField.some(fieldId => {
|
|
61
|
+
if (!Object.hasOwn(typeObject.fieldObjectsById, fieldId)) {
|
|
62
|
+
return false;
|
|
63
|
+
}
|
|
64
|
+
const fieldObject = typeObject.fieldObjectsById[fieldId];
|
|
65
|
+
return (fieldObject == null ? void 0 : fieldObject.isReadOnly) || (fieldObject == null ? void 0 : fieldObject.type) === "Collaboration~Documents/Document";
|
|
66
|
+
});
|
|
67
|
+
return disableCurrentUserVar ? [triggeredEntityVar] : [triggeredEntityVar, currentUserVar];
|
|
68
|
+
} else if (trigger.trigger === "collection-item-added" || trigger.trigger === "collection-item-removed") {
|
|
69
|
+
const collectionItemFieldObject = getTriggerCollectionFieldObject(typeId, trigger.args.changedCollectionField, fiberySchema);
|
|
70
|
+
const collectionItemTypeObject = collectionItemFieldObject == null ? void 0 : collectionItemFieldObject.typeObject;
|
|
71
|
+
const variables = [triggeredEntityVar];
|
|
72
|
+
collectionItemTypeObject && variables.push({
|
|
73
|
+
id: "triggered-collection-item",
|
|
74
|
+
title: `Step 1 ${trigger.trigger === "collection-item-added" ? "linked" : "unlinked"} ${collectionItemTypeObject.title}`,
|
|
75
|
+
typeObject: collectionItemTypeObject,
|
|
76
|
+
description: `The ${collectionItemTypeObject.title} from step 1 of this Rule`
|
|
77
|
+
});
|
|
78
|
+
const disableCurrentUserVar = collectionItemFieldObject == null ? void 0 : collectionItemFieldObject.isReadOnly;
|
|
79
|
+
!disableCurrentUserVar && variables.push(currentUserVar);
|
|
80
|
+
return variables;
|
|
81
|
+
} else {
|
|
82
|
+
return [];
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
const getAutomationTriggersContextVariables = (triggers, typeId, fiberySchema) => {
|
|
86
|
+
return ___default["default"].uniqBy(___default["default"].flatten(triggers.map(trigger => {
|
|
87
|
+
return getTriggerContextVariables(trigger, typeId, fiberySchema);
|
|
88
|
+
})), variable => `${variable.id}_${variable.typeObject.id}`);
|
|
89
|
+
};
|
|
7
90
|
const getEntityQueryVariables = schema => {
|
|
8
91
|
const grouped = ___default["default"].groupBy(schema.typeObjects.filter(x => x.isDomain), typeObject => typeObject.pluralTitle);
|
|
9
92
|
return ___default["default"].flatten(Object.values(grouped).map(group => {
|
|
@@ -17,4 +100,7 @@ const getEntityQueryVariables = schema => {
|
|
|
17
100
|
}));
|
|
18
101
|
};
|
|
19
102
|
|
|
103
|
+
exports.getAutomationButtonContextVariables = getAutomationButtonContextVariables;
|
|
104
|
+
exports.getAutomationTriggersContextVariables = getAutomationTriggersContextVariables;
|
|
20
105
|
exports.getEntityQueryVariables = getEntityQueryVariables;
|
|
106
|
+
exports.getTriggerCollectionFieldObject = getTriggerCollectionFieldObject;
|
package/lib/expression-utils.js
CHANGED
|
@@ -269,9 +269,6 @@ const binaryOperations = new Set(["=", "!=", "<", ">", "<=", ">=", "in",
|
|
|
269
269
|
"q/contains", "q/not-contains", "+", "-", "q/+", "q/-", "*", "/", "and", "or",
|
|
270
270
|
//asc: obsolete. use q/and, q/or
|
|
271
271
|
"q/and", "q/or", "q/in", "q/not-in"]);
|
|
272
|
-
|
|
273
|
-
// TODO: get rid of this. Use visitors everywhere
|
|
274
|
-
const naryOperations = new Set(["and", "or", "q/and", "q/or"]);
|
|
275
272
|
const logicalOperators = new Set(["and", "or", "q/and", "q/or"]);
|
|
276
273
|
const relationalOperators = new Set(["=", "!=", "<", ">", "<=", ">="]);
|
|
277
274
|
const mathOperators = new Set(["+", "-", "*", "/", "q/+", "q/-", "q/concat"]);
|
|
@@ -284,7 +281,7 @@ const isCollectionFunctionExpression = expression =>
|
|
|
284
281
|
(expression.length === 2 || expression.length === 3) && collectionOps.has(expression[0]);
|
|
285
282
|
const isAccessFunctionExpression = expresion => expresion.length === 2 && expresion[0] === "q/access?" && isFieldExpression(expresion[1]);
|
|
286
283
|
const isBinaryExpression = expression => expression.length === 3 && binaryOperations.has(expression[0]);
|
|
287
|
-
const isNaryExpression = expression => expression.length > 1 &&
|
|
284
|
+
const isNaryExpression = expression => expression.length > 1 && logicalOperators.has(expression[0]);
|
|
288
285
|
const isVariableExpression = expression => ___default["default"].isString(expression) && expression.startsWith("$");
|
|
289
286
|
const isMultiFieldAccess = expression => ___default["default"].isArray(expression) && expression.length === 2 && !isFunctionCallExpression(expression) && expression.every(x => ___default["default"].isString(x));
|
|
290
287
|
const isMultiFieldExpression = expression => Array.isArray(expression) && !isFunctionCallExpression(expression) && expression.some(x => isMultiFieldAccess(x)) && expression.every(x => !isVariableExpression(x) && !binaryOperations.has(x) && (___default["default"].isString(x) || isMultiFieldAccess(x)));
|
|
@@ -1014,6 +1011,89 @@ var visitors = {
|
|
|
1014
1011
|
fieldAccessVisitorTypeAware: fieldAccessVisitorTypeAware
|
|
1015
1012
|
};
|
|
1016
1013
|
|
|
1014
|
+
const getAutomationButtonContextVariables = (typeId, typeName, fiberySchema) => {
|
|
1015
|
+
if (!fiberySchema) {
|
|
1016
|
+
return [];
|
|
1017
|
+
}
|
|
1018
|
+
const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
|
|
1019
|
+
const typeObject = fiberySchema.typeObjectsById[typeId];
|
|
1020
|
+
return [{
|
|
1021
|
+
id: "triggeredEntity",
|
|
1022
|
+
title: `Step 1 ${typeName}`,
|
|
1023
|
+
typeObject,
|
|
1024
|
+
description: `The ${typeName} for which the Button was clicked`
|
|
1025
|
+
}, {
|
|
1026
|
+
id: "currentUser",
|
|
1027
|
+
title: `User who clicked Button`,
|
|
1028
|
+
typeObject: userTypeObject,
|
|
1029
|
+
description: `The User who triggered this automation by clicking a Button.`
|
|
1030
|
+
}];
|
|
1031
|
+
};
|
|
1032
|
+
const getTriggerCollectionFieldObject = (typeId, collectionFieldId, fiberySchema) => {
|
|
1033
|
+
if (!fiberySchema) {
|
|
1034
|
+
return undefined;
|
|
1035
|
+
}
|
|
1036
|
+
if (!Object.hasOwn(fiberySchema.typeObjectsById, typeId)) {
|
|
1037
|
+
return undefined;
|
|
1038
|
+
}
|
|
1039
|
+
if (!Object.hasOwn(fiberySchema.typeObjectsById[typeId].fieldObjectsById, collectionFieldId)) {
|
|
1040
|
+
return undefined;
|
|
1041
|
+
}
|
|
1042
|
+
return fiberySchema.typeObjectsById[typeId].fieldObjectsById[collectionFieldId];
|
|
1043
|
+
};
|
|
1044
|
+
const getTriggerContextVariables = (trigger, typeId, fiberySchema) => {
|
|
1045
|
+
if (!fiberySchema) {
|
|
1046
|
+
return [];
|
|
1047
|
+
}
|
|
1048
|
+
const typeObject = fiberySchema.typeObjectsById[typeId];
|
|
1049
|
+
const userTypeObject = fiberySchema.typeObjectsByName["fibery/user"];
|
|
1050
|
+
const triggeredEntityVar = {
|
|
1051
|
+
id: "triggeredEntity",
|
|
1052
|
+
title: `Step 1 ${typeObject.title}`,
|
|
1053
|
+
typeObject,
|
|
1054
|
+
description: `The ${typeObject.title} from step 1 of this Rule`
|
|
1055
|
+
};
|
|
1056
|
+
const currentUserVar = {
|
|
1057
|
+
id: "currentUser",
|
|
1058
|
+
title: `User who triggered Rule`,
|
|
1059
|
+
typeObject: userTypeObject,
|
|
1060
|
+
description: `The User who made the change which triggered this automation. Empty in case the change was made by a formula or an integration.`
|
|
1061
|
+
};
|
|
1062
|
+
if (trigger.trigger === "time-based") {
|
|
1063
|
+
return [triggeredEntityVar];
|
|
1064
|
+
} else if (trigger.trigger === "created") {
|
|
1065
|
+
return typeObject.syncSource !== null ? [triggeredEntityVar] : [triggeredEntityVar, currentUserVar];
|
|
1066
|
+
} else if (trigger.trigger === "updated") {
|
|
1067
|
+
const disableCurrentUserVar = trigger.args.updatedField && ___default["default"].isArray(trigger.args.updatedField) && trigger.args.updatedField.some(fieldId => {
|
|
1068
|
+
if (!Object.hasOwn(typeObject.fieldObjectsById, fieldId)) {
|
|
1069
|
+
return false;
|
|
1070
|
+
}
|
|
1071
|
+
const fieldObject = typeObject.fieldObjectsById[fieldId];
|
|
1072
|
+
return (fieldObject == null ? void 0 : fieldObject.isReadOnly) || (fieldObject == null ? void 0 : fieldObject.type) === "Collaboration~Documents/Document";
|
|
1073
|
+
});
|
|
1074
|
+
return disableCurrentUserVar ? [triggeredEntityVar] : [triggeredEntityVar, currentUserVar];
|
|
1075
|
+
} else if (trigger.trigger === "collection-item-added" || trigger.trigger === "collection-item-removed") {
|
|
1076
|
+
const collectionItemFieldObject = getTriggerCollectionFieldObject(typeId, trigger.args.changedCollectionField, fiberySchema);
|
|
1077
|
+
const collectionItemTypeObject = collectionItemFieldObject == null ? void 0 : collectionItemFieldObject.typeObject;
|
|
1078
|
+
const variables = [triggeredEntityVar];
|
|
1079
|
+
collectionItemTypeObject && variables.push({
|
|
1080
|
+
id: "triggered-collection-item",
|
|
1081
|
+
title: `Step 1 ${trigger.trigger === "collection-item-added" ? "linked" : "unlinked"} ${collectionItemTypeObject.title}`,
|
|
1082
|
+
typeObject: collectionItemTypeObject,
|
|
1083
|
+
description: `The ${collectionItemTypeObject.title} from step 1 of this Rule`
|
|
1084
|
+
});
|
|
1085
|
+
const disableCurrentUserVar = collectionItemFieldObject == null ? void 0 : collectionItemFieldObject.isReadOnly;
|
|
1086
|
+
!disableCurrentUserVar && variables.push(currentUserVar);
|
|
1087
|
+
return variables;
|
|
1088
|
+
} else {
|
|
1089
|
+
return [];
|
|
1090
|
+
}
|
|
1091
|
+
};
|
|
1092
|
+
const getAutomationTriggersContextVariables = (triggers, typeId, fiberySchema) => {
|
|
1093
|
+
return ___default["default"].uniqBy(___default["default"].flatten(triggers.map(trigger => {
|
|
1094
|
+
return getTriggerContextVariables(trigger, typeId, fiberySchema);
|
|
1095
|
+
})), variable => `${variable.id}_${variable.typeObject.id}`);
|
|
1096
|
+
};
|
|
1017
1097
|
const getEntityQueryVariables = schema => {
|
|
1018
1098
|
const grouped = ___default["default"].groupBy(schema.typeObjects.filter(x => x.isDomain), typeObject => typeObject.pluralTitle);
|
|
1019
1099
|
return ___default["default"].flatten(Object.values(grouped).map(group => {
|
|
@@ -1029,6 +1109,9 @@ const getEntityQueryVariables = schema => {
|
|
|
1029
1109
|
|
|
1030
1110
|
var contextVariables = {
|
|
1031
1111
|
__proto__: null,
|
|
1112
|
+
getAutomationButtonContextVariables: getAutomationButtonContextVariables,
|
|
1113
|
+
getTriggerCollectionFieldObject: getTriggerCollectionFieldObject,
|
|
1114
|
+
getAutomationTriggersContextVariables: getAutomationTriggersContextVariables,
|
|
1032
1115
|
getEntityQueryVariables: getEntityQueryVariables
|
|
1033
1116
|
};
|
|
1034
1117
|
|
package/lib/utils.js
CHANGED
|
@@ -39,9 +39,6 @@ const binaryOperations = new Set(["=", "!=", "<", ">", "<=", ">=", "in",
|
|
|
39
39
|
"q/contains", "q/not-contains", "+", "-", "q/+", "q/-", "*", "/", "and", "or",
|
|
40
40
|
//asc: obsolete. use q/and, q/or
|
|
41
41
|
"q/and", "q/or", "q/in", "q/not-in"]);
|
|
42
|
-
|
|
43
|
-
// TODO: get rid of this. Use visitors everywhere
|
|
44
|
-
const naryOperations = new Set(["and", "or", "q/and", "q/or"]);
|
|
45
42
|
const logicalOperators = new Set(["and", "or", "q/and", "q/or"]);
|
|
46
43
|
const relationalOperators = new Set(["=", "!=", "<", ">", "<=", ">="]);
|
|
47
44
|
const mathOperators = new Set(["+", "-", "*", "/", "q/+", "q/-", "q/concat"]);
|
|
@@ -54,7 +51,7 @@ const isCollectionFunctionExpression = expression =>
|
|
|
54
51
|
(expression.length === 2 || expression.length === 3) && collectionOps.has(expression[0]);
|
|
55
52
|
const isAccessFunctionExpression = expresion => expresion.length === 2 && expresion[0] === "q/access?" && isFieldExpression(expresion[1]);
|
|
56
53
|
const isBinaryExpression = expression => expression.length === 3 && binaryOperations.has(expression[0]);
|
|
57
|
-
const isNaryExpression = expression => expression.length > 1 &&
|
|
54
|
+
const isNaryExpression = expression => expression.length > 1 && logicalOperators.has(expression[0]);
|
|
58
55
|
const isVariableExpression = expression => ___default["default"].isString(expression) && expression.startsWith("$");
|
|
59
56
|
const isMultiFieldAccess = expression => ___default["default"].isArray(expression) && expression.length === 2 && !isFunctionCallExpression(expression) && expression.every(x => ___default["default"].isString(x));
|
|
60
57
|
const isMultiFieldExpression = expression => Array.isArray(expression) && !isFunctionCallExpression(expression) && expression.some(x => isMultiFieldAccess(x)) && expression.every(x => !isVariableExpression(x) && !binaryOperations.has(x) && (___default["default"].isString(x) || isMultiFieldAccess(x)));
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@fibery/expression-utils",
|
|
3
|
-
"version": "9.
|
|
3
|
+
"version": "9.3.0",
|
|
4
4
|
"description": "utils for working with fibery api expressions",
|
|
5
5
|
"exports": {
|
|
6
6
|
".": "./lib/expression-utils.js",
|
|
@@ -26,7 +26,7 @@
|
|
|
26
26
|
"dependencies": {
|
|
27
27
|
"lodash": "4.17.21",
|
|
28
28
|
"moment": "2.29.4",
|
|
29
|
-
"@fibery/helpers": "1.3.
|
|
29
|
+
"@fibery/helpers": "1.3.3"
|
|
30
30
|
},
|
|
31
31
|
"devDependencies": {
|
|
32
32
|
"@babel/core": "7.23.9",
|
|
@@ -38,7 +38,7 @@
|
|
|
38
38
|
"@fibery/eslint-config": "8.6.1"
|
|
39
39
|
},
|
|
40
40
|
"peerDependencies": {
|
|
41
|
-
"@fibery/schema": "10.2.
|
|
41
|
+
"@fibery/schema": "10.2.8"
|
|
42
42
|
},
|
|
43
43
|
"jest": {
|
|
44
44
|
"testEnvironment": "node",
|
package/types.d.ts
CHANGED
|
@@ -70,10 +70,30 @@ declare module "@fibery/expression-utils" {
|
|
|
70
70
|
relationalOperators: Set<string>;
|
|
71
71
|
mathOperators: Set<string>;
|
|
72
72
|
};
|
|
73
|
+
type TContextVariable = {
|
|
74
|
+
typeObject: TypeObject;
|
|
75
|
+
id: string;
|
|
76
|
+
title: string;
|
|
77
|
+
isCollection: boolean;
|
|
78
|
+
description: string;
|
|
79
|
+
};
|
|
73
80
|
export const contextVariables: {
|
|
74
|
-
getEntityQueryVariables: (
|
|
75
|
-
|
|
76
|
-
|
|
81
|
+
getEntityQueryVariables: (schema: Schema) => Array<TContextVariable>;
|
|
82
|
+
getAutomationButtonContextVariables: (
|
|
83
|
+
typeId: string,
|
|
84
|
+
typeName: string,
|
|
85
|
+
fiberySchema?: Schema
|
|
86
|
+
) => Array<TContextVariable>;
|
|
87
|
+
getAutomationTriggersContextVariables: (
|
|
88
|
+
triggers: Array<{trigger: string; args: Record<string, unknown>}>,
|
|
89
|
+
typeId: string,
|
|
90
|
+
fiberySchema?: Schema
|
|
91
|
+
) => Array<TContextVariable>;
|
|
92
|
+
getTriggerCollectionFieldObject: (
|
|
93
|
+
typeId: string,
|
|
94
|
+
collectionFieldId: string,
|
|
95
|
+
fiberySchema?: Schema
|
|
96
|
+
) => FieldObject | undefined;
|
|
77
97
|
};
|
|
78
98
|
export const paramsPlaceholders: {
|
|
79
99
|
formulaTodayDateParamPlaceholder: string;
|