@stackbit/cms-core 3.0.7 → 3.0.8
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/dist/.tsbuildinfo +1 -1
- package/dist/content-store.d.ts +3 -2
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +16 -10
- package/dist/content-store.js.map +1 -1
- package/dist/types/content-store-types.d.ts +1 -0
- package/dist/types/content-store-types.d.ts.map +1 -1
- package/dist/types/custom-actions.d.ts +104 -20
- package/dist/types/custom-actions.d.ts.map +1 -1
- package/dist/utils/create-update-csi-docs.d.ts +6 -6
- package/dist/utils/create-update-csi-docs.d.ts.map +1 -1
- package/dist/utils/create-update-csi-docs.js +7 -14
- package/dist/utils/create-update-csi-docs.js.map +1 -1
- package/dist/utils/custom-actions.d.ts +13 -10
- package/dist/utils/custom-actions.d.ts.map +1 -1
- package/dist/utils/custom-actions.js +530 -339
- package/dist/utils/custom-actions.js.map +1 -1
- package/dist/utils/document-hooks.d.ts.map +1 -1
- package/dist/utils/document-hooks.js +18 -0
- package/dist/utils/document-hooks.js.map +1 -1
- package/dist/utils/field-path-utils.d.ts.map +1 -1
- package/dist/utils/field-path-utils.js +1 -1
- package/dist/utils/field-path-utils.js.map +1 -1
- package/dist/utils/model-utils.d.ts.map +1 -1
- package/dist/utils/model-utils.js +7 -3
- package/dist/utils/model-utils.js.map +1 -1
- package/package.json +5 -5
- package/src/content-store.ts +21 -12
- package/src/types/content-store-types.ts +1 -0
- package/src/types/custom-actions.ts +123 -20
- package/src/utils/create-update-csi-docs.ts +7 -15
- package/src/utils/custom-actions.ts +658 -403
- package/src/utils/document-hooks.ts +18 -0
- package/src/utils/field-path-utils.ts +3 -1
- package/src/utils/model-utils.ts +9 -5
|
@@ -3,9 +3,10 @@ var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
|
3
3
|
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
4
|
};
|
|
5
5
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
-
exports.runCustomAction = exports.
|
|
6
|
+
exports.runCustomAction = exports.getRunningActions = exports.resolveCustomActionsById = exports.getGlobalAndBulkAPIActions = void 0;
|
|
7
7
|
const lodash_1 = __importDefault(require("lodash"));
|
|
8
8
|
const utils_1 = require("@stackbit/utils");
|
|
9
|
+
const uuid_1 = require("uuid");
|
|
9
10
|
const config_delegate_1 = require("./config-delegate");
|
|
10
11
|
const document_hooks_1 = require("./document-hooks");
|
|
11
12
|
const store_to_csi_docs_converter_1 = require("./store-to-csi-docs-converter");
|
|
@@ -19,10 +20,9 @@ async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionRunState
|
|
|
19
20
|
contentSourceDataById: contentSourceDataById,
|
|
20
21
|
logger: userLogger
|
|
21
22
|
});
|
|
22
|
-
|
|
23
|
+
const apiCustomActions = [];
|
|
24
|
+
for (const action of stackbitConfig.actions) {
|
|
23
25
|
const actionId = globalActionId(action);
|
|
24
|
-
const actionRunState = customActionRunStateMap[actionId];
|
|
25
|
-
let state;
|
|
26
26
|
const pageDocument = getCSIDocumentWithSourceFromDocumentSpec(currentPageDocument, configDelegate);
|
|
27
27
|
const commonStateOptions = {
|
|
28
28
|
actionId: actionId,
|
|
@@ -32,35 +32,28 @@ async function getGlobalAndBulkAPIActions({ stackbitConfig, customActionRunState
|
|
|
32
32
|
currentPageDocument: pageDocument,
|
|
33
33
|
...configDelegate
|
|
34
34
|
};
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
else {
|
|
42
|
-
state = actionRunState?.lastResultState ?? 'enabled';
|
|
43
|
-
}
|
|
44
|
-
let permissionsResult;
|
|
45
|
-
if (commonStateOptions.currentUser) {
|
|
46
|
-
const commonPermissionsOptions = {
|
|
47
|
-
...lodash_1.default.omit(commonStateOptions, ['currentUser']),
|
|
48
|
-
userContext: commonStateOptions.currentUser
|
|
49
|
-
};
|
|
50
|
-
permissionsResult = resolveCustomActionPermissions({ action, commonPermissionsOptions, contentSourceDataById });
|
|
51
|
-
}
|
|
52
|
-
return (0, utils_1.omitByNil)({
|
|
53
|
-
type: action.type,
|
|
54
|
-
actionId: actionId,
|
|
55
|
-
name: action.name,
|
|
56
|
-
label: action.label ?? lodash_1.default.startCase(action.name),
|
|
57
|
-
icon: action.icon,
|
|
58
|
-
state: state,
|
|
59
|
-
inputFields: action.inputFields,
|
|
60
|
-
hidden: action.hidden,
|
|
61
|
-
permissions: permissionsResult
|
|
35
|
+
const actionRunState = customActionRunStateMap[actionId];
|
|
36
|
+
const extendedAction = getExtendedActionFromGlobalAction({
|
|
37
|
+
action,
|
|
38
|
+
actionId,
|
|
39
|
+
actionRunState,
|
|
40
|
+
generateNewActionId: false
|
|
62
41
|
});
|
|
63
|
-
|
|
42
|
+
const state = await resolveCustomActionState({
|
|
43
|
+
extendedAction,
|
|
44
|
+
commonStateOptions
|
|
45
|
+
});
|
|
46
|
+
const permissions = resolveCustomActionPermissions({
|
|
47
|
+
extendedAction,
|
|
48
|
+
commonStateOptions
|
|
49
|
+
});
|
|
50
|
+
apiCustomActions.push(extendedActionToApiAction({
|
|
51
|
+
extendedAction,
|
|
52
|
+
state,
|
|
53
|
+
permissions
|
|
54
|
+
}));
|
|
55
|
+
}
|
|
56
|
+
return apiCustomActions;
|
|
64
57
|
}
|
|
65
58
|
exports.getGlobalAndBulkAPIActions = getGlobalAndBulkAPIActions;
|
|
66
59
|
async function resolveCustomActionsById({ getActionRequest, customActionRunStateMap, contentSourceDataById, stackbitConfig, userLogger }) {
|
|
@@ -71,7 +64,7 @@ async function resolveCustomActionsById({ getActionRequest, customActionRunState
|
|
|
71
64
|
logger: userLogger
|
|
72
65
|
});
|
|
73
66
|
for (const actionId of customActionIds) {
|
|
74
|
-
const extendedAction =
|
|
67
|
+
const extendedAction = getExtendedActionById({
|
|
75
68
|
actionId,
|
|
76
69
|
customActionRunStateMap,
|
|
77
70
|
contentSourceDataById,
|
|
@@ -91,81 +84,18 @@ async function resolveCustomActionsById({ getActionRequest, customActionRunState
|
|
|
91
84
|
...configDelegate
|
|
92
85
|
};
|
|
93
86
|
try {
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
|
|
100
|
-
|
|
101
|
-
|
|
102
|
-
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
});
|
|
107
|
-
state = await extendedAction.state({
|
|
108
|
-
...commonStateOptions,
|
|
109
|
-
document: document,
|
|
110
|
-
model: model
|
|
111
|
-
});
|
|
112
|
-
}
|
|
113
|
-
else if (extendedAction.type === 'objectModel') {
|
|
114
|
-
const stateObjectParams = getHandlerParamsForObjectModelAction({
|
|
115
|
-
extendedAction,
|
|
116
|
-
contentSourceDataById
|
|
117
|
-
});
|
|
118
|
-
state = await extendedAction.state({
|
|
119
|
-
...commonStateOptions,
|
|
120
|
-
...stateObjectParams
|
|
121
|
-
});
|
|
122
|
-
}
|
|
123
|
-
else if (extendedAction.type === 'objectField') {
|
|
124
|
-
const stateObjectParams = getHandlerParamsForObjectFieldAction({
|
|
125
|
-
extendedAction,
|
|
126
|
-
contentSourceDataById
|
|
127
|
-
});
|
|
128
|
-
state = await extendedAction.state({
|
|
129
|
-
...commonStateOptions,
|
|
130
|
-
...stateObjectParams
|
|
131
|
-
});
|
|
132
|
-
}
|
|
133
|
-
else if (extendedAction.type === 'field') {
|
|
134
|
-
const stateFieldParams = getHandlerParamsForFieldAction({
|
|
135
|
-
extendedAction,
|
|
136
|
-
contentSourceDataById
|
|
137
|
-
});
|
|
138
|
-
state = await extendedAction.state({
|
|
139
|
-
...commonStateOptions,
|
|
140
|
-
...stateFieldParams
|
|
141
|
-
});
|
|
142
|
-
}
|
|
143
|
-
else {
|
|
144
|
-
const _exhaustiveCheck = extendedAction;
|
|
145
|
-
continue;
|
|
146
|
-
}
|
|
147
|
-
}
|
|
148
|
-
else {
|
|
149
|
-
state = extendedAction.lastResultState ?? 'enabled';
|
|
150
|
-
}
|
|
151
|
-
let permissionsResult;
|
|
152
|
-
if (commonStateOptions.currentUser) {
|
|
153
|
-
const commonPermissionsOptions = {
|
|
154
|
-
...lodash_1.default.omit(commonStateOptions, ['currentUser']),
|
|
155
|
-
userContext: commonStateOptions.currentUser
|
|
156
|
-
};
|
|
157
|
-
permissionsResult = resolveCustomActionPermissions({ action: extendedAction, commonPermissionsOptions, contentSourceDataById });
|
|
158
|
-
}
|
|
159
|
-
result.push((0, utils_1.omitByNil)({
|
|
160
|
-
actionId: actionId,
|
|
161
|
-
type: storeActionTypeToAPIActionType(extendedAction.type),
|
|
162
|
-
name: extendedAction.name,
|
|
163
|
-
label: extendedAction.label,
|
|
164
|
-
icon: extendedAction.icon,
|
|
165
|
-
state: state,
|
|
166
|
-
inputFields: extendedAction.inputFields,
|
|
167
|
-
hidden: extendedAction.hidden,
|
|
168
|
-
permissions: permissionsResult
|
|
87
|
+
const state = await resolveCustomActionState({
|
|
88
|
+
extendedAction,
|
|
89
|
+
commonStateOptions
|
|
90
|
+
});
|
|
91
|
+
const permissions = resolveCustomActionPermissions({
|
|
92
|
+
extendedAction,
|
|
93
|
+
commonStateOptions
|
|
94
|
+
});
|
|
95
|
+
result.push(extendedActionToApiAction({
|
|
96
|
+
extendedAction,
|
|
97
|
+
state,
|
|
98
|
+
permissions
|
|
169
99
|
}));
|
|
170
100
|
}
|
|
171
101
|
catch (error) {
|
|
@@ -175,71 +105,211 @@ async function resolveCustomActionsById({ getActionRequest, customActionRunState
|
|
|
175
105
|
return result;
|
|
176
106
|
}
|
|
177
107
|
exports.resolveCustomActionsById = resolveCustomActionsById;
|
|
178
|
-
function
|
|
179
|
-
if (
|
|
180
|
-
return
|
|
108
|
+
async function resolveCustomActionState({ extendedAction, commonStateOptions }) {
|
|
109
|
+
if (extendedAction.isRunning) {
|
|
110
|
+
return 'running';
|
|
181
111
|
}
|
|
182
|
-
|
|
183
|
-
|
|
112
|
+
if (!('state' in extendedAction) || typeof extendedAction.state !== 'function') {
|
|
113
|
+
return 'enabled';
|
|
114
|
+
}
|
|
115
|
+
switch (extendedAction.extendedType) {
|
|
184
116
|
case 'global':
|
|
185
117
|
case 'bulk':
|
|
186
|
-
return
|
|
118
|
+
return await extendedAction.state(commonStateOptions);
|
|
187
119
|
case 'document':
|
|
188
|
-
|
|
189
|
-
|
|
190
|
-
|
|
191
|
-
|
|
192
|
-
|
|
120
|
+
return await extendedAction.state({
|
|
121
|
+
...commonStateOptions,
|
|
122
|
+
document: extendedAction.documentWithSource,
|
|
123
|
+
model: extendedAction.modelWithSource
|
|
124
|
+
});
|
|
193
125
|
case 'objectModel':
|
|
194
|
-
|
|
195
|
-
|
|
196
|
-
|
|
126
|
+
return await extendedAction.state({
|
|
127
|
+
...commonStateOptions,
|
|
128
|
+
parentDocument: extendedAction.documentWithSource,
|
|
129
|
+
parentModel: extendedAction.modelWithSource,
|
|
130
|
+
documentField: extendedAction.documentField,
|
|
131
|
+
modelField: extendedAction.modelField,
|
|
132
|
+
objectModel: extendedAction.objectModel,
|
|
133
|
+
fieldPath: extendedAction.fieldPath
|
|
197
134
|
});
|
|
198
|
-
return action.permissions?.({ ...commonPermissionsOptions, ...stateParams });
|
|
199
135
|
case 'objectField':
|
|
200
|
-
|
|
201
|
-
|
|
202
|
-
|
|
136
|
+
return await extendedAction.state({
|
|
137
|
+
...commonStateOptions,
|
|
138
|
+
parentDocument: extendedAction.documentWithSource,
|
|
139
|
+
parentModel: extendedAction.modelWithSource,
|
|
140
|
+
documentField: extendedAction.documentField,
|
|
141
|
+
modelField: extendedAction.modelField,
|
|
142
|
+
fieldPath: extendedAction.fieldPath
|
|
203
143
|
});
|
|
204
|
-
return action.permissions?.({ ...commonPermissionsOptions, ...stateParams });
|
|
205
144
|
case 'field':
|
|
206
|
-
|
|
207
|
-
|
|
208
|
-
|
|
145
|
+
return await extendedAction.state({
|
|
146
|
+
...commonStateOptions,
|
|
147
|
+
parentDocument: extendedAction.documentWithSource,
|
|
148
|
+
parentModel: extendedAction.modelWithSource,
|
|
149
|
+
documentField: extendedAction.documentField,
|
|
150
|
+
modelField: extendedAction.modelField,
|
|
151
|
+
fieldPath: extendedAction.fieldPath
|
|
209
152
|
});
|
|
210
|
-
|
|
153
|
+
default: {
|
|
154
|
+
// actions of type 'model' do not support 'state' function
|
|
155
|
+
const _exhaustiveCheck = extendedAction;
|
|
156
|
+
throw new Error(`error getting action state, action of type '${_exhaustiveCheck['type']}' does not support states`);
|
|
157
|
+
}
|
|
211
158
|
}
|
|
212
159
|
}
|
|
213
|
-
|
|
214
|
-
|
|
215
|
-
|
|
160
|
+
function resolveCustomActionPermissions({ extendedAction, commonStateOptions }) {
|
|
161
|
+
if (typeof extendedAction?.permissions !== 'function' || !commonStateOptions.currentUser) {
|
|
162
|
+
return undefined;
|
|
163
|
+
}
|
|
164
|
+
const { currentUser, ...restOptions } = commonStateOptions;
|
|
165
|
+
const commonPermissionsOptions = {
|
|
166
|
+
...restOptions,
|
|
167
|
+
userContext: currentUser
|
|
168
|
+
};
|
|
169
|
+
switch (extendedAction.extendedType) {
|
|
170
|
+
case 'global':
|
|
171
|
+
case 'bulk':
|
|
172
|
+
case 'modelGlobal':
|
|
173
|
+
case 'model':
|
|
174
|
+
case 'modelObject': {
|
|
175
|
+
return extendedAction.permissions?.(commonPermissionsOptions);
|
|
176
|
+
}
|
|
177
|
+
case 'document': {
|
|
178
|
+
return extendedAction.permissions?.({
|
|
179
|
+
...commonPermissionsOptions,
|
|
180
|
+
document: extendedAction.documentWithSource,
|
|
181
|
+
model: extendedAction.modelWithSource
|
|
182
|
+
});
|
|
183
|
+
}
|
|
184
|
+
case 'objectModel': {
|
|
185
|
+
return extendedAction.permissions?.({
|
|
186
|
+
...commonPermissionsOptions,
|
|
187
|
+
parentDocument: extendedAction.documentWithSource,
|
|
188
|
+
parentModel: extendedAction.modelWithSource,
|
|
189
|
+
documentField: extendedAction.documentField,
|
|
190
|
+
modelField: extendedAction.modelField,
|
|
191
|
+
objectModel: extendedAction.objectModel,
|
|
192
|
+
fieldPath: extendedAction.fieldPath
|
|
193
|
+
});
|
|
194
|
+
}
|
|
195
|
+
case 'objectField': {
|
|
196
|
+
return extendedAction.permissions?.({
|
|
197
|
+
...commonPermissionsOptions,
|
|
198
|
+
parentDocument: extendedAction.documentWithSource,
|
|
199
|
+
parentModel: extendedAction.modelWithSource,
|
|
200
|
+
documentField: extendedAction.documentField,
|
|
201
|
+
modelField: extendedAction.modelField,
|
|
202
|
+
fieldPath: extendedAction.fieldPath
|
|
203
|
+
});
|
|
204
|
+
}
|
|
205
|
+
case 'field': {
|
|
206
|
+
return extendedAction.permissions?.({
|
|
207
|
+
...commonPermissionsOptions,
|
|
208
|
+
parentDocument: extendedAction.documentWithSource,
|
|
209
|
+
parentModel: extendedAction.modelWithSource,
|
|
210
|
+
documentField: extendedAction.documentField,
|
|
211
|
+
modelField: extendedAction.modelField,
|
|
212
|
+
fieldPath: extendedAction.fieldPath
|
|
213
|
+
});
|
|
214
|
+
}
|
|
215
|
+
default: {
|
|
216
|
+
const _exhaustiveCheck = extendedAction;
|
|
217
|
+
return _exhaustiveCheck;
|
|
218
|
+
}
|
|
219
|
+
}
|
|
220
|
+
}
|
|
221
|
+
function getRunningActions({ getRunningActionsRequest, customActionRunStateMap, contentSourceDataById, stackbitConfig, userLogger }) {
|
|
222
|
+
if (!getRunningActionsRequest.user) {
|
|
223
|
+
return [];
|
|
224
|
+
}
|
|
225
|
+
const runningActions = [];
|
|
226
|
+
for (const [actionId, actionRunState] of Object.entries(customActionRunStateMap)) {
|
|
227
|
+
if (actionRunState.isRunning && actionRunState.userId === getRunningActionsRequest.user.id) {
|
|
228
|
+
const extendedAction = getExtendedActionById({
|
|
229
|
+
actionId,
|
|
230
|
+
customActionRunStateMap,
|
|
231
|
+
contentSourceDataById,
|
|
232
|
+
stackbitConfig
|
|
233
|
+
});
|
|
234
|
+
if (!extendedAction) {
|
|
235
|
+
userLogger.debug(`custom action with id: '${actionId}' was not found`);
|
|
236
|
+
continue;
|
|
237
|
+
}
|
|
238
|
+
runningActions.push(extendedActionToApiAction({
|
|
239
|
+
extendedAction,
|
|
240
|
+
state: 'running'
|
|
241
|
+
}));
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
return runningActions;
|
|
245
|
+
}
|
|
246
|
+
exports.getRunningActions = getRunningActions;
|
|
247
|
+
function extendedActionToApiAction({ extendedAction, state, permissions }) {
|
|
248
|
+
const apiAction = (0, utils_1.omitByNil)({
|
|
249
|
+
type: extendedAction.type,
|
|
250
|
+
name: extendedAction.name,
|
|
251
|
+
label: extendedAction.label,
|
|
252
|
+
actionId: extendedAction.actionId,
|
|
253
|
+
userId: extendedAction.userId,
|
|
254
|
+
icon: extendedAction.icon,
|
|
255
|
+
state: state,
|
|
256
|
+
permissions: permissions,
|
|
257
|
+
models: extendedAction.extendedType === 'modelGlobal' && 'models' in extendedAction ? extendedAction.models : undefined,
|
|
258
|
+
inputFields: extendedAction.inputFields,
|
|
259
|
+
hidden: extendedAction.hidden
|
|
260
|
+
});
|
|
261
|
+
return apiAction;
|
|
262
|
+
}
|
|
263
|
+
async function runCustomAction({ runActionRequest, customActionRunStateMap, contentSourceDataById, stackbitConfig, userLogger, presets, onProgress }) {
|
|
264
|
+
const extendedAction = getExtendedActionById({
|
|
216
265
|
actionId: runActionRequest.actionId,
|
|
217
266
|
customActionRunStateMap,
|
|
218
267
|
contentSourceDataById,
|
|
219
|
-
stackbitConfig
|
|
268
|
+
stackbitConfig,
|
|
269
|
+
generateNewActionId: true
|
|
220
270
|
});
|
|
221
271
|
if (!extendedAction) {
|
|
222
272
|
throw new Error(`Error running action: action not found, action name: '${runActionRequest.actionName}' action ID: '${runActionRequest.actionId}'.`);
|
|
223
273
|
}
|
|
224
|
-
|
|
225
|
-
|
|
226
|
-
throw new Error(`Error running action: action is not enabled, action name: '${runActionRequest.actionName}' action ID: '${runActionRequest.actionId}'.`);
|
|
274
|
+
if (extendedAction.isRunning) {
|
|
275
|
+
throw new Error(`Error running action: action '${runActionRequest.actionName}' with ID '${runActionRequest.actionId}' is already running.`);
|
|
227
276
|
}
|
|
277
|
+
const actionId = extendedAction.actionId;
|
|
278
|
+
let isRunning = true;
|
|
228
279
|
try {
|
|
229
280
|
const actionLogger = userLogger.createLogger({ label: `action:${extendedAction.name}` });
|
|
230
281
|
const configDelegate = (0, config_delegate_1.createConfigDelegate)({ contentSourceDataById: contentSourceDataById, logger: actionLogger });
|
|
231
282
|
const currentPageDocument = getCSIDocumentWithSourceFromDocumentSpec(runActionRequest.currentPageDocument, configDelegate);
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
|
|
283
|
+
const actionRunState = {
|
|
284
|
+
isRunning: true,
|
|
285
|
+
userId: runActionRequest.user?.id
|
|
235
286
|
};
|
|
287
|
+
customActionRunStateMap[actionId] = actionRunState;
|
|
288
|
+
const inputData = runActionRequest.inputData ?? {};
|
|
289
|
+
if (runActionRequest.actionType === 'model' && 'presetId' in runActionRequest) {
|
|
290
|
+
inputData.presetData = presets[runActionRequest.presetId]?.data;
|
|
291
|
+
}
|
|
236
292
|
const commonRunOptions = {
|
|
237
|
-
actionId:
|
|
238
|
-
inputData:
|
|
293
|
+
actionId: actionId,
|
|
294
|
+
inputData: inputData,
|
|
239
295
|
currentLocale: runActionRequest.locale,
|
|
240
296
|
currentUser: runActionRequest.user,
|
|
241
297
|
currentPageUrl: runActionRequest.pageUrl,
|
|
242
298
|
currentPageDocument: currentPageDocument,
|
|
299
|
+
progress: ({ message, percent }) => {
|
|
300
|
+
if (!isRunning) {
|
|
301
|
+
return;
|
|
302
|
+
}
|
|
303
|
+
onProgress((0, utils_1.omitByNil)({
|
|
304
|
+
actionId: actionId,
|
|
305
|
+
actionName: extendedAction.name,
|
|
306
|
+
actionType: extendedAction.type,
|
|
307
|
+
userId: runActionRequest.user?.id,
|
|
308
|
+
state: 'running',
|
|
309
|
+
message,
|
|
310
|
+
percent
|
|
311
|
+
}));
|
|
312
|
+
},
|
|
243
313
|
getContentSourceActionsForSource: (0, document_hooks_1.getContentSourceActionsForSourceThunk)({
|
|
244
314
|
getContentSourceDataById: () => contentSourceDataById,
|
|
245
315
|
logger: userLogger,
|
|
@@ -250,10 +320,13 @@ function runCustomAction({ runActionRequest, customActionRunStateMap, contentSou
|
|
|
250
320
|
...configDelegate
|
|
251
321
|
};
|
|
252
322
|
let promise;
|
|
253
|
-
if (extendedAction.
|
|
323
|
+
if (extendedAction.extendedType === 'global') {
|
|
254
324
|
promise = extendedAction.run(commonRunOptions);
|
|
255
325
|
}
|
|
256
|
-
else if (extendedAction.
|
|
326
|
+
else if (extendedAction.extendedType === 'bulk') {
|
|
327
|
+
if (!('documents' in runActionRequest) || !Array.isArray(runActionRequest.documents)) {
|
|
328
|
+
throw new Error(`Bulk action run request must contain array of documents, action name: '${runActionRequest.actionName}', action ID: '${runActionRequest.actionId}'.`);
|
|
329
|
+
}
|
|
257
330
|
const documents = runActionRequest.documents.map((documentSpec) => {
|
|
258
331
|
const { document } = getCSIDocumentAndModelWithSourceFromDocumentSpec({
|
|
259
332
|
documentSpec,
|
|
@@ -266,114 +339,167 @@ function runCustomAction({ runActionRequest, customActionRunStateMap, contentSou
|
|
|
266
339
|
documents
|
|
267
340
|
});
|
|
268
341
|
}
|
|
269
|
-
else if (extendedAction.
|
|
270
|
-
|
|
271
|
-
|
|
272
|
-
|
|
342
|
+
else if (extendedAction.extendedType === 'modelGlobal') {
|
|
343
|
+
if (!('srcType' in runActionRequest) || !('srcProjectId' in runActionRequest) || !('modelName' in runActionRequest)) {
|
|
344
|
+
throw new Error(`Global model action run request must contain srcType, srcProjectId and modelName, action name: '${runActionRequest.actionName}', action ID: '${runActionRequest.actionId}'.`);
|
|
345
|
+
}
|
|
346
|
+
actionRunState.modelSpec = {
|
|
347
|
+
srcType: runActionRequest.srcType,
|
|
348
|
+
srcProjectId: runActionRequest.srcProjectId,
|
|
349
|
+
modelName: runActionRequest.modelName
|
|
350
|
+
};
|
|
351
|
+
const runOptions = getRunParamsForModelAction({
|
|
352
|
+
modelSpec: actionRunState.modelSpec,
|
|
353
|
+
location: runActionRequest.location,
|
|
354
|
+
contentSourceDataById,
|
|
355
|
+
getContentSourceActionsForSource: commonRunOptions.getContentSourceActionsForSource
|
|
356
|
+
});
|
|
357
|
+
promise = extendedAction.run({
|
|
358
|
+
...commonRunOptions,
|
|
359
|
+
...runOptions
|
|
360
|
+
});
|
|
361
|
+
}
|
|
362
|
+
else if (extendedAction.extendedType === 'model') {
|
|
363
|
+
const runOptions = getRunParamsForModelAction({
|
|
364
|
+
modelSpec: extendedAction.modelSpec,
|
|
365
|
+
location: runActionRequest.location,
|
|
366
|
+
contentSourceDataById,
|
|
367
|
+
getContentSourceActionsForSource: commonRunOptions.getContentSourceActionsForSource
|
|
368
|
+
});
|
|
369
|
+
promise = extendedAction.run({
|
|
370
|
+
...commonRunOptions,
|
|
371
|
+
...runOptions
|
|
273
372
|
});
|
|
373
|
+
}
|
|
374
|
+
else if (extendedAction.extendedType === 'document') {
|
|
274
375
|
promise = extendedAction.run({
|
|
275
376
|
...commonRunOptions,
|
|
276
|
-
document,
|
|
277
|
-
model,
|
|
377
|
+
document: extendedAction.documentWithSource,
|
|
378
|
+
model: extendedAction.modelWithSource,
|
|
278
379
|
contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
|
|
279
|
-
srcType: extendedAction.
|
|
280
|
-
srcProjectId: extendedAction.
|
|
380
|
+
srcType: extendedAction.documentWithSource.srcType,
|
|
381
|
+
srcProjectId: extendedAction.documentWithSource.srcProjectId
|
|
281
382
|
})
|
|
282
383
|
});
|
|
283
384
|
}
|
|
284
|
-
else if (extendedAction.
|
|
285
|
-
const handlerObjectParams = getHandlerParamsForObjectModelAction({
|
|
286
|
-
extendedAction,
|
|
287
|
-
contentSourceDataById
|
|
288
|
-
});
|
|
385
|
+
else if (extendedAction.extendedType === 'modelObject') {
|
|
289
386
|
promise = extendedAction.run({
|
|
290
387
|
...commonRunOptions,
|
|
291
|
-
|
|
388
|
+
parentDocument: extendedAction.documentWithSource,
|
|
389
|
+
parentModel: extendedAction.modelWithSource,
|
|
390
|
+
modelField: extendedAction.modelField,
|
|
391
|
+
objectModel: extendedAction.objectModel,
|
|
392
|
+
fieldPath: extendedAction.fieldPath,
|
|
393
|
+
location: runActionRequest.location,
|
|
292
394
|
contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
|
|
293
|
-
srcType: extendedAction.
|
|
294
|
-
srcProjectId: extendedAction.
|
|
395
|
+
srcType: extendedAction.documentWithSource.srcType,
|
|
396
|
+
srcProjectId: extendedAction.documentWithSource.srcProjectId
|
|
295
397
|
})
|
|
296
398
|
});
|
|
297
399
|
}
|
|
298
|
-
else if (extendedAction.
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
400
|
+
else if (extendedAction.extendedType === 'objectModel') {
|
|
401
|
+
promise = extendedAction.run({
|
|
402
|
+
...commonRunOptions,
|
|
403
|
+
parentDocument: extendedAction.documentWithSource,
|
|
404
|
+
parentModel: extendedAction.modelWithSource,
|
|
405
|
+
documentField: extendedAction.documentField,
|
|
406
|
+
modelField: extendedAction.modelField,
|
|
407
|
+
objectModel: extendedAction.objectModel,
|
|
408
|
+
fieldPath: extendedAction.fieldPath,
|
|
409
|
+
contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
|
|
410
|
+
srcType: extendedAction.documentWithSource.srcType,
|
|
411
|
+
srcProjectId: extendedAction.documentWithSource.srcProjectId
|
|
412
|
+
})
|
|
302
413
|
});
|
|
414
|
+
}
|
|
415
|
+
else if (extendedAction.extendedType === 'objectField') {
|
|
303
416
|
promise = extendedAction.run({
|
|
304
417
|
...commonRunOptions,
|
|
305
|
-
|
|
418
|
+
parentDocument: extendedAction.documentWithSource,
|
|
419
|
+
parentModel: extendedAction.modelWithSource,
|
|
420
|
+
documentField: extendedAction.documentField,
|
|
421
|
+
modelField: extendedAction.modelField,
|
|
422
|
+
fieldPath: extendedAction.fieldPath,
|
|
306
423
|
contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
|
|
307
|
-
srcType: extendedAction.
|
|
308
|
-
srcProjectId: extendedAction.
|
|
424
|
+
srcType: extendedAction.documentWithSource.srcType,
|
|
425
|
+
srcProjectId: extendedAction.documentWithSource.srcProjectId
|
|
309
426
|
})
|
|
310
427
|
});
|
|
311
428
|
}
|
|
312
|
-
else if (extendedAction.
|
|
313
|
-
const handlerFieldParams = getHandlerParamsForFieldAction({ extendedAction, contentSourceDataById });
|
|
429
|
+
else if (extendedAction.extendedType === 'field') {
|
|
314
430
|
promise = extendedAction.run({
|
|
315
431
|
...commonRunOptions,
|
|
316
|
-
|
|
432
|
+
parentDocument: extendedAction.documentWithSource,
|
|
433
|
+
parentModel: extendedAction.modelWithSource,
|
|
434
|
+
documentField: extendedAction.documentField,
|
|
435
|
+
modelField: extendedAction.modelField,
|
|
436
|
+
fieldPath: extendedAction.fieldPath,
|
|
317
437
|
contentSourceActions: commonRunOptions.getContentSourceActionsForSource({
|
|
318
|
-
srcType: extendedAction.
|
|
319
|
-
srcProjectId: extendedAction.
|
|
438
|
+
srcType: extendedAction.documentWithSource.srcType,
|
|
439
|
+
srcProjectId: extendedAction.documentWithSource.srcProjectId
|
|
320
440
|
})
|
|
321
441
|
});
|
|
322
442
|
}
|
|
323
443
|
else {
|
|
324
|
-
|
|
444
|
+
const _exhaustiveCheck = extendedAction;
|
|
445
|
+
throw new Error(`action of type ${_exhaustiveCheck['type']} is not supported`);
|
|
325
446
|
}
|
|
326
|
-
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
331
|
-
|
|
332
|
-
|
|
333
|
-
|
|
334
|
-
|
|
447
|
+
onProgress((0, utils_1.omitByNil)({
|
|
448
|
+
actionId: actionId,
|
|
449
|
+
actionName: extendedAction.name,
|
|
450
|
+
actionType: extendedAction.type,
|
|
451
|
+
userId: runActionRequest.user?.id,
|
|
452
|
+
state: 'running'
|
|
453
|
+
}));
|
|
454
|
+
promise
|
|
455
|
+
.then(async (actionResult) => {
|
|
456
|
+
isRunning = false;
|
|
457
|
+
delete customActionRunStateMap[actionId];
|
|
458
|
+
userLogger.debug(`Action completed: ${actionId}`);
|
|
459
|
+
const state = await resolveCustomActionState({
|
|
460
|
+
extendedAction,
|
|
461
|
+
commonStateOptions: {
|
|
462
|
+
actionId: actionId,
|
|
463
|
+
currentLocale: runActionRequest.locale,
|
|
464
|
+
currentUser: runActionRequest.user,
|
|
465
|
+
currentPageUrl: runActionRequest.pageUrl,
|
|
466
|
+
currentPageDocument: currentPageDocument,
|
|
467
|
+
...configDelegate
|
|
468
|
+
}
|
|
469
|
+
});
|
|
470
|
+
onProgress((0, utils_1.omitByNil)({
|
|
471
|
+
actionId: actionId,
|
|
335
472
|
actionName: extendedAction.name,
|
|
336
|
-
actionType:
|
|
337
|
-
|
|
338
|
-
state:
|
|
473
|
+
actionType: extendedAction.type,
|
|
474
|
+
userId: runActionRequest.user?.id,
|
|
475
|
+
state: extendedAction.type === 'model' ? 'finished' : state,
|
|
339
476
|
success: actionResult?.success,
|
|
340
477
|
error: actionResult?.error,
|
|
341
478
|
result: actionResult?.result
|
|
342
479
|
}));
|
|
343
480
|
})
|
|
344
481
|
.catch((error) => {
|
|
345
|
-
|
|
346
|
-
|
|
347
|
-
|
|
348
|
-
|
|
349
|
-
userLogger.debug(`Error running action: ${error.message}`);
|
|
350
|
-
return Promise.resolve({
|
|
482
|
+
isRunning = false;
|
|
483
|
+
delete customActionRunStateMap[actionId];
|
|
484
|
+
userLogger.warn(`Error running action: ${error.stack ?? error.message}`);
|
|
485
|
+
onProgress((0, utils_1.omitByNil)({
|
|
351
486
|
actionId: extendedAction.actionId,
|
|
352
487
|
actionName: extendedAction.name,
|
|
353
|
-
actionType:
|
|
354
|
-
|
|
355
|
-
state:
|
|
488
|
+
actionType: extendedAction.type,
|
|
489
|
+
userId: runActionRequest.user?.id,
|
|
490
|
+
state: extendedAction.type === 'model' ? 'failed' : 'enabled',
|
|
356
491
|
error: `Error running action: ${error.message}`
|
|
357
|
-
});
|
|
492
|
+
}));
|
|
358
493
|
});
|
|
359
494
|
}
|
|
360
495
|
catch (error) {
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
364
|
-
|
|
365
|
-
|
|
366
|
-
}
|
|
367
|
-
userLogger.debug(`Error running action: ${error.message}`);
|
|
368
|
-
return Promise.resolve({
|
|
369
|
-
actionId: runActionRequest.actionId,
|
|
370
|
-
actionName: extendedAction?.name ?? runActionRequest.actionName,
|
|
371
|
-
actionType: storeActionTypeToAPIActionType(extendedAction?.type) ?? runActionRequest.actionType,
|
|
372
|
-
// TODO: resolve the state if state function is defined
|
|
373
|
-
state: prevResultState ?? 'enabled',
|
|
374
|
-
error: `Error running action: ${error.message}`
|
|
375
|
-
});
|
|
496
|
+
isRunning = false;
|
|
497
|
+
delete customActionRunStateMap[actionId];
|
|
498
|
+
userLogger.warn(`Error running action: ${error.stack ?? error.message}`);
|
|
499
|
+
// rethrow the error to return erroneous response
|
|
500
|
+
throw error;
|
|
376
501
|
}
|
|
502
|
+
return { actionId };
|
|
377
503
|
}
|
|
378
504
|
exports.runCustomAction = runCustomAction;
|
|
379
505
|
function getCSIDocumentWithSourceFromDocumentSpec(documentSpec, configDelegate) {
|
|
@@ -385,76 +511,24 @@ function getCSIDocumentWithSourceFromDocumentSpec(documentSpec, configDelegate)
|
|
|
385
511
|
})
|
|
386
512
|
: undefined;
|
|
387
513
|
}
|
|
388
|
-
function
|
|
389
|
-
const
|
|
390
|
-
|
|
391
|
-
|
|
392
|
-
|
|
393
|
-
|
|
394
|
-
throw new Error(`object document field not found at field path: ${extendedAction.fieldPath.join('.')}`);
|
|
395
|
-
}
|
|
396
|
-
const documentField = fieldActionCommonParams.documentField;
|
|
397
|
-
const documentSpec = extendedAction.documentSpec;
|
|
398
|
-
const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(documentSpec.srcType, documentSpec.srcProjectId, contentSourceDataById);
|
|
399
|
-
const objectModel = contentSourceData.modelMap[documentField.modelName];
|
|
400
|
-
if (!objectModel || objectModel.type !== 'object') {
|
|
401
|
-
throw new Error(`object model '${documentField.modelName}' not found`);
|
|
402
|
-
}
|
|
403
|
-
return {
|
|
404
|
-
...fieldActionCommonParams,
|
|
405
|
-
documentField,
|
|
406
|
-
modelField: fieldActionCommonParams.modelField,
|
|
407
|
-
objectModel: {
|
|
408
|
-
...objectModel,
|
|
409
|
-
srcType: documentSpec.srcType,
|
|
410
|
-
srcProjectId: documentSpec.srcProjectId
|
|
411
|
-
}
|
|
412
|
-
};
|
|
413
|
-
}
|
|
414
|
-
function getHandlerParamsForObjectFieldAction({ extendedAction, contentSourceDataById }) {
|
|
415
|
-
const fieldActionCommonParams = getHandlerParamsForFieldAction({
|
|
416
|
-
extendedAction,
|
|
417
|
-
contentSourceDataById
|
|
418
|
-
});
|
|
419
|
-
if (!fieldActionCommonParams.documentField) {
|
|
420
|
-
throw new Error(`object document field not found at field path: ${extendedAction.fieldPath.join('.')}`);
|
|
421
|
-
}
|
|
422
|
-
return {
|
|
423
|
-
...fieldActionCommonParams,
|
|
424
|
-
documentField: fieldActionCommonParams.documentField,
|
|
425
|
-
modelField: fieldActionCommonParams.modelField
|
|
426
|
-
};
|
|
427
|
-
}
|
|
428
|
-
function getHandlerParamsForFieldAction({ extendedAction, contentSourceDataById }) {
|
|
429
|
-
const documentSpec = extendedAction.documentSpec;
|
|
430
|
-
const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(documentSpec.srcType, documentSpec.srcProjectId, contentSourceDataById);
|
|
431
|
-
const document = contentSourceData.documentMap[documentSpec.srcDocumentId];
|
|
432
|
-
const csiDocument = contentSourceData.csiDocumentMap[documentSpec.srcDocumentId];
|
|
433
|
-
if (!document || !csiDocument) {
|
|
434
|
-
throw new Error(`document not found for srcType: ${documentSpec.srcType}, srcProjectId: ${documentSpec.srcProjectId}, srcDocumentId: ${documentSpec.srcDocumentId}`);
|
|
435
|
-
}
|
|
436
|
-
const model = contentSourceData.modelMap[document.srcModelName];
|
|
437
|
-
if (!model) {
|
|
438
|
-
throw new Error(`model '${document.srcModelName}' not found`);
|
|
514
|
+
function getRunParamsForModelAction({ modelSpec, location, contentSourceDataById, getContentSourceActionsForSource }) {
|
|
515
|
+
const { srcType, srcProjectId, modelName } = modelSpec;
|
|
516
|
+
const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(srcType, srcProjectId, contentSourceDataById);
|
|
517
|
+
const actionModel = contentSourceData.modelMap[modelName];
|
|
518
|
+
if (!actionModel) {
|
|
519
|
+
throw new Error(`model '${modelName}' not found`);
|
|
439
520
|
}
|
|
440
|
-
const mappedCSIDocument = (0, store_to_csi_docs_converter_1.mapStoreDocumentToCSIDocumentWithSource)({ document, csiDocument });
|
|
441
|
-
// the documentField should be localized because fieldPath includes locales
|
|
442
|
-
const { modelField, documentField } = (0, field_path_utils_1.getModelAndDocumentFieldForLocalizedFieldPath)({
|
|
443
|
-
document,
|
|
444
|
-
fieldPath: extendedAction.fieldPath,
|
|
445
|
-
modelMap: contentSourceData.modelMap
|
|
446
|
-
});
|
|
447
|
-
const csiDocumentField = documentField ? (0, store_to_csi_docs_converter_1.mapStoreFieldToCSIField)(documentField) : undefined;
|
|
448
521
|
return {
|
|
449
|
-
|
|
450
|
-
|
|
451
|
-
|
|
452
|
-
|
|
453
|
-
srcProjectId: documentSpec.srcProjectId
|
|
522
|
+
actionModel: {
|
|
523
|
+
...actionModel,
|
|
524
|
+
srcType,
|
|
525
|
+
srcProjectId
|
|
454
526
|
},
|
|
455
|
-
|
|
456
|
-
|
|
457
|
-
|
|
527
|
+
location,
|
|
528
|
+
contentSourceActions: getContentSourceActionsForSource({
|
|
529
|
+
srcType,
|
|
530
|
+
srcProjectId
|
|
531
|
+
})
|
|
458
532
|
};
|
|
459
533
|
}
|
|
460
534
|
function getCSIDocumentAndModelWithSourceFromDocumentSpec({ documentSpec, contentSourceDataById }) {
|
|
@@ -464,16 +538,16 @@ function getCSIDocumentAndModelWithSourceFromDocumentSpec({ documentSpec, conten
|
|
|
464
538
|
if (!document || !csiDocument) {
|
|
465
539
|
throw new Error(`document not found, srcType: ${documentSpec.srcType}, srcProjectId: ${documentSpec.srcProjectId}, srcDocumentId: ${documentSpec.srcDocumentId}`);
|
|
466
540
|
}
|
|
467
|
-
const
|
|
541
|
+
const documentWithSource = (0, store_to_csi_docs_converter_1.mapStoreDocumentToCSIDocumentWithSource)({
|
|
468
542
|
document,
|
|
469
543
|
csiDocument
|
|
470
544
|
});
|
|
471
|
-
const model = contentSourceData.modelMap[
|
|
545
|
+
const model = contentSourceData.modelMap[documentWithSource.modelName];
|
|
472
546
|
if (!model) {
|
|
473
|
-
throw new Error(`model '${
|
|
547
|
+
throw new Error(`model '${documentWithSource.modelName}' not found`);
|
|
474
548
|
}
|
|
475
549
|
return {
|
|
476
|
-
document:
|
|
550
|
+
document: documentWithSource,
|
|
477
551
|
model: {
|
|
478
552
|
...model,
|
|
479
553
|
srcType: documentSpec.srcType,
|
|
@@ -481,14 +555,10 @@ function getCSIDocumentAndModelWithSourceFromDocumentSpec({ documentSpec, conten
|
|
|
481
555
|
}
|
|
482
556
|
};
|
|
483
557
|
}
|
|
484
|
-
function
|
|
485
|
-
if (storeActionType === 'objectModel' || storeActionType === 'objectField') {
|
|
486
|
-
return 'object';
|
|
487
|
-
}
|
|
488
|
-
return storeActionType;
|
|
489
|
-
}
|
|
490
|
-
function findCustomActionById({ actionId, customActionRunStateMap, contentSourceDataById, stackbitConfig }) {
|
|
558
|
+
function getExtendedActionById({ actionId, customActionRunStateMap, contentSourceDataById, stackbitConfig, generateNewActionId }) {
|
|
491
559
|
const actionRunState = customActionRunStateMap[actionId];
|
|
560
|
+
// Check if this is a global action defined in the actions array of stackbit.config.
|
|
561
|
+
// Their types can be "global", "bulk" or "model"
|
|
492
562
|
if (isGlobalActionId(actionId)) {
|
|
493
563
|
if (!stackbitConfig || !Array.isArray(stackbitConfig.actions)) {
|
|
494
564
|
return undefined;
|
|
@@ -498,47 +568,84 @@ function findCustomActionById({ actionId, customActionRunStateMap, contentSource
|
|
|
498
568
|
if (!action) {
|
|
499
569
|
return undefined;
|
|
500
570
|
}
|
|
571
|
+
return getExtendedActionFromGlobalAction({
|
|
572
|
+
action,
|
|
573
|
+
actionId,
|
|
574
|
+
actionRunState,
|
|
575
|
+
generateNewActionId
|
|
576
|
+
});
|
|
577
|
+
}
|
|
578
|
+
const { srcType, srcProjectId, modelName, srcDocumentId, fieldPath, actionName } = parseActionId(actionId) ?? {};
|
|
579
|
+
if (!srcType || !srcProjectId || !actionName) {
|
|
580
|
+
return undefined;
|
|
581
|
+
}
|
|
582
|
+
const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(srcType, srcProjectId, contentSourceDataById);
|
|
583
|
+
// If "modelName" was specified without "srcDocumentId" then it has to be a
|
|
584
|
+
// "model" action defined in a model of type 'page' or 'data'.
|
|
585
|
+
if (!srcDocumentId && modelName) {
|
|
586
|
+
const model = contentSourceData.modelMap[modelName];
|
|
587
|
+
if (!model || (model.type !== 'page' && model.type !== 'data')) {
|
|
588
|
+
return undefined;
|
|
589
|
+
}
|
|
590
|
+
const action = model.actions?.find((action) => action.name === actionName);
|
|
591
|
+
if (!action || action.type !== 'model') {
|
|
592
|
+
return undefined;
|
|
593
|
+
}
|
|
501
594
|
return {
|
|
502
595
|
...action,
|
|
503
|
-
|
|
504
|
-
|
|
505
|
-
|
|
506
|
-
|
|
596
|
+
extendedType: 'model',
|
|
597
|
+
actionId: generateNewActionId && !actionRunState
|
|
598
|
+
? JSON.stringify({
|
|
599
|
+
srcType,
|
|
600
|
+
srcProjectId,
|
|
601
|
+
modelName,
|
|
602
|
+
actionName,
|
|
603
|
+
actionRunId: (0, uuid_1.v4)().substring(0, 8)
|
|
604
|
+
})
|
|
605
|
+
: actionId,
|
|
606
|
+
label: getActionLabel(action),
|
|
607
|
+
modelSpec: { srcType, srcProjectId, modelName },
|
|
608
|
+
userId: actionRunState?.userId,
|
|
609
|
+
isRunning: actionRunState?.isRunning
|
|
507
610
|
};
|
|
508
611
|
}
|
|
509
|
-
|
|
510
|
-
if (!srcType || !srcProjectId || !srcDocumentId || !actionName) {
|
|
612
|
+
if (!srcDocumentId) {
|
|
511
613
|
return undefined;
|
|
512
614
|
}
|
|
513
|
-
const documentSpec = { srcType, srcProjectId, srcDocumentId };
|
|
514
|
-
const contentSourceData = (0, content_store_utils_1.getContentSourceDataByTypeAndProjectIdOrThrow)(srcType, srcProjectId, contentSourceDataById);
|
|
515
615
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
516
|
-
|
|
616
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
617
|
+
if (!document || !csiDocument) {
|
|
517
618
|
return undefined;
|
|
518
619
|
}
|
|
519
|
-
const
|
|
520
|
-
const model = contentSourceData.modelMap[modelName];
|
|
620
|
+
const model = contentSourceData.modelMap[document.srcModelName];
|
|
521
621
|
// The model of a document is always 'page' or 'data',
|
|
522
|
-
// this condition helps
|
|
622
|
+
// this condition helps Typescript infer the right type of model.actions
|
|
523
623
|
if (!model || (model.type !== 'page' && model.type !== 'data')) {
|
|
524
624
|
return undefined;
|
|
525
625
|
}
|
|
626
|
+
const documentWithSource = (0, store_to_csi_docs_converter_1.mapStoreDocumentToCSIDocumentWithSource)({
|
|
627
|
+
document,
|
|
628
|
+
csiDocument
|
|
629
|
+
});
|
|
630
|
+
const modelWithSource = { ...model, srcType, srcProjectId };
|
|
526
631
|
if (typeof fieldPath === 'undefined') {
|
|
527
|
-
// fieldPath was not provided,
|
|
528
|
-
// and the action type must be 'document'
|
|
632
|
+
// If fieldPath was not provided, then the model must be of type "page" or "data",
|
|
633
|
+
// and the action type must be of type 'document'
|
|
529
634
|
const action = model.actions?.find((action) => action.name === actionName);
|
|
530
|
-
if (!action) {
|
|
635
|
+
if (!action || action.type !== 'document') {
|
|
531
636
|
return undefined;
|
|
532
637
|
}
|
|
533
638
|
return {
|
|
534
639
|
// if configuration is updated, the new action properties will override the stored action properties
|
|
535
640
|
...action,
|
|
536
641
|
type: 'document',
|
|
642
|
+
extendedType: 'document',
|
|
537
643
|
actionId,
|
|
538
|
-
label:
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
644
|
+
label: getActionLabel(action),
|
|
645
|
+
documentWithSource,
|
|
646
|
+
modelWithSource,
|
|
647
|
+
userId: actionRunState?.userId,
|
|
648
|
+
isRunning: actionRunState?.isRunning
|
|
542
649
|
};
|
|
543
650
|
}
|
|
544
651
|
else {
|
|
@@ -547,53 +654,133 @@ function findCustomActionById({ actionId, customActionRunStateMap, contentSource
|
|
|
547
654
|
fieldPath,
|
|
548
655
|
modelMap: contentSourceData.modelMap
|
|
549
656
|
});
|
|
657
|
+
const csiDocumentField = documentField ? (0, store_to_csi_docs_converter_1.mapStoreFieldToCSIField)(documentField) : undefined;
|
|
658
|
+
// Find an action with the provided 'actionName' in model.field.actions
|
|
550
659
|
if ('actions' in modelField && Array.isArray(modelField.actions)) {
|
|
551
660
|
const action = modelField.actions.find((action) => action.name === actionName);
|
|
552
661
|
if (action) {
|
|
662
|
+
if (action.type === 'object') {
|
|
663
|
+
if (!csiDocumentField) {
|
|
664
|
+
throw new Error(`object document field not found at field path: ${fieldPath.join('.')}`);
|
|
665
|
+
}
|
|
666
|
+
if (modelField.type !== 'object' || csiDocumentField.type !== 'object') {
|
|
667
|
+
// action of type 'object' cannot be defined on field other than 'object'
|
|
668
|
+
return undefined;
|
|
669
|
+
}
|
|
670
|
+
return {
|
|
671
|
+
// if configuration is updated, the new action properties will override the stored action properties
|
|
672
|
+
...action,
|
|
673
|
+
extendedType: 'objectField',
|
|
674
|
+
actionId,
|
|
675
|
+
label: getActionLabel(action),
|
|
676
|
+
documentWithSource,
|
|
677
|
+
modelWithSource,
|
|
678
|
+
documentField: csiDocumentField,
|
|
679
|
+
modelField: modelField,
|
|
680
|
+
fieldPath,
|
|
681
|
+
userId: actionRunState?.userId,
|
|
682
|
+
isRunning: actionRunState?.isRunning
|
|
683
|
+
};
|
|
684
|
+
}
|
|
553
685
|
return {
|
|
554
686
|
// if configuration is updated, the new action properties will override the stored action properties
|
|
555
687
|
...action,
|
|
556
|
-
type:
|
|
688
|
+
type: 'field',
|
|
689
|
+
extendedType: 'field',
|
|
557
690
|
actionId,
|
|
558
|
-
label:
|
|
559
|
-
|
|
691
|
+
label: getActionLabel(action),
|
|
692
|
+
documentWithSource,
|
|
693
|
+
modelWithSource,
|
|
694
|
+
documentField: csiDocumentField,
|
|
695
|
+
modelField,
|
|
560
696
|
fieldPath,
|
|
561
|
-
|
|
562
|
-
|
|
697
|
+
userId: actionRunState?.userId,
|
|
698
|
+
isRunning: actionRunState?.isRunning
|
|
563
699
|
};
|
|
564
700
|
}
|
|
565
701
|
}
|
|
702
|
+
// If the field is of type "model", find an action with the provided 'actionName' in model.actions
|
|
566
703
|
if (modelField.type === 'model') {
|
|
567
|
-
if (
|
|
568
|
-
|
|
569
|
-
|
|
570
|
-
|
|
571
|
-
|
|
572
|
-
|
|
573
|
-
|
|
574
|
-
|
|
575
|
-
|
|
576
|
-
|
|
704
|
+
if (modelName) {
|
|
705
|
+
const objectModel = contentSourceData.modelMap[modelName];
|
|
706
|
+
if (!objectModel || objectModel.type !== 'object') {
|
|
707
|
+
return undefined;
|
|
708
|
+
}
|
|
709
|
+
// This is a nested model of type "object", so the action must be CustomActionModelObject
|
|
710
|
+
const action = objectModel.actions?.find((action) => action.name === actionName);
|
|
711
|
+
if (!action || action.type !== 'model') {
|
|
712
|
+
return undefined;
|
|
713
|
+
}
|
|
714
|
+
return {
|
|
715
|
+
// if configuration is updated, the new action properties will override the stored action properties
|
|
716
|
+
...action,
|
|
717
|
+
extendedType: 'modelObject',
|
|
718
|
+
actionId,
|
|
719
|
+
label: getActionLabel(action),
|
|
720
|
+
documentWithSource,
|
|
721
|
+
modelWithSource,
|
|
722
|
+
modelField,
|
|
723
|
+
objectModel: { ...objectModel, srcType, srcProjectId },
|
|
724
|
+
fieldPath,
|
|
725
|
+
userId: actionRunState?.userId,
|
|
726
|
+
isRunning: actionRunState?.isRunning
|
|
727
|
+
};
|
|
577
728
|
}
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
|
|
729
|
+
else {
|
|
730
|
+
if (!csiDocumentField || csiDocumentField.type !== 'model' || csiDocumentField.localized) {
|
|
731
|
+
return undefined;
|
|
732
|
+
}
|
|
733
|
+
const modelName = csiDocumentField.modelName;
|
|
734
|
+
const objectModel = contentSourceData.modelMap[modelName];
|
|
735
|
+
if (!objectModel || objectModel.type !== 'object') {
|
|
736
|
+
return undefined;
|
|
737
|
+
}
|
|
738
|
+
// This is a nested model of type "object", so the action must be CustomActionObjectModel
|
|
739
|
+
const action = objectModel.actions?.find((action) => action.name === actionName);
|
|
740
|
+
if (!action || action.type !== 'object') {
|
|
741
|
+
return undefined;
|
|
742
|
+
}
|
|
743
|
+
return {
|
|
744
|
+
// if configuration is updated, the new action properties will override the stored action properties
|
|
745
|
+
...action,
|
|
746
|
+
type: 'object',
|
|
747
|
+
extendedType: 'objectModel',
|
|
748
|
+
actionId,
|
|
749
|
+
label: getActionLabel(action),
|
|
750
|
+
documentWithSource,
|
|
751
|
+
modelWithSource,
|
|
752
|
+
documentField: csiDocumentField,
|
|
753
|
+
modelField,
|
|
754
|
+
objectModel: { ...objectModel, srcType, srcProjectId },
|
|
755
|
+
fieldPath,
|
|
756
|
+
userId: actionRunState?.userId,
|
|
757
|
+
isRunning: actionRunState?.isRunning
|
|
758
|
+
};
|
|
582
759
|
}
|
|
583
|
-
return {
|
|
584
|
-
// if configuration is updated, the new action properties will override the stored action properties
|
|
585
|
-
...action,
|
|
586
|
-
type: 'objectModel',
|
|
587
|
-
actionId,
|
|
588
|
-
label: action.label ?? lodash_1.default.startCase(action.name),
|
|
589
|
-
documentSpec,
|
|
590
|
-
fieldPath,
|
|
591
|
-
runningHandler: actionRunState?.runningHandler,
|
|
592
|
-
lastResultState: actionRunState?.lastResultState
|
|
593
|
-
};
|
|
594
760
|
}
|
|
595
761
|
}
|
|
596
762
|
}
|
|
763
|
+
function getExtendedActionFromGlobalAction({ action, actionId, actionRunState, generateNewActionId }) {
|
|
764
|
+
if (action.type === 'model') {
|
|
765
|
+
return {
|
|
766
|
+
...action,
|
|
767
|
+
extendedType: 'modelGlobal',
|
|
768
|
+
actionId: generateNewActionId && !actionRunState ? `${actionId}.${(0, uuid_1.v4)().substring(0, 8)}` : actionId,
|
|
769
|
+
label: getActionLabel(action),
|
|
770
|
+
modelSpec: actionRunState?.modelSpec,
|
|
771
|
+
userId: actionRunState?.userId,
|
|
772
|
+
isRunning: actionRunState?.isRunning
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
return {
|
|
776
|
+
...action,
|
|
777
|
+
extendedType: action.type,
|
|
778
|
+
actionId,
|
|
779
|
+
label: getActionLabel(action),
|
|
780
|
+
userId: actionRunState?.userId,
|
|
781
|
+
isRunning: actionRunState?.isRunning
|
|
782
|
+
};
|
|
783
|
+
}
|
|
597
784
|
function globalActionId(action) {
|
|
598
785
|
return `config.actions.${action.name}`;
|
|
599
786
|
}
|
|
@@ -601,7 +788,8 @@ function isGlobalActionId(actionId) {
|
|
|
601
788
|
return actionId.startsWith('config.actions.');
|
|
602
789
|
}
|
|
603
790
|
function getGlobalActionNameFromId(actionId) {
|
|
604
|
-
|
|
791
|
+
const result = actionId.match(/^config\.actions\.(.+?)(?:\.([\w-]+))?$/);
|
|
792
|
+
return result ? result[1] : undefined;
|
|
605
793
|
}
|
|
606
794
|
function parseActionId(actionId) {
|
|
607
795
|
try {
|
|
@@ -611,4 +799,7 @@ function parseActionId(actionId) {
|
|
|
611
799
|
return undefined;
|
|
612
800
|
}
|
|
613
801
|
}
|
|
802
|
+
function getActionLabel(action) {
|
|
803
|
+
return action.label ?? lodash_1.default.startCase(action.name);
|
|
804
|
+
}
|
|
614
805
|
//# sourceMappingURL=custom-actions.js.map
|