@fibery/expression-utils 9.2.1 → 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.
@@ -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;
@@ -1011,6 +1011,89 @@ var visitors = {
1011
1011
  fieldAccessVisitorTypeAware: fieldAccessVisitorTypeAware
1012
1012
  };
1013
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
+ };
1014
1097
  const getEntityQueryVariables = schema => {
1015
1098
  const grouped = ___default["default"].groupBy(schema.typeObjects.filter(x => x.isDomain), typeObject => typeObject.pluralTitle);
1016
1099
  return ___default["default"].flatten(Object.values(grouped).map(group => {
@@ -1026,6 +1109,9 @@ const getEntityQueryVariables = schema => {
1026
1109
 
1027
1110
  var contextVariables = {
1028
1111
  __proto__: null,
1112
+ getAutomationButtonContextVariables: getAutomationButtonContextVariables,
1113
+ getTriggerCollectionFieldObject: getTriggerCollectionFieldObject,
1114
+ getAutomationTriggersContextVariables: getAutomationTriggersContextVariables,
1029
1115
  getEntityQueryVariables: getEntityQueryVariables
1030
1116
  };
1031
1117
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@fibery/expression-utils",
3
- "version": "9.2.1",
3
+ "version": "9.3.0",
4
4
  "description": "utils for working with fibery api expressions",
5
5
  "exports": {
6
6
  ".": "./lib/expression-utils.js",
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
- schema: Schema
76
- ) => Array<{typeObject: TypeObject; id: string; title: string; isCollection: boolean; description: string}>;
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;