@stackbit/cms-contentstack 0.2.13-staging.1 → 0.2.13

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.
@@ -1,730 +0,0 @@
1
- "use strict";
2
- var __importDefault = (this && this.__importDefault) || function (mod) {
3
- return (mod && mod.__esModule) ? mod : { "default": mod };
4
- };
5
- Object.defineProperty(exports, "__esModule", { value: true });
6
- exports.convertOperationFieldValue = exports.getUpdatedEntryAtFieldPathWithOperation = exports.updateEntryFromUpdateOperations = exports.createEntryFromOperationFields = void 0;
7
- const uuid_1 = require("uuid");
8
- const lodash_1 = __importDefault(require("lodash"));
9
- function createEntryFromOperationFields({ updateOperationFields, model, getDocumentById, getModelByName, logger }) {
10
- return lodash_1.default.reduce(updateOperationFields, (accum, updateOperationField, fieldName) => {
11
- const modelField = getModelField(model, fieldName);
12
- if (!modelField) {
13
- return accum;
14
- }
15
- accum[fieldName] = convertOperationFieldValue({
16
- updateOperationField,
17
- modelField,
18
- rootModel: model,
19
- entryFieldPath: [fieldName],
20
- modelFieldPath: [fieldName],
21
- getDocumentById,
22
- getModelByName,
23
- errorPrefix: 'Error occurred while creating an entry.'
24
- });
25
- return accum;
26
- }, { title: (0, uuid_1.v4)() });
27
- }
28
- exports.createEntryFromOperationFields = createEntryFromOperationFields;
29
- function updateEntryFromUpdateOperations({ entry, updateOperations, getDocumentById, getModelByName, logger }) {
30
- var _a;
31
- const model = getModelByName(entry.content_type_uid);
32
- if (!model) {
33
- throw new Error(`Error occurred while updating entry ${entry.uid}, model ${entry.content_type_uid} not found.`);
34
- }
35
- // remove all model fields
36
- for (const field of (_a = model.fields) !== null && _a !== void 0 ? _a : []) {
37
- const { [field.name]: entryField, ...rest } = entry;
38
- entry = rest;
39
- }
40
- return lodash_1.default.reduce(updateOperations, (entry, updateOperation) => {
41
- return getUpdatedEntryAtFieldPathWithOperation({
42
- entry,
43
- updateOperation,
44
- model,
45
- getDocumentById,
46
- getModelByName,
47
- logger
48
- });
49
- }, entry);
50
- // const updateStatements = updateOperations.map((updateOp) => {
51
- // switch (updateOp.opType) {
52
- // case 'set': {
53
- // const fieldValue = convertOperationFieldValue({
54
- // updateOperationField: updateOp.field,
55
- // rootModel: model,
56
- // modelField: updateOp.modelField,
57
- // modelFieldPath: transformToModelFieldPath(updateOp.fieldPath),
58
- // entryFieldPath: transformToEntryFieldPath(updateOp.fieldPath),
59
- // getDocumentById,
60
- // getModelByName
61
- // });
62
- // return updateFieldAtFieldPath({
63
- // entry,
64
- // updateEntry,
65
- // fieldPath: updateOp.fieldPath,
66
- // fieldValue,
67
- // model,
68
- // getModelByName
69
- // });
70
- // }
71
- // case 'unset': {
72
- // return updateFieldAtFieldPath({
73
- // entry,
74
- // fieldPath: updateOp.fieldPath,
75
- // value: null,
76
- // model,
77
- // getModelByName
78
- // });
79
- // }
80
- // case 'insert': {
81
- // const { item, index } = updateOp;
82
- //
83
- // const container = getDataByPath(entry, updateOp.fieldPath, model, getModelByName) || [];
84
- // const convertedData = convertObject(container);
85
- //
86
- // const fieldValue = convertOperationFieldValue(item, { name: '', ...updateOp.modelField }, getDocumentById, model, modelMap);
87
- //
88
- // convertedData.splice(index, 0, fieldValue);
89
- //
90
- // return updateFieldAtFieldPath(entry, updateOp.fieldPath, convertedData, model, getModelByName);
91
- // }
92
- // case 'remove': {
93
- // const { index } = updateOp;
94
- //
95
- // const container = getDataByPath(entry, updateOp.fieldPath, model, getModelByName);
96
- // const convertedData = convertObject(container);
97
- //
98
- // convertedData.splice(index, 1);
99
- // return updateFieldAtFieldPath(entry, updateOp.fieldPath, convertedData, model, getModelByName);
100
- // }
101
- // case 'reorder': {
102
- // const { order } = updateOp;
103
- //
104
- // const container = getDataByPath(entry, updateOp.fieldPath, model, getModelByName);
105
- // const convertedData = convertObject(container);
106
- //
107
- // const reorderedItems = order.map((itemIndex) => convertedData.at(itemIndex));
108
- // return updateFieldAtFieldPath(entry, updateOp.fieldPath, reorderedItems, model, getModelByName);
109
- // }
110
- //
111
- // default: {
112
- // const _exhaustiveCheck: never = updateOp;
113
- // return _exhaustiveCheck;
114
- // }
115
- // }
116
- // });
117
- //
118
- // return updateStatements.filter(Boolean);
119
- }
120
- exports.updateEntryFromUpdateOperations = updateEntryFromUpdateOperations;
121
- // export function getDataByPath(obj: any, fieldPath: (number | string)[], model: Model, modelMap: ModelMap): any {
122
- // if (fieldPath.length === 0) {
123
- // return obj;
124
- // }
125
- //
126
- // const currentFieldName = fieldPath[0] as string;
127
- // const modelField = (model.fields || []).find((field) => field.name === currentFieldName);
128
- //
129
- // if (!modelField) {
130
- // return getDataByPath(obj[currentFieldName], fieldPath.slice(1), model, modelMap);
131
- // }
132
- //
133
- // if (fieldPath.length >= 2 && modelField.type === 'list' && modelField.items.type === 'model') {
134
- // const isFieldBlockField = isBlocksField(modelField, model.name);
135
- // const currentObj = handleGetDataGroup(obj, modelField);
136
- //
137
- // if (!isFieldBlockField) {
138
- // return getDataByPath(currentObj, fieldPath.slice(1), modelMap[modelField.items.models[0] as string] as Model, modelMap);
139
- // }
140
- //
141
- // const index = fieldPath[1] as number;
142
- // const item = currentObj[index];
143
- //
144
- // const blockType = Object.keys(item)[0] as string;
145
- // const blockModelName = generateBlockModelName(model.name, blockType);
146
- //
147
- // return getDataByPath(item[blockType], fieldPath.slice(2), modelMap[blockModelName] as Model, modelMap);
148
- // }
149
- //
150
- // return getDataByPath(handleGetDataGroup(obj, modelField), fieldPath.slice(1), model, modelMap);
151
- // }
152
- //
153
- // export function isBlocksField(field: Field, modelName: string) {
154
- // return field.type === 'list' && field.items.type === 'model' && field.items.models?.some((fieldModel) => fieldModel.includes(`BLOCK_${modelName}__`));
155
- // }
156
- //
157
- // export function generateBlockModelName(modelName: string, blockName: string) {
158
- // return `BLOCK_${modelName}__${blockName}`;
159
- // }
160
- //
161
- // export function convertObject(obj: any): any {
162
- // if (obj === null) {
163
- // return obj;
164
- // }
165
- //
166
- // if (typeof obj !== 'object') {
167
- // return obj;
168
- // }
169
- //
170
- // if (Array.isArray(obj)) {
171
- // return obj.map(convertObject);
172
- // }
173
- //
174
- // if (obj.hasOwnProperty('uid') && !obj.hasOwnProperty('_content_type_uid')) {
175
- // return obj.uid;
176
- // }
177
- //
178
- // return Object.entries(obj).reduce((acc: Record<string, any>, [key, value]) => {
179
- // if (value === null) {
180
- // return acc;
181
- // }
182
- //
183
- // acc[key] = key === '_metadata' ? value : convertObject(value);
184
- // return acc;
185
- // }, {});
186
- // }
187
- function getUpdatedEntryAtFieldPathWithOperation({ entry, updateOperation, model, getDocumentById, getModelByName, logger }) {
188
- let opMessage;
189
- switch (updateOperation.opType) {
190
- case 'set':
191
- opMessage = 'setting a field';
192
- break;
193
- case 'unset':
194
- opMessage = 'unsetting a field';
195
- break;
196
- case 'insert':
197
- opMessage = 'inserting an item into a list';
198
- break;
199
- case 'remove':
200
- opMessage = 'removing an item from a list';
201
- break;
202
- case 'reorder':
203
- opMessage = 'reordering a list';
204
- break;
205
- }
206
- const errorPrefix = `Error occurred while ${opMessage} in entry '${entry.uid}'.`;
207
- function createError(detailedMessage) {
208
- return new Error(`${errorPrefix} ${detailedMessage}`);
209
- }
210
- function iterateModel({ value, model, fieldPath, entryFieldPath, modelFieldPath }) {
211
- if (fieldPath.length === 0) {
212
- return getValueForOperation({
213
- updateOperation,
214
- value,
215
- rootEntry: entry,
216
- rootModel: model,
217
- entryFieldPath,
218
- modelFieldPath,
219
- getDocumentById,
220
- getModelByName,
221
- errorPrefix,
222
- logger
223
- });
224
- }
225
- const fieldName = lodash_1.default.head(fieldPath);
226
- const fieldPathTail = lodash_1.default.tail(fieldPath);
227
- if (typeof fieldName !== 'string') {
228
- throw createError(`The field '${fieldName}' in field path '${entryFieldPath.concat(fieldName).join('.')}' ` +
229
- `is not a string, 'typeof ${fieldName} === ${typeof fieldName}'.`);
230
- }
231
- const modelField = getModelField(model, fieldName);
232
- if (!modelField) {
233
- throw createError(`The field '${fieldName}' in field path '${entryFieldPath.concat(fieldName).join('.')}' does not exist in model '${model.name}'.`);
234
- }
235
- return {
236
- ...value,
237
- [fieldName]: iterateField({
238
- value: value ? value[fieldName] : undefined,
239
- modelField,
240
- fieldPath: fieldPathTail,
241
- rootModel: model,
242
- entryFieldPath: entryFieldPath.concat(fieldName),
243
- modelFieldPath: modelFieldPath.concat(fieldName)
244
- })
245
- };
246
- }
247
- function iterateField({ value, modelField, fieldPath, rootModel, entryFieldPath, modelFieldPath }) {
248
- var _a, _b;
249
- if (fieldPath.length === 0) {
250
- return getValueForOperation({
251
- updateOperation,
252
- value,
253
- rootEntry: entry,
254
- rootModel,
255
- entryFieldPath,
256
- modelFieldPath,
257
- getDocumentById,
258
- getModelByName,
259
- errorPrefix,
260
- logger
261
- });
262
- }
263
- const fieldName = lodash_1.default.head(fieldPath);
264
- const fieldPathTail = lodash_1.default.tail(fieldPath);
265
- // The value must be an object to apply the operation on its nested field with name fieldName,
266
- // otherwise the fieldPath is illegal
267
- if (!isObject(value)) {
268
- throw createError(`The field path '${entryFieldPath.join('.')}' points to an empty value.`);
269
- }
270
- if (typeof fieldName !== 'string') {
271
- throw createError(`The field '${fieldName}' in field path '${entryFieldPath.concat(fieldName).join('.')}' ` +
272
- `is not a string, 'typeof ${fieldName} === ${typeof fieldName}'.`);
273
- }
274
- if (modelField.type === 'object') {
275
- const childModelField = getModelField(modelField, fieldName);
276
- if (!childModelField) {
277
- throw createError(`The field '${fieldName}' in field path '${entryFieldPath.concat(fieldName).join('.')}' does not exist in model '${model.name}'.`);
278
- }
279
- return {
280
- ...value,
281
- [fieldName]: iterateField({
282
- value: value ? value[fieldName] : undefined,
283
- modelField: childModelField,
284
- fieldPath: fieldPathTail,
285
- rootModel,
286
- entryFieldPath: entryFieldPath.concat(fieldName),
287
- modelFieldPath: modelFieldPath.concat(fieldName)
288
- })
289
- };
290
- }
291
- else if (modelField.type === 'model') {
292
- const fieldValue = value[fieldName];
293
- const fieldPathStr = modelFieldPath.join('.');
294
- const blockMap = (_b = (_a = rootModel.context) === null || _a === void 0 ? void 0 : _a.blockMap) === null || _b === void 0 ? void 0 : _b[fieldPathStr];
295
- if (blockMap) {
296
- // This is a modular blocks field with keys mapping to inline (object)
297
- // or global (model) fields mapped by model.context.blockMap[blockUID].
298
- // The fieldValue must be an object with a single property matching the block uid
299
- if (fieldPathTail.length === 0) {
300
- return {
301
- ...value,
302
- [fieldName]: getValueForOperation({
303
- updateOperation,
304
- value: value[fieldName],
305
- rootEntry: entry,
306
- rootModel,
307
- entryFieldPath: entryFieldPath.concat(fieldName),
308
- modelFieldPath: modelFieldPath.concat(fieldName),
309
- getDocumentById,
310
- getModelByName,
311
- errorPrefix,
312
- logger
313
- })
314
- };
315
- }
316
- if (!isObject(fieldValue)) {
317
- throw createError(`The field path '${entryFieldPath.concat(fieldName).join('.')}' points to an empty value.`);
318
- }
319
- const blockUIDs = lodash_1.default.keys(fieldValue);
320
- const blockUID = blockUIDs[0];
321
- if (blockUIDs.length !== 1 || !blockUID) {
322
- throw createError(`The modular block at field path '${entryFieldPath.concat(fieldName).join('.')}' has no UID.`);
323
- }
324
- const nestedBlock = fieldValue[blockUID];
325
- if (!isObject(nestedBlock)) {
326
- throw createError(`The field path '${entryFieldPath.concat(fieldName).join('.')}' points to an empty modular block '${blockUID}'.`);
327
- }
328
- const modelName = blockMap.blockIdToModelName[blockUID];
329
- if (!modelName) {
330
- throw createError(`Could not match model name for modular block '${blockUID}' at field path '${entryFieldPath.concat(fieldName).join('.')}'.`);
331
- }
332
- const nestedModel = getModelByName(modelName);
333
- if (!nestedModel) {
334
- throw createError(`Could not find model named '${modelName}' for modular block '${blockUID}' ` +
335
- `at field path '${entryFieldPath.concat(fieldName).join('.')}'.`);
336
- }
337
- return {
338
- ...value,
339
- [fieldName]: {
340
- [blockUID]: iterateModel({
341
- value: nestedBlock,
342
- fieldPath: fieldPathTail,
343
- model: nestedModel,
344
- entryFieldPath: entryFieldPath.concat(fieldName),
345
- modelFieldPath: modelFieldPath.concat(fieldName)
346
- })
347
- }
348
- };
349
- }
350
- else {
351
- // This is as a regular global field (model) with a single model or as a __link_model
352
- if (modelField.models.length !== 1) {
353
- throw createError(`A model field at field path '${entryFieldPath.join('.')}' has more than one (or zero) models ${modelField.models.join('.')}.`);
354
- }
355
- const modelName = modelField.models[0];
356
- const nestedModel = getModelByName(modelName);
357
- if (!nestedModel) {
358
- throw createError(`Could not find model named '${modelName}' for object at field path '${entryFieldPath.concat(fieldName).join('.')}.'`);
359
- }
360
- return {
361
- ...value,
362
- [fieldName]: iterateModel({
363
- value: isObject(fieldValue) ? fieldValue : undefined,
364
- fieldPath: fieldPathTail,
365
- model: nestedModel,
366
- entryFieldPath: entryFieldPath.concat(fieldName),
367
- modelFieldPath: modelFieldPath.concat(fieldName)
368
- })
369
- };
370
- }
371
- }
372
- else if (modelField.type === 'list') {
373
- const fieldValue = value[fieldName];
374
- if (fieldPathTail.length === 0) {
375
- return {
376
- ...value,
377
- [fieldName]: getValueForOperation({
378
- updateOperation,
379
- value: fieldValue,
380
- rootEntry: entry,
381
- rootModel,
382
- entryFieldPath: entryFieldPath.concat(fieldName),
383
- modelFieldPath: modelFieldPath.concat(fieldName),
384
- getDocumentById,
385
- getModelByName,
386
- errorPrefix,
387
- logger
388
- })
389
- };
390
- }
391
- if (!Array.isArray(fieldValue)) {
392
- throw createError(`The value at field path '${entryFieldPath.concat(fieldName).join('.')}' is not an array.`);
393
- }
394
- const nestedModel = modelField.items;
395
- const itemIndex = lodash_1.default.toNumber(lodash_1.default.head(fieldPathTail));
396
- return {
397
- ...value,
398
- [fieldName]: fieldValue.slice().splice(itemIndex, 1, iterateField({
399
- value: fieldValue[itemIndex],
400
- fieldPath: lodash_1.default.tail(fieldPathTail),
401
- modelField: nestedModel,
402
- rootModel,
403
- entryFieldPath: entryFieldPath.concat([fieldName, itemIndex]),
404
- modelFieldPath: modelFieldPath.concat(fieldName)
405
- }))
406
- };
407
- }
408
- throw createError(`The field type '${modelField.type}' at field path '${entryFieldPath.join('.')}' is not a container type (object, model, list).`);
409
- }
410
- return iterateModel({
411
- value: entry,
412
- model,
413
- fieldPath: updateOperation.fieldPath,
414
- entryFieldPath: [],
415
- modelFieldPath: []
416
- });
417
- }
418
- exports.getUpdatedEntryAtFieldPathWithOperation = getUpdatedEntryAtFieldPathWithOperation;
419
- function getValueForOperation({ updateOperation, value, rootModel, entryFieldPath, modelFieldPath, getDocumentById, getModelByName, errorPrefix, logger }) {
420
- switch (updateOperation.opType) {
421
- case 'set':
422
- return convertOperationFieldValue({
423
- updateOperationField: updateOperation.field,
424
- modelField: updateOperation.modelField,
425
- rootModel: rootModel,
426
- entryFieldPath: entryFieldPath,
427
- modelFieldPath: modelFieldPath,
428
- getModelByName,
429
- getDocumentById,
430
- errorPrefix
431
- });
432
- case 'unset': {
433
- // TODO: is unset different per type?
434
- return null;
435
- }
436
- case 'insert': {
437
- if (!Array.isArray(value)) {
438
- // TODO: error
439
- return {};
440
- }
441
- const newValue = convertOperationFieldValue({
442
- updateOperationField: updateOperation.item,
443
- modelField: updateOperation.modelField,
444
- rootModel: rootModel,
445
- entryFieldPath: entryFieldPath,
446
- modelFieldPath: modelFieldPath,
447
- getModelByName,
448
- getDocumentById,
449
- errorPrefix
450
- });
451
- if (typeof updateOperation.index === 'undefined') {
452
- return value.concat(newValue);
453
- }
454
- else {
455
- return value.slice().splice(updateOperation.index, 0, newValue);
456
- }
457
- }
458
- case 'remove': {
459
- if (!Array.isArray(value)) {
460
- // TODO: error
461
- return {};
462
- }
463
- return value.slice().splice(updateOperation.index, 1);
464
- }
465
- case 'reorder': {
466
- if (!Array.isArray(value)) {
467
- // TODO: error
468
- return {};
469
- }
470
- return updateOperation.order.map((newIndex) => value[newIndex]);
471
- }
472
- default: {
473
- const _exhaustiveCheck = updateOperation;
474
- return _exhaustiveCheck;
475
- }
476
- }
477
- }
478
- // export function updateField({
479
- // entry,
480
- // updateOperations,
481
- // fieldPath,
482
- // fieldValue,
483
- // model,
484
- // getModelByName
485
- // }: {
486
- // entry: Entry;
487
- // updateOperations: Record<string, any>;
488
- // fieldPath: (number | string)[];
489
- // fieldValue: any;
490
- // model: ModelWithContext;
491
- // getModelByName: GetModelByName;
492
- // }): Record<string, any> {
493
- // if (fieldPath.length == 0) {
494
- // return fieldValue;
495
- // }
496
- //
497
- // const currentFieldName = fieldPath[0] as string;
498
- // const modelField = getModelField(model, currentFieldName) as Field;
499
- //
500
- // if (fieldPath.length == 1) {
501
- // return handleGroup(currentFieldName, modelField, fieldValue);
502
- // }
503
- //
504
- // if (modelField.type === 'list') {
505
- // const index = fieldPath[1] as number;
506
- //
507
- // if (modelField.items.type === 'model') {
508
- // const isFieldBlockField = isBlocksField(modelField, model.name);
509
- // const currentObject = entry[currentFieldName][index];
510
- //
511
- // const itemModelName = isFieldBlockField
512
- // ? generateBlockModelName(model.name, Object.keys(currentObject)[0] as string)
513
- // : (modelField.items.models[0] as string);
514
- // const cleanBlockModelName = itemModelName.replace(`BLOCK_${model.name}__`, '');
515
- //
516
- // const data = {
517
- // UPDATE: {
518
- // index,
519
- // data: isFieldBlockField
520
- // ? handleGroup(
521
- // itemModelName.replace(`BLOCK_${model.name}__`, ''),
522
- // modelField,
523
- // updateFieldAtFieldPath(
524
- // currentObject[cleanBlockModelName],
525
- // fieldPath.slice(2),
526
- // fieldValue,
527
- // getModelByName(itemModelName),
528
- // getModelByName
529
- // )
530
- // )
531
- // : updateFieldAtFieldPath(currentObject, fieldPath.slice(2), fieldValue, getModelByName(itemModelName), getModelByName)
532
- // }
533
- // };
534
- //
535
- // return handleGroup(currentFieldName, modelField, data);
536
- // }
537
- //
538
- // return {
539
- // [currentFieldName]: {
540
- // UPDATE: {
541
- // index,
542
- // data: updateFieldAtFieldPath(entry[currentFieldName][index], fieldPath.slice(2), fieldValue, model, getModelByName)
543
- // }
544
- // }
545
- // };
546
- // }
547
- //
548
- // return handleGroup(currentFieldName, modelField, updateFieldAtFieldPath(entry[currentFieldName], fieldPath.slice(1), fieldValue, model, getModelByName));
549
- // }
550
- function convertOperationFieldValue({ updateOperationField, rootModel, modelField, entryFieldPath, modelFieldPath, getDocumentById, getModelByName, errorPrefix }) {
551
- var _a, _b;
552
- function createError(detailedMessage) {
553
- return new Error(`${errorPrefix} ${detailedMessage}`);
554
- }
555
- switch (updateOperationField.type) {
556
- case 'string':
557
- case 'url':
558
- case 'slug':
559
- case 'text':
560
- case 'markdown':
561
- case 'html':
562
- case 'date':
563
- case 'datetime':
564
- case 'color':
565
- case 'enum': {
566
- return updateOperationField.value;
567
- }
568
- case 'number': {
569
- return updateOperationField.value || 0;
570
- }
571
- case 'boolean': {
572
- return updateOperationField.value || false;
573
- }
574
- case 'file':
575
- case 'image': {
576
- return updateOperationField.value || null;
577
- }
578
- case 'richText':
579
- case 'json':
580
- case 'style':
581
- case 'cross-reference': {
582
- return updateOperationField.value;
583
- }
584
- case 'reference': {
585
- if ((updateOperationField === null || updateOperationField === void 0 ? void 0 : updateOperationField.refType) === 'asset') {
586
- return updateOperationField.refId;
587
- }
588
- const document = getDocumentById(updateOperationField === null || updateOperationField === void 0 ? void 0 : updateOperationField.refId);
589
- if (!document) {
590
- throw createError(`Could not find entry with ID '${updateOperationField === null || updateOperationField === void 0 ? void 0 : updateOperationField.refId}' referenced at field path ${entryFieldPath.join('.')}.`);
591
- }
592
- return {
593
- uid: updateOperationField.refId,
594
- _content_type_uid: document.modelName
595
- };
596
- }
597
- case 'object': {
598
- if (modelField.type !== 'object') {
599
- throw createError(`The operation field type 'object' does not match model field type '${modelField.type}' at field path ${entryFieldPath.join('.')}.`);
600
- }
601
- return lodash_1.default.reduce(updateOperationField.fields, (accum, nestedUpdateOperationField, fieldName) => {
602
- const nestedModelField = getModelField(modelField, fieldName);
603
- if (!nestedModelField) {
604
- return accum;
605
- }
606
- accum[fieldName] = convertOperationFieldValue({
607
- updateOperationField: nestedUpdateOperationField,
608
- rootModel,
609
- modelField: nestedModelField,
610
- entryFieldPath: entryFieldPath.concat(fieldName),
611
- modelFieldPath: modelFieldPath.concat(fieldName),
612
- getDocumentById,
613
- getModelByName,
614
- errorPrefix
615
- });
616
- return accum;
617
- }, {});
618
- }
619
- case 'model': {
620
- if (modelField.type !== 'model') {
621
- throw createError(`The operation field type 'model' does not match model field type '${modelField.type}' at field path ${entryFieldPath.join('.')}.`);
622
- }
623
- const nestedModel = getModelByName(updateOperationField.modelName);
624
- if (!nestedModel) {
625
- throw createError(`Could not find model named '${updateOperationField.modelName}' for field at path '${entryFieldPath.join('.')}'.`);
626
- }
627
- const fieldPathStr = modelFieldPath.join('.');
628
- const blockMap = (_b = (_a = rootModel.context) === null || _a === void 0 ? void 0 : _a.blockMap) === null || _b === void 0 ? void 0 : _b[fieldPathStr];
629
- if (blockMap) {
630
- // this is a modular blocks field with keys mapping to inline (object) or global (model) fields
631
- // mapped by context.blockMap[blockUID]
632
- const blockUID = blockMap.modelNameToBlockId[updateOperationField.modelName];
633
- if (!blockUID) {
634
- throw createError(`Could not match modular block UI for model '${updateOperationField.modelName}' for field path '${entryFieldPath.join('.')}'.`);
635
- }
636
- return {
637
- [blockUID]: lodash_1.default.reduce(updateOperationField.fields, (accum, nestedUpdateOperationField, fieldName) => {
638
- const nestedModelField = getModelField(nestedModel, fieldName);
639
- if (!nestedModelField) {
640
- return accum;
641
- }
642
- accum[fieldName] = convertOperationFieldValue({
643
- updateOperationField: nestedUpdateOperationField,
644
- rootModel: nestedModel,
645
- modelField: nestedModelField,
646
- entryFieldPath: entryFieldPath.concat([blockUID, fieldName]),
647
- modelFieldPath: [fieldName],
648
- getDocumentById,
649
- getModelByName,
650
- errorPrefix
651
- });
652
- return accum;
653
- }, {})
654
- };
655
- }
656
- else {
657
- // this is a regular global field (model) with a single model or as a __link_model
658
- return lodash_1.default.reduce(updateOperationField.fields, (accum, nestedUpdateOperationField, fieldName) => {
659
- const nestedModelField = getModelField(nestedModel, fieldName);
660
- if (!nestedModelField) {
661
- return accum;
662
- }
663
- accum[fieldName] = convertOperationFieldValue({
664
- updateOperationField: nestedUpdateOperationField,
665
- rootModel: nestedModel,
666
- modelField: nestedModelField,
667
- entryFieldPath: entryFieldPath.concat(fieldName),
668
- modelFieldPath: [fieldName],
669
- getDocumentById,
670
- getModelByName,
671
- errorPrefix
672
- });
673
- return accum;
674
- }, {});
675
- }
676
- }
677
- case 'list': {
678
- if (modelField.type !== 'list') {
679
- throw createError(`The operation field type 'list' does not match model field type '${modelField.type}' at field path ${entryFieldPath.join('.')}.`);
680
- }
681
- return lodash_1.default.reduce(updateOperationField.items, (accum, listItem, index) => {
682
- return convertOperationFieldValue({
683
- updateOperationField: listItem,
684
- rootModel,
685
- modelField: modelField.items,
686
- entryFieldPath: entryFieldPath.concat(index),
687
- modelFieldPath: modelFieldPath,
688
- getDocumentById,
689
- getModelByName,
690
- errorPrefix
691
- });
692
- }, []);
693
- }
694
- default: {
695
- const _exhaustiveCheck = updateOperationField;
696
- return _exhaustiveCheck;
697
- }
698
- }
699
- }
700
- exports.convertOperationFieldValue = convertOperationFieldValue;
701
- function getModelField(model, fieldName) {
702
- var _a;
703
- return (_a = model.fields) === null || _a === void 0 ? void 0 : _a.find((field) => field.name === fieldName);
704
- }
705
- function isObject(value) {
706
- return lodash_1.default.isPlainObject(value);
707
- }
708
- // function handleGroup(fieldName: string, modelField: Field, value: any) {
709
- // if (modelField?.group) {
710
- // return {
711
- // [modelField.group]: {
712
- // [fieldName]: value
713
- // }
714
- // };
715
- // }
716
- //
717
- // return {
718
- // [fieldName]: value
719
- // };
720
- // }
721
- //
722
- // function handleGetDataGroup(obj: Record<string, any>, modelField: Field) {
723
- // const fieldName = modelField.name;
724
- // if (modelField.group) {
725
- // return obj[modelField.group][fieldName];
726
- // }
727
- //
728
- // return obj[fieldName];
729
- // }
730
- //# sourceMappingURL=transformation-utils.js.map