@stackbit/cms-core 0.0.18-alpha.0 → 0.0.19-alpha.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/dist/content-source-interface.d.ts +27 -19
- package/dist/content-source-interface.d.ts.map +1 -1
- package/dist/content-source-interface.js +1 -1
- package/dist/content-source-interface.js.map +1 -1
- package/dist/content-store.d.ts +4 -4
- package/dist/content-store.d.ts.map +1 -1
- package/dist/content-store.js +189 -142
- package/dist/content-store.js.map +1 -1
- package/package.json +2 -2
- package/src/content-source-interface.ts +38 -18
- package/src/content-store.ts +253 -180
package/src/content-store.ts
CHANGED
|
@@ -16,8 +16,11 @@ import {
|
|
|
16
16
|
Model
|
|
17
17
|
} from '@stackbit/sdk';
|
|
18
18
|
import { mapPromise, omitByNil } from '@stackbit/utils';
|
|
19
|
-
import * as
|
|
20
|
-
import {
|
|
19
|
+
import * as CSITypes from './content-source-interface';
|
|
20
|
+
import {
|
|
21
|
+
DocumentObjectField,
|
|
22
|
+
isLocalizedField
|
|
23
|
+
} from './content-source-interface';
|
|
21
24
|
import * as ContentStoreTypes from './content-store-types';
|
|
22
25
|
import { IMAGE_MODEL } from './common/common-schema';
|
|
23
26
|
import { Timer } from './utils/timer';
|
|
@@ -27,7 +30,7 @@ export interface ContentSourceOptions {
|
|
|
27
30
|
userLogger: ContentStoreTypes.Logger;
|
|
28
31
|
localDev: boolean;
|
|
29
32
|
stackbitYamlDir: string;
|
|
30
|
-
contentSources:
|
|
33
|
+
contentSources: CSITypes.ContentSourceInterface[];
|
|
31
34
|
onSchemaChangeCallback: () => void;
|
|
32
35
|
onContentChangeCallback: (contentChanges: ContentStoreTypes.ContentChangeResult) => void;
|
|
33
36
|
handleConfigAssets: (config: Config) => Promise<Config>;
|
|
@@ -35,21 +38,25 @@ export interface ContentSourceOptions {
|
|
|
35
38
|
|
|
36
39
|
interface ContentSourceData {
|
|
37
40
|
id: string;
|
|
38
|
-
instance:
|
|
41
|
+
instance: CSITypes.ContentSourceInterface;
|
|
39
42
|
type: string;
|
|
40
43
|
projectId: string;
|
|
41
44
|
models: Model[];
|
|
42
45
|
modelMap: Record<string, Model>;
|
|
43
|
-
locales?:
|
|
46
|
+
locales?: CSITypes.Locale[];
|
|
44
47
|
defaultLocaleCode?: string;
|
|
48
|
+
csiDocuments: CSITypes.Document[];
|
|
49
|
+
csiDocumentMap: Record<string, CSITypes.Document>;
|
|
45
50
|
documents: ContentStoreTypes.Document[];
|
|
46
51
|
documentMap: Record<string, ContentStoreTypes.Document>;
|
|
52
|
+
csiAssets: CSITypes.Asset[];
|
|
53
|
+
csiAssetMap: Record<string, CSITypes.Asset>;
|
|
47
54
|
assets: ContentStoreTypes.Asset[];
|
|
48
55
|
assetMap: Record<string, ContentStoreTypes.Asset>;
|
|
49
56
|
}
|
|
50
57
|
|
|
51
58
|
export class ContentStore {
|
|
52
|
-
private readonly contentSources:
|
|
59
|
+
private readonly contentSources: CSITypes.ContentSourceInterface[];
|
|
53
60
|
private readonly logger: ContentStoreTypes.Logger;
|
|
54
61
|
private readonly userLogger: ContentStoreTypes.Logger;
|
|
55
62
|
private readonly localDev: boolean;
|
|
@@ -205,7 +212,7 @@ export class ContentStore {
|
|
|
205
212
|
return config;
|
|
206
213
|
}
|
|
207
214
|
|
|
208
|
-
async loadContentSourceData({ contentSourceInstance, init }: { contentSourceInstance:
|
|
215
|
+
async loadContentSourceData({ contentSourceInstance, init }: { contentSourceInstance: CSITypes.ContentSourceInterface; init: boolean }) {
|
|
209
216
|
// TODO: defer loading content if content is already loading for a specific content source
|
|
210
217
|
|
|
211
218
|
// TODO: optimize: cache raw responses from contentSource
|
|
@@ -242,19 +249,24 @@ export class ContentStore {
|
|
|
242
249
|
// that maps presetIds by model name instead of storing that map inside every model
|
|
243
250
|
this.presets = config.presets;
|
|
244
251
|
|
|
245
|
-
const
|
|
246
|
-
const
|
|
247
|
-
const
|
|
248
|
-
|
|
252
|
+
const csiDocuments = await contentSourceInstance.getDocuments({ modelMap });
|
|
253
|
+
const csiAssets = await contentSourceInstance.getAssets();
|
|
254
|
+
const csiDocumentMap = _.keyBy(csiDocuments, 'id');
|
|
255
|
+
const csiAssetMap = _.keyBy(csiAssets, 'id');
|
|
256
|
+
|
|
257
|
+
const contentStoreDocuments = mapCSIDocumentsToStoreDocuments({
|
|
258
|
+
csiDocuments,
|
|
249
259
|
contentSourceInstance,
|
|
250
260
|
modelMap,
|
|
251
261
|
defaultLocaleCode
|
|
252
262
|
});
|
|
253
|
-
const contentStoreAssets =
|
|
254
|
-
|
|
263
|
+
const contentStoreAssets = mapCSIAssetsToStoreAssets({
|
|
264
|
+
csiAssets,
|
|
255
265
|
contentSourceInstance,
|
|
256
266
|
defaultLocaleCode
|
|
257
267
|
});
|
|
268
|
+
const documentMap = _.keyBy(contentStoreDocuments, 'srcObjectId');
|
|
269
|
+
const assetMap = _.keyBy(contentStoreAssets, 'srcObjectId');
|
|
258
270
|
|
|
259
271
|
this.logger.debug('loaded content source data', {
|
|
260
272
|
contentSourceId,
|
|
@@ -274,10 +286,14 @@ export class ContentStore {
|
|
|
274
286
|
defaultLocaleCode: defaultLocaleCode,
|
|
275
287
|
models: models,
|
|
276
288
|
modelMap: modelMap,
|
|
289
|
+
csiDocuments: csiDocuments,
|
|
290
|
+
csiDocumentMap: csiDocumentMap,
|
|
277
291
|
documents: contentStoreDocuments,
|
|
278
|
-
documentMap:
|
|
292
|
+
documentMap: documentMap,
|
|
293
|
+
csiAssets: csiAssets,
|
|
294
|
+
csiAssetMap: csiAssetMap,
|
|
279
295
|
assets: contentStoreAssets,
|
|
280
|
-
assetMap:
|
|
296
|
+
assetMap: assetMap
|
|
281
297
|
};
|
|
282
298
|
|
|
283
299
|
contentSourceInstance.startWatchingContentUpdates({
|
|
@@ -285,7 +301,13 @@ export class ContentStore {
|
|
|
285
301
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
286
302
|
return contentSourceData.modelMap;
|
|
287
303
|
},
|
|
288
|
-
|
|
304
|
+
getDocument({ documentId }: { documentId: string }) {
|
|
305
|
+
return csiDocumentMap[documentId];
|
|
306
|
+
},
|
|
307
|
+
getAsset({ assetId }: { assetId: string }) {
|
|
308
|
+
return csiAssetMap[assetId];
|
|
309
|
+
},
|
|
310
|
+
onContentChange: (contentChangeEvent: CSITypes.ContentChangeEvent) => {
|
|
289
311
|
this.logger.debug('content source called onContentChange', { contentSourceId });
|
|
290
312
|
const result = this.onContentChange(contentSourceId, contentChangeEvent);
|
|
291
313
|
this.onContentChangeCallback(result);
|
|
@@ -301,7 +323,7 @@ export class ContentStore {
|
|
|
301
323
|
});
|
|
302
324
|
}
|
|
303
325
|
|
|
304
|
-
onContentChange(contentSourceId: string, contentChangeEvent:
|
|
326
|
+
onContentChange(contentSourceId: string, contentChangeEvent: CSITypes.ContentChangeEvent): ContentStoreTypes.ContentChangeResult {
|
|
305
327
|
// TODO: prevent content change process for contentSourceId if loading content is in progress
|
|
306
328
|
|
|
307
329
|
this.logger.debug('onContentChange', {
|
|
@@ -320,50 +342,80 @@ export class ContentStore {
|
|
|
320
342
|
};
|
|
321
343
|
|
|
322
344
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
345
|
+
|
|
346
|
+
// update contentSourceData with deleted documents
|
|
323
347
|
contentChangeEvent.deletedDocumentIds.forEach((docId) => {
|
|
348
|
+
// delete document from documents map
|
|
324
349
|
delete contentSourceData.documentMap[docId];
|
|
350
|
+
delete contentSourceData.csiDocumentMap[docId];
|
|
351
|
+
|
|
352
|
+
// delete document from document array
|
|
325
353
|
const index = contentSourceData.documents.findIndex((document) => document.srcObjectId === docId);
|
|
326
354
|
if (index !== -1) {
|
|
355
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
327
356
|
contentSourceData.documents.splice(index, 1);
|
|
357
|
+
contentSourceData.csiDocuments.splice(index, 1);
|
|
328
358
|
}
|
|
359
|
+
|
|
329
360
|
result.deletedDocuments.push({
|
|
330
361
|
srcType: contentSourceData.type,
|
|
331
362
|
srcProjectId: contentSourceData.projectId,
|
|
332
363
|
srcObjectId: docId
|
|
333
364
|
});
|
|
334
365
|
});
|
|
366
|
+
|
|
367
|
+
// update contentSourceData with deleted assets
|
|
335
368
|
contentChangeEvent.deletedAssetIds.forEach((assetId) => {
|
|
369
|
+
// delete document from asset map
|
|
336
370
|
delete contentSourceData.assetMap[assetId];
|
|
371
|
+
delete contentSourceData.csiAssetMap[assetId];
|
|
372
|
+
|
|
373
|
+
// delete document from asset array
|
|
337
374
|
const index = contentSourceData.assets.findIndex((asset) => asset.srcObjectId === assetId);
|
|
338
375
|
if (index !== -1) {
|
|
376
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
339
377
|
contentSourceData.assets.splice(index, 1);
|
|
378
|
+
contentSourceData.csiAssets.splice(index, 1);
|
|
340
379
|
}
|
|
380
|
+
|
|
341
381
|
result.deletedAssets.push({
|
|
342
382
|
srcType: contentSourceData.type,
|
|
343
383
|
srcProjectId: contentSourceData.projectId,
|
|
344
384
|
srcObjectId: assetId
|
|
345
385
|
});
|
|
346
386
|
});
|
|
347
|
-
|
|
348
|
-
|
|
387
|
+
|
|
388
|
+
// map csi documents and assets to content store documents and assets
|
|
389
|
+
const documents = mapCSIDocumentsToStoreDocuments({
|
|
390
|
+
csiDocuments: contentChangeEvent.documents,
|
|
349
391
|
contentSourceInstance: contentSourceData.instance,
|
|
350
392
|
modelMap: contentSourceData.modelMap,
|
|
351
393
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
352
394
|
});
|
|
353
|
-
const assets =
|
|
354
|
-
|
|
395
|
+
const assets = mapCSIAssetsToStoreAssets({
|
|
396
|
+
csiAssets: contentChangeEvent.assets,
|
|
355
397
|
contentSourceInstance: contentSourceData.instance,
|
|
356
398
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
357
399
|
});
|
|
358
400
|
|
|
401
|
+
// update contentSourceData with new or updated documents and assets
|
|
402
|
+
Object.assign(contentSourceData.csiDocumentMap, _.keyBy(contentChangeEvent.documents, 'id'));
|
|
403
|
+
Object.assign(contentSourceData.csiAssets, _.keyBy(contentChangeEvent.assets, 'id'));
|
|
359
404
|
Object.assign(contentSourceData.documentMap, _.keyBy(documents, 'srcObjectId'));
|
|
360
405
|
Object.assign(contentSourceData.assetMap, _.keyBy(assets, 'srcObjectId'));
|
|
361
|
-
|
|
362
|
-
|
|
363
|
-
|
|
406
|
+
|
|
407
|
+
for (let idx = 0; idx < documents.length; idx++) {
|
|
408
|
+
// the indexes of mapped documents and documents from changeEvent are the same
|
|
409
|
+
const document = documents[idx]!;
|
|
410
|
+
const csiDocument = contentChangeEvent.documents[idx]!;
|
|
411
|
+
const dataIndex = contentSourceData.documents.findIndex((existingDoc) => existingDoc.srcObjectId === document.srcObjectId);
|
|
412
|
+
if (dataIndex === -1) {
|
|
364
413
|
contentSourceData.documents.push(document);
|
|
414
|
+
contentSourceData.csiDocuments.push(csiDocument)
|
|
365
415
|
} else {
|
|
366
|
-
|
|
416
|
+
// the indexes of documents and csiDocuments are always the same as they are always updated at the same time
|
|
417
|
+
contentSourceData.documents.splice(dataIndex, 1, document);
|
|
418
|
+
contentSourceData.csiDocuments.splice(dataIndex, 1, csiDocument);
|
|
367
419
|
}
|
|
368
420
|
result.updatedDocuments.push({
|
|
369
421
|
srcType: contentSourceData.type,
|
|
@@ -371,12 +423,19 @@ export class ContentStore {
|
|
|
371
423
|
srcObjectId: document.srcObjectId
|
|
372
424
|
});
|
|
373
425
|
}
|
|
374
|
-
|
|
426
|
+
|
|
427
|
+
for (let idx = 0; idx < assets.length; idx++) {
|
|
428
|
+
// the indexes of mapped assets and assets from changeEvent are the same
|
|
429
|
+
const asset = assets[idx]!;
|
|
430
|
+
const csiAsset = contentChangeEvent.assets[idx]!;
|
|
375
431
|
const index = contentSourceData.assets.findIndex((existingAsset) => existingAsset.srcObjectId === asset.srcObjectId);
|
|
376
432
|
if (index === -1) {
|
|
377
433
|
contentSourceData.assets.push(asset);
|
|
434
|
+
contentSourceData.csiAssets.push(csiAsset);
|
|
378
435
|
} else {
|
|
436
|
+
// the indexes of assets and csiAssets are always the same as they are always updated at the same time
|
|
379
437
|
contentSourceData.assets.splice(index, 1, asset);
|
|
438
|
+
contentSourceData.csiAssets.splice(index, 1, csiAsset);
|
|
380
439
|
}
|
|
381
440
|
result.updatedAssets.push({
|
|
382
441
|
srcType: contentSourceData.type,
|
|
@@ -614,7 +673,8 @@ export class ContentStore {
|
|
|
614
673
|
|
|
615
674
|
// get the document that is being updated
|
|
616
675
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
617
|
-
|
|
676
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
677
|
+
if (!document || !csiDocument) {
|
|
618
678
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
619
679
|
}
|
|
620
680
|
|
|
@@ -663,7 +723,7 @@ export class ContentStore {
|
|
|
663
723
|
refId: result.srcDocumentId
|
|
664
724
|
} as const;
|
|
665
725
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
666
|
-
|
|
726
|
+
document: csiDocument,
|
|
667
727
|
modelMap: modelMap,
|
|
668
728
|
userContext: userContext,
|
|
669
729
|
operations: [
|
|
@@ -713,7 +773,8 @@ export class ContentStore {
|
|
|
713
773
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
714
774
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
715
775
|
const document = contentSourceData.documentMap[srcDocumentId];
|
|
716
|
-
|
|
776
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
777
|
+
if (!document || !csiDocument) {
|
|
717
778
|
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
718
779
|
}
|
|
719
780
|
|
|
@@ -753,7 +814,7 @@ export class ContentStore {
|
|
|
753
814
|
refId: result.id
|
|
754
815
|
} as const;
|
|
755
816
|
const updatedDocument = await contentSourceData.instance.updateDocument({
|
|
756
|
-
|
|
817
|
+
document: csiDocument,
|
|
757
818
|
modelMap: modelMap,
|
|
758
819
|
userContext: userContext,
|
|
759
820
|
operations: [
|
|
@@ -841,21 +902,23 @@ export class ContentStore {
|
|
|
841
902
|
|
|
842
903
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
843
904
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
844
|
-
const modelMap = contentSourceData.modelMap;
|
|
845
905
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
906
|
+
const document = contentSourceData.documentMap[srcDocumentId];
|
|
907
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
908
|
+
if (!document || !csiDocument) {
|
|
909
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
910
|
+
}
|
|
911
|
+
|
|
912
|
+
const modelMap = contentSourceData.modelMap;
|
|
913
|
+
const documentModelName = document.srcModelName;
|
|
914
|
+
const model = modelMap[documentModelName];
|
|
915
|
+
if (!model) {
|
|
916
|
+
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
917
|
+
}
|
|
846
918
|
|
|
847
919
|
const operations = await mapPromise(
|
|
848
920
|
updateOperations,
|
|
849
|
-
async (updateOperation): Promise<
|
|
850
|
-
const document = contentSourceData.documentMap[srcDocumentId];
|
|
851
|
-
if (!document) {
|
|
852
|
-
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
853
|
-
}
|
|
854
|
-
const documentModelName = document.srcModelName;
|
|
855
|
-
const model = modelMap[documentModelName];
|
|
856
|
-
if (!model) {
|
|
857
|
-
throw new Error(`error updating document, could not find document model: '${documentModelName}'`);
|
|
858
|
-
}
|
|
921
|
+
async (updateOperation): Promise<CSITypes.UpdateOperation> => {
|
|
859
922
|
const locale = updateOperation.locale ?? contentSourceData.defaultLocaleCode;
|
|
860
923
|
const modelField = getModelFieldForFieldAtPath(document, model, updateOperation.fieldPath, modelMap, locale);
|
|
861
924
|
switch (updateOperation.opType) {
|
|
@@ -899,8 +962,8 @@ export class ContentStore {
|
|
|
899
962
|
}
|
|
900
963
|
);
|
|
901
964
|
|
|
902
|
-
const
|
|
903
|
-
|
|
965
|
+
const updatedDocumentResult = await contentSourceData.instance.updateDocument({
|
|
966
|
+
document: csiDocument,
|
|
904
967
|
modelMap,
|
|
905
968
|
userContext,
|
|
906
969
|
operations
|
|
@@ -911,7 +974,7 @@ export class ContentStore {
|
|
|
911
974
|
// and use data from contentChangeEvent to update the cache
|
|
912
975
|
// contentSourceData.documentMap = Object.assign(contentSourceData.documentMap, { [document.srcObjectId]: document });
|
|
913
976
|
|
|
914
|
-
return { srcDocumentId:
|
|
977
|
+
return { srcDocumentId: updatedDocumentResult.id };
|
|
915
978
|
}
|
|
916
979
|
|
|
917
980
|
async duplicateDocument({
|
|
@@ -976,7 +1039,7 @@ export class ContentStore {
|
|
|
976
1039
|
|
|
977
1040
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
978
1041
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
979
|
-
const sourceAssets:
|
|
1042
|
+
const sourceAssets: CSITypes.Asset[] = [];
|
|
980
1043
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
981
1044
|
|
|
982
1045
|
locale = locale ?? contentSourceData.defaultLocaleCode;
|
|
@@ -999,8 +1062,8 @@ export class ContentStore {
|
|
|
999
1062
|
});
|
|
1000
1063
|
sourceAssets.push(sourceAsset);
|
|
1001
1064
|
}
|
|
1002
|
-
const storeAssets =
|
|
1003
|
-
|
|
1065
|
+
const storeAssets = mapCSIAssetsToStoreAssets({
|
|
1066
|
+
csiAssets: sourceAssets,
|
|
1004
1067
|
contentSourceInstance: contentSourceData.instance,
|
|
1005
1068
|
defaultLocaleCode: contentSourceData.defaultLocaleCode
|
|
1006
1069
|
});
|
|
@@ -1023,7 +1086,11 @@ export class ContentStore {
|
|
|
1023
1086
|
const userContext = getUserContextForSrcType(srcType, user);
|
|
1024
1087
|
const contentSourceId = getContentSourceId(srcType, srcProjectId);
|
|
1025
1088
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
1026
|
-
|
|
1089
|
+
const csiDocument = contentSourceData.csiDocumentMap[srcDocumentId];
|
|
1090
|
+
if (!csiDocument) {
|
|
1091
|
+
throw new Error(`no document with id '${srcDocumentId}' was found in ${contentSourceData.id}`);
|
|
1092
|
+
}
|
|
1093
|
+
await contentSourceData.instance.deleteDocument({ document: csiDocument, userContext });
|
|
1027
1094
|
|
|
1028
1095
|
// do not update cache in contentSourceData.documents and documentMap,
|
|
1029
1096
|
// instead wait for contentSource to call onContentChange(contentChangeEvent)
|
|
@@ -1044,20 +1111,12 @@ export class ContentStore {
|
|
|
1044
1111
|
|
|
1045
1112
|
const objectsBySourceId = _.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
1046
1113
|
let errors: ContentStoreTypes.ValidationError[] = [];
|
|
1047
|
-
for (const [contentSourceId,
|
|
1114
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
1048
1115
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
1049
1116
|
locale = locale ?? contentSourceData.defaultLocaleCode;
|
|
1050
|
-
const
|
|
1051
|
-
const assetIds = [];
|
|
1052
|
-
for (const object of objects) {
|
|
1053
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
1054
|
-
documentIds.push(object.srcObjectId);
|
|
1055
|
-
} else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
1056
|
-
assetIds.push(object.srcObjectId);
|
|
1057
|
-
}
|
|
1058
|
-
}
|
|
1117
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
1059
1118
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
1060
|
-
const validationResult = await contentSourceData.instance.validateDocuments({
|
|
1119
|
+
const validationResult = await contentSourceData.instance.validateDocuments({ documents, assets, locale, userContext });
|
|
1061
1120
|
errors = errors.concat(
|
|
1062
1121
|
validationResult.errors.map((validationError) => ({
|
|
1063
1122
|
message: validationError.message,
|
|
@@ -1093,19 +1152,11 @@ export class ContentStore {
|
|
|
1093
1152
|
this.logger.debug('publishDocuments');
|
|
1094
1153
|
|
|
1095
1154
|
const objectsBySourceId = _.groupBy(objects, (object) => getContentSourceId(object.srcType, object.srcProjectId));
|
|
1096
|
-
for (const [contentSourceId,
|
|
1155
|
+
for (const [contentSourceId, contentSourceObjects] of Object.entries(objectsBySourceId)) {
|
|
1097
1156
|
const contentSourceData = this.getContentSourceDataByIdOrThrow(contentSourceId);
|
|
1098
1157
|
const userContext = getUserContextForSrcType(contentSourceData.type, user);
|
|
1099
|
-
const
|
|
1100
|
-
|
|
1101
|
-
for (const object of objects) {
|
|
1102
|
-
if (object.srcObjectId in contentSourceData.documentMap) {
|
|
1103
|
-
documentIds.push(object.srcObjectId);
|
|
1104
|
-
} else if (object.srcObjectId in contentSourceData.assetMap) {
|
|
1105
|
-
assetIds.push(object.srcObjectId);
|
|
1106
|
-
}
|
|
1107
|
-
}
|
|
1108
|
-
await contentSourceData.instance.publishDocuments({ documentIds, assetIds, userContext });
|
|
1158
|
+
const { documents, assets } = getCSIDocumentsAndAssetsFromContentSourceDataByIds(contentSourceData, contentSourceObjects);
|
|
1159
|
+
await contentSourceData.instance.publishDocuments({ documents, assets, userContext });
|
|
1109
1160
|
}
|
|
1110
1161
|
}
|
|
1111
1162
|
|
|
@@ -1126,13 +1177,13 @@ function getUserContextForSrcType(srcType: string, user?: ContentStoreTypes.User
|
|
|
1126
1177
|
return user?.connections?.find((connection) => connection.type === srcType);
|
|
1127
1178
|
}
|
|
1128
1179
|
|
|
1129
|
-
function
|
|
1130
|
-
|
|
1180
|
+
function mapCSIAssetsToStoreAssets({
|
|
1181
|
+
csiAssets,
|
|
1131
1182
|
contentSourceInstance,
|
|
1132
1183
|
defaultLocaleCode
|
|
1133
1184
|
}: {
|
|
1134
|
-
|
|
1135
|
-
contentSourceInstance:
|
|
1185
|
+
csiAssets: CSITypes.Asset[];
|
|
1186
|
+
contentSourceInstance: CSITypes.ContentSourceInterface;
|
|
1136
1187
|
defaultLocaleCode?: string;
|
|
1137
1188
|
}): ContentStoreTypes.Asset[] {
|
|
1138
1189
|
const extra = {
|
|
@@ -1141,53 +1192,53 @@ function mapSourceAssetsToStoreAssets({
|
|
|
1141
1192
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
1142
1193
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
1143
1194
|
};
|
|
1144
|
-
return
|
|
1195
|
+
return csiAssets.map((csiAsset) => sourceAssetToStoreAsset({ csiAsset, defaultLocaleCode, extra }));
|
|
1145
1196
|
}
|
|
1146
1197
|
|
|
1147
1198
|
function sourceAssetToStoreAsset({
|
|
1148
|
-
|
|
1199
|
+
csiAsset,
|
|
1149
1200
|
defaultLocaleCode,
|
|
1150
1201
|
extra
|
|
1151
1202
|
}: {
|
|
1152
|
-
|
|
1203
|
+
csiAsset: CSITypes.Asset;
|
|
1153
1204
|
defaultLocaleCode?: string;
|
|
1154
1205
|
extra: { srcType: string; srcProjectId: string; srcProjectUrl: string; srcEnvironment: string };
|
|
1155
1206
|
}): ContentStoreTypes.Asset {
|
|
1156
1207
|
return {
|
|
1157
1208
|
type: 'asset',
|
|
1158
1209
|
...extra,
|
|
1159
|
-
srcObjectId:
|
|
1160
|
-
srcObjectUrl:
|
|
1161
|
-
srcObjectLabel: getObjectLabel(
|
|
1210
|
+
srcObjectId: csiAsset.id,
|
|
1211
|
+
srcObjectUrl: csiAsset.manageUrl,
|
|
1212
|
+
srcObjectLabel: getObjectLabel(csiAsset.fields, IMAGE_MODEL, defaultLocaleCode),
|
|
1162
1213
|
srcModelName: IMAGE_MODEL.name,
|
|
1163
1214
|
srcModelLabel: IMAGE_MODEL.label!,
|
|
1164
|
-
isChanged:
|
|
1165
|
-
status:
|
|
1166
|
-
createdAt:
|
|
1167
|
-
createdBy:
|
|
1168
|
-
updatedAt:
|
|
1169
|
-
updatedBy:
|
|
1215
|
+
isChanged: csiAsset.status === 'added' || csiAsset.status === 'modified',
|
|
1216
|
+
status: csiAsset.status,
|
|
1217
|
+
createdAt: csiAsset.createdAt,
|
|
1218
|
+
createdBy: csiAsset.createdBy,
|
|
1219
|
+
updatedAt: csiAsset.updatedAt,
|
|
1220
|
+
updatedBy: csiAsset.updatedBy,
|
|
1170
1221
|
fields: {
|
|
1171
1222
|
title: {
|
|
1172
1223
|
label: 'Title',
|
|
1173
|
-
...
|
|
1224
|
+
...csiAsset.fields.title
|
|
1174
1225
|
},
|
|
1175
1226
|
file: {
|
|
1176
1227
|
label: 'File',
|
|
1177
|
-
...
|
|
1228
|
+
...csiAsset.fields.file
|
|
1178
1229
|
}
|
|
1179
1230
|
}
|
|
1180
1231
|
};
|
|
1181
1232
|
}
|
|
1182
1233
|
|
|
1183
|
-
function
|
|
1184
|
-
|
|
1234
|
+
function mapCSIDocumentsToStoreDocuments({
|
|
1235
|
+
csiDocuments,
|
|
1185
1236
|
contentSourceInstance,
|
|
1186
1237
|
modelMap,
|
|
1187
1238
|
defaultLocaleCode
|
|
1188
1239
|
}: {
|
|
1189
|
-
|
|
1190
|
-
contentSourceInstance:
|
|
1240
|
+
csiDocuments: CSITypes.Document[];
|
|
1241
|
+
contentSourceInstance: CSITypes.ContentSourceInterface;
|
|
1191
1242
|
modelMap: Record<string, Model>;
|
|
1192
1243
|
defaultLocaleCode?: string;
|
|
1193
1244
|
}): ContentStoreTypes.Document[] {
|
|
@@ -1197,19 +1248,19 @@ function mapSourceDocumentsToStoreDocuments({
|
|
|
1197
1248
|
srcProjectUrl: contentSourceInstance.getProjectManageUrl(),
|
|
1198
1249
|
srcEnvironment: contentSourceInstance.getProjectEnvironment()
|
|
1199
1250
|
};
|
|
1200
|
-
return
|
|
1201
|
-
|
|
1251
|
+
return csiDocuments.map((csiDocument) =>
|
|
1252
|
+
mapCSIDocumentToStoreDocument({ csiDocument, model: modelMap[csiDocument.modelName]!, modelMap, defaultLocaleCode, extra })
|
|
1202
1253
|
);
|
|
1203
1254
|
}
|
|
1204
1255
|
|
|
1205
|
-
function
|
|
1206
|
-
|
|
1256
|
+
function mapCSIDocumentToStoreDocument({
|
|
1257
|
+
csiDocument,
|
|
1207
1258
|
model,
|
|
1208
1259
|
modelMap,
|
|
1209
1260
|
defaultLocaleCode,
|
|
1210
1261
|
extra
|
|
1211
1262
|
}: {
|
|
1212
|
-
|
|
1263
|
+
csiDocument: CSITypes.Document;
|
|
1213
1264
|
model: Model;
|
|
1214
1265
|
modelMap: Record<string, Model>;
|
|
1215
1266
|
defaultLocaleCode?: string;
|
|
@@ -1218,19 +1269,19 @@ function mapSourceDocumentToStoreDocument({
|
|
|
1218
1269
|
return {
|
|
1219
1270
|
type: 'document',
|
|
1220
1271
|
...extra,
|
|
1221
|
-
srcObjectId:
|
|
1222
|
-
srcObjectUrl:
|
|
1223
|
-
srcObjectLabel: getObjectLabel(
|
|
1224
|
-
srcModelLabel: model.label ?? _.startCase(
|
|
1225
|
-
srcModelName:
|
|
1226
|
-
isChanged:
|
|
1227
|
-
status:
|
|
1228
|
-
createdAt:
|
|
1229
|
-
createdBy:
|
|
1230
|
-
updatedAt:
|
|
1231
|
-
updatedBy:
|
|
1232
|
-
fields:
|
|
1233
|
-
|
|
1272
|
+
srcObjectId: csiDocument.id,
|
|
1273
|
+
srcObjectUrl: csiDocument.manageUrl,
|
|
1274
|
+
srcObjectLabel: getObjectLabel(csiDocument.fields, model, defaultLocaleCode),
|
|
1275
|
+
srcModelLabel: model.label ?? _.startCase(csiDocument.modelName),
|
|
1276
|
+
srcModelName: csiDocument.modelName,
|
|
1277
|
+
isChanged: csiDocument.status === 'added' || csiDocument.status === 'modified',
|
|
1278
|
+
status: csiDocument.status,
|
|
1279
|
+
createdAt: csiDocument.createdAt,
|
|
1280
|
+
createdBy: csiDocument.createdBy,
|
|
1281
|
+
updatedAt: csiDocument.updatedAt,
|
|
1282
|
+
updatedBy: csiDocument.updatedBy,
|
|
1283
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1284
|
+
csiDocumentFields: csiDocument.fields,
|
|
1234
1285
|
modelFields: model.fields ?? [],
|
|
1235
1286
|
modelMap,
|
|
1236
1287
|
defaultLocaleCode
|
|
@@ -1238,21 +1289,21 @@ function mapSourceDocumentToStoreDocument({
|
|
|
1238
1289
|
};
|
|
1239
1290
|
}
|
|
1240
1291
|
|
|
1241
|
-
function
|
|
1242
|
-
|
|
1292
|
+
function mapCSIFieldsToStoreFields({
|
|
1293
|
+
csiDocumentFields,
|
|
1243
1294
|
modelFields,
|
|
1244
1295
|
modelMap,
|
|
1245
1296
|
defaultLocaleCode
|
|
1246
1297
|
}: {
|
|
1247
|
-
|
|
1298
|
+
csiDocumentFields: Record<string, CSITypes.DocumentField>;
|
|
1248
1299
|
modelFields: Field[];
|
|
1249
1300
|
modelMap: Record<string, Model>;
|
|
1250
1301
|
defaultLocaleCode?: string;
|
|
1251
1302
|
}): Record<string, ContentStoreTypes.DocumentField> {
|
|
1252
1303
|
return modelFields.reduce((result: Record<string, ContentStoreTypes.DocumentField>, modelField) => {
|
|
1253
|
-
const
|
|
1304
|
+
const csiDocumentField = csiDocumentFields[modelField.name];
|
|
1254
1305
|
const docField = mapSourceFieldToStoreField({
|
|
1255
|
-
|
|
1306
|
+
csiDocumentField,
|
|
1256
1307
|
modelField,
|
|
1257
1308
|
modelMap,
|
|
1258
1309
|
defaultLocaleCode
|
|
@@ -1264,17 +1315,17 @@ function mapSourceFieldsToStoreFields({
|
|
|
1264
1315
|
}
|
|
1265
1316
|
|
|
1266
1317
|
function mapSourceFieldToStoreField({
|
|
1267
|
-
|
|
1318
|
+
csiDocumentField,
|
|
1268
1319
|
modelField,
|
|
1269
1320
|
modelMap,
|
|
1270
1321
|
defaultLocaleCode
|
|
1271
1322
|
}: {
|
|
1272
|
-
|
|
1323
|
+
csiDocumentField: CSITypes.DocumentField | undefined;
|
|
1273
1324
|
modelField: FieldSpecificProps;
|
|
1274
1325
|
modelMap: Record<string, Model>;
|
|
1275
1326
|
defaultLocaleCode?: string;
|
|
1276
1327
|
}): ContentStoreTypes.DocumentField {
|
|
1277
|
-
if (!
|
|
1328
|
+
if (!csiDocumentField) {
|
|
1278
1329
|
const isUnset = ['object', 'model', 'reference', 'richText', 'markdown', 'image', 'file', 'json'].includes(modelField.type);
|
|
1279
1330
|
return {
|
|
1280
1331
|
type: modelField.type,
|
|
@@ -1285,32 +1336,32 @@ function mapSourceFieldToStoreField({
|
|
|
1285
1336
|
// TODO: check if need to add "options" to "enum" and subtype/min/max to "number"
|
|
1286
1337
|
switch (modelField.type) {
|
|
1287
1338
|
case 'object':
|
|
1288
|
-
return mapObjectField(
|
|
1339
|
+
return mapObjectField(csiDocumentField as CSITypes.DocumentObjectField, modelField, modelMap, defaultLocaleCode);
|
|
1289
1340
|
case 'model':
|
|
1290
|
-
return mapModelField(
|
|
1341
|
+
return mapModelField(csiDocumentField as CSITypes.DocumentModelField, modelField, modelMap, defaultLocaleCode);
|
|
1291
1342
|
case 'list':
|
|
1292
|
-
return mapListField(
|
|
1343
|
+
return mapListField(csiDocumentField as CSITypes.DocumentListField, modelField, modelMap, defaultLocaleCode);
|
|
1293
1344
|
case 'richText':
|
|
1294
|
-
return mapRichTextField(
|
|
1345
|
+
return mapRichTextField(csiDocumentField as CSITypes.DocumentRichTextField);
|
|
1295
1346
|
case 'markdown':
|
|
1296
|
-
return mapMarkdownField(
|
|
1347
|
+
return mapMarkdownField(csiDocumentField as CSITypes.DocumentValueField);
|
|
1297
1348
|
default:
|
|
1298
|
-
return
|
|
1349
|
+
return csiDocumentField as ContentStoreTypes.DocumentField;
|
|
1299
1350
|
}
|
|
1300
1351
|
}
|
|
1301
1352
|
|
|
1302
1353
|
function mapObjectField(
|
|
1303
|
-
|
|
1354
|
+
csiDocumentField: CSITypes.DocumentObjectField,
|
|
1304
1355
|
modelField: FieldObjectProps,
|
|
1305
1356
|
modelMap: Record<string, Model>,
|
|
1306
1357
|
defaultLocaleCode?: string
|
|
1307
1358
|
): ContentStoreTypes.DocumentObjectField {
|
|
1308
|
-
if (!isLocalizedField(
|
|
1359
|
+
if (!isLocalizedField(csiDocumentField)) {
|
|
1309
1360
|
return {
|
|
1310
|
-
type:
|
|
1311
|
-
srcObjectLabel: getObjectLabel(
|
|
1312
|
-
fields:
|
|
1313
|
-
|
|
1361
|
+
type: csiDocumentField.type,
|
|
1362
|
+
srcObjectLabel: getObjectLabel(csiDocumentField.fields ?? {}, modelField ?? [], defaultLocaleCode),
|
|
1363
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1364
|
+
csiDocumentFields: csiDocumentField.fields ?? {},
|
|
1314
1365
|
modelFields: modelField.fields ?? [],
|
|
1315
1366
|
modelMap,
|
|
1316
1367
|
defaultLocaleCode
|
|
@@ -1318,14 +1369,14 @@ function mapObjectField(
|
|
|
1318
1369
|
};
|
|
1319
1370
|
}
|
|
1320
1371
|
return {
|
|
1321
|
-
type:
|
|
1372
|
+
type: csiDocumentField.type,
|
|
1322
1373
|
localized: true,
|
|
1323
|
-
locales: _.mapValues(
|
|
1374
|
+
locales: _.mapValues(csiDocumentField.locales, (locale) => {
|
|
1324
1375
|
return {
|
|
1325
1376
|
locale: locale.locale,
|
|
1326
1377
|
srcObjectLabel: getObjectLabel(locale.fields ?? {}, modelField, locale.locale),
|
|
1327
|
-
fields:
|
|
1328
|
-
|
|
1378
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1379
|
+
csiDocumentFields: locale.fields ?? {},
|
|
1329
1380
|
modelFields: modelField.fields ?? [],
|
|
1330
1381
|
modelMap,
|
|
1331
1382
|
defaultLocaleCode
|
|
@@ -1336,20 +1387,20 @@ function mapObjectField(
|
|
|
1336
1387
|
}
|
|
1337
1388
|
|
|
1338
1389
|
function mapModelField(
|
|
1339
|
-
|
|
1390
|
+
csiDocumentField: CSITypes.DocumentModelField,
|
|
1340
1391
|
modelField: FieldModelProps,
|
|
1341
1392
|
modelMap: Record<string, Model>,
|
|
1342
1393
|
defaultLocaleCode?: string
|
|
1343
1394
|
): ContentStoreTypes.DocumentModelField {
|
|
1344
|
-
if (!isLocalizedField(
|
|
1345
|
-
const model = modelMap[
|
|
1395
|
+
if (!isLocalizedField(csiDocumentField)) {
|
|
1396
|
+
const model = modelMap[csiDocumentField.modelName]!;
|
|
1346
1397
|
return {
|
|
1347
|
-
type:
|
|
1348
|
-
srcObjectLabel: getObjectLabel(
|
|
1349
|
-
srcModelName:
|
|
1398
|
+
type: csiDocumentField.type,
|
|
1399
|
+
srcObjectLabel: getObjectLabel(csiDocumentField.fields ?? {}, model, defaultLocaleCode),
|
|
1400
|
+
srcModelName: csiDocumentField.modelName,
|
|
1350
1401
|
srcModelLabel: model.label ?? _.startCase(model.name),
|
|
1351
|
-
fields:
|
|
1352
|
-
|
|
1402
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1403
|
+
csiDocumentFields: csiDocumentField.fields ?? {},
|
|
1353
1404
|
modelFields: model.fields ?? [],
|
|
1354
1405
|
modelMap,
|
|
1355
1406
|
defaultLocaleCode
|
|
@@ -1357,17 +1408,17 @@ function mapModelField(
|
|
|
1357
1408
|
};
|
|
1358
1409
|
}
|
|
1359
1410
|
return {
|
|
1360
|
-
type:
|
|
1411
|
+
type: csiDocumentField.type,
|
|
1361
1412
|
localized: true,
|
|
1362
|
-
locales: _.mapValues(
|
|
1413
|
+
locales: _.mapValues(csiDocumentField.locales, (locale) => {
|
|
1363
1414
|
const model = modelMap[locale.modelName]!;
|
|
1364
1415
|
return {
|
|
1365
1416
|
locale: locale.locale,
|
|
1366
1417
|
srcObjectLabel: getObjectLabel(locale.fields ?? {}, model, locale.locale),
|
|
1367
1418
|
srcModelName: locale.modelName,
|
|
1368
1419
|
srcModelLabel: model.label ?? _.startCase(model.name),
|
|
1369
|
-
fields:
|
|
1370
|
-
|
|
1420
|
+
fields: mapCSIFieldsToStoreFields({
|
|
1421
|
+
csiDocumentFields: locale.fields ?? {},
|
|
1371
1422
|
modelFields: model.fields ?? [],
|
|
1372
1423
|
modelMap,
|
|
1373
1424
|
defaultLocaleCode
|
|
@@ -1378,17 +1429,17 @@ function mapModelField(
|
|
|
1378
1429
|
}
|
|
1379
1430
|
|
|
1380
1431
|
function mapListField(
|
|
1381
|
-
|
|
1432
|
+
csiDocumentField: CSITypes.DocumentListField,
|
|
1382
1433
|
modelField: FieldListProps,
|
|
1383
1434
|
modelMap: Record<string, Model>,
|
|
1384
1435
|
defaultLocaleCode?: string
|
|
1385
1436
|
): ContentStoreTypes.DocumentListField {
|
|
1386
|
-
if (!isLocalizedField(
|
|
1437
|
+
if (!isLocalizedField(csiDocumentField)) {
|
|
1387
1438
|
return {
|
|
1388
|
-
type:
|
|
1389
|
-
items:
|
|
1439
|
+
type: csiDocumentField.type,
|
|
1440
|
+
items: csiDocumentField.items.map((item) =>
|
|
1390
1441
|
mapSourceFieldToStoreField({
|
|
1391
|
-
|
|
1442
|
+
csiDocumentField: item,
|
|
1392
1443
|
modelField: modelField.items ?? { type: 'string' },
|
|
1393
1444
|
modelMap,
|
|
1394
1445
|
defaultLocaleCode
|
|
@@ -1397,14 +1448,14 @@ function mapListField(
|
|
|
1397
1448
|
};
|
|
1398
1449
|
}
|
|
1399
1450
|
return {
|
|
1400
|
-
type:
|
|
1451
|
+
type: csiDocumentField.type,
|
|
1401
1452
|
localized: true,
|
|
1402
|
-
locales: _.mapValues(
|
|
1453
|
+
locales: _.mapValues(csiDocumentField.locales, (locale) => {
|
|
1403
1454
|
return {
|
|
1404
1455
|
locale: locale.locale,
|
|
1405
1456
|
items: (locale.items ?? []).map((item) =>
|
|
1406
1457
|
mapSourceFieldToStoreField({
|
|
1407
|
-
|
|
1458
|
+
csiDocumentField: item,
|
|
1408
1459
|
modelField: modelField.items ?? { type: 'string' },
|
|
1409
1460
|
modelMap,
|
|
1410
1461
|
defaultLocaleCode
|
|
@@ -1415,17 +1466,17 @@ function mapListField(
|
|
|
1415
1466
|
};
|
|
1416
1467
|
}
|
|
1417
1468
|
|
|
1418
|
-
function mapRichTextField(
|
|
1419
|
-
if (!isLocalizedField(
|
|
1469
|
+
function mapRichTextField(csiDocumentField: CSITypes.DocumentRichTextField): ContentStoreTypes.DocumentRichTextField {
|
|
1470
|
+
if (!isLocalizedField(csiDocumentField)) {
|
|
1420
1471
|
return {
|
|
1421
|
-
...
|
|
1472
|
+
...csiDocumentField,
|
|
1422
1473
|
multiElement: true
|
|
1423
1474
|
};
|
|
1424
1475
|
}
|
|
1425
1476
|
return {
|
|
1426
|
-
type:
|
|
1477
|
+
type: csiDocumentField.type,
|
|
1427
1478
|
localized: true,
|
|
1428
|
-
locales: _.mapValues(
|
|
1479
|
+
locales: _.mapValues(csiDocumentField.locales, (locale) => {
|
|
1429
1480
|
return {
|
|
1430
1481
|
...locale,
|
|
1431
1482
|
multiElement: true
|
|
@@ -1434,18 +1485,18 @@ function mapRichTextField(documentField: ContentSourceTypes.DocumentRichTextFiel
|
|
|
1434
1485
|
};
|
|
1435
1486
|
}
|
|
1436
1487
|
|
|
1437
|
-
function mapMarkdownField(
|
|
1438
|
-
if (!isLocalizedField(
|
|
1488
|
+
function mapMarkdownField(csiDocumentField: CSITypes.DocumentValueField): ContentStoreTypes.DocumentMarkdownField {
|
|
1489
|
+
if (!isLocalizedField(csiDocumentField)) {
|
|
1439
1490
|
return {
|
|
1440
1491
|
type: 'markdown',
|
|
1441
|
-
value:
|
|
1492
|
+
value: csiDocumentField.value,
|
|
1442
1493
|
multiElement: true
|
|
1443
1494
|
};
|
|
1444
1495
|
}
|
|
1445
1496
|
return {
|
|
1446
1497
|
type: 'markdown',
|
|
1447
1498
|
localized: true,
|
|
1448
|
-
locales: _.mapValues(
|
|
1499
|
+
locales: _.mapValues(csiDocumentField.locales, (locale) => {
|
|
1449
1500
|
return {
|
|
1450
1501
|
...locale,
|
|
1451
1502
|
multiElement: true
|
|
@@ -1462,12 +1513,12 @@ function mapStoreFieldsToSourceFields({
|
|
|
1462
1513
|
documentFields: Record<string, ContentStoreTypes.DocumentField>;
|
|
1463
1514
|
modelFields: Field[];
|
|
1464
1515
|
modelMap: Record<string, Model>;
|
|
1465
|
-
}): Record<string,
|
|
1466
|
-
// TODO:
|
|
1516
|
+
}): Record<string, CSITypes.DocumentField> {
|
|
1517
|
+
// TODO: implement
|
|
1467
1518
|
throw new Error(`duplicateDocument not implemented yet`);
|
|
1468
1519
|
}
|
|
1469
1520
|
|
|
1470
|
-
function getContentSourceIdForContentSource(contentSource:
|
|
1521
|
+
function getContentSourceIdForContentSource(contentSource: CSITypes.ContentSourceInterface): string {
|
|
1471
1522
|
return getContentSourceId(contentSource.getContentSourceType(), contentSource.getProjectId());
|
|
1472
1523
|
}
|
|
1473
1524
|
|
|
@@ -1483,7 +1534,7 @@ function sanitizeSlug(slug: string) {
|
|
|
1483
1534
|
}
|
|
1484
1535
|
|
|
1485
1536
|
function getObjectLabel(
|
|
1486
|
-
documentFields: Record<string,
|
|
1537
|
+
documentFields: Record<string, CSITypes.DocumentField | CSITypes.AssetFileField>,
|
|
1487
1538
|
modelOrObjectField: Model | FieldObjectProps,
|
|
1488
1539
|
locale?: string
|
|
1489
1540
|
): string {
|
|
@@ -1671,8 +1722,8 @@ async function createDocumentRecursively({
|
|
|
1671
1722
|
modelMap: Record<string, Model>;
|
|
1672
1723
|
locale?: string;
|
|
1673
1724
|
userContext: unknown;
|
|
1674
|
-
contentSourceInstance:
|
|
1675
|
-
}): Promise<{ document:
|
|
1725
|
+
contentSourceInstance: CSITypes.ContentSourceInterface;
|
|
1726
|
+
}): Promise<{ document: CSITypes.Document; newRefDocuments: CSITypes.Document[] }> {
|
|
1676
1727
|
if (model.type === 'page') {
|
|
1677
1728
|
const tokens = extractTokensFromString(String(model.urlPath));
|
|
1678
1729
|
const slugField = _.last(tokens);
|
|
@@ -1719,10 +1770,10 @@ async function createNestedObjectRecursively({
|
|
|
1719
1770
|
modelMap: Record<string, Model>;
|
|
1720
1771
|
locale?: string;
|
|
1721
1772
|
userContext: unknown;
|
|
1722
|
-
contentSourceInstance:
|
|
1773
|
+
contentSourceInstance: CSITypes.ContentSourceInterface;
|
|
1723
1774
|
}): Promise<{
|
|
1724
|
-
fields: Record<string,
|
|
1725
|
-
newRefDocuments:
|
|
1775
|
+
fields: Record<string, CSITypes.DocumentFieldNonLocalized>;
|
|
1776
|
+
newRefDocuments: CSITypes.Document[];
|
|
1726
1777
|
}> {
|
|
1727
1778
|
const createNestedField = async ({
|
|
1728
1779
|
value,
|
|
@@ -1732,7 +1783,7 @@ async function createNestedObjectRecursively({
|
|
|
1732
1783
|
value: any;
|
|
1733
1784
|
modelField: FieldSpecificProps;
|
|
1734
1785
|
fieldPath: (string | number)[];
|
|
1735
|
-
}): Promise<{ field:
|
|
1786
|
+
}): Promise<{ field: CSITypes.DocumentFieldNonLocalized; newRefDocuments: CSITypes.Document[] }> => {
|
|
1736
1787
|
if (modelField.type === 'object') {
|
|
1737
1788
|
const result = await createNestedObjectRecursively({
|
|
1738
1789
|
object: value,
|
|
@@ -1866,22 +1917,22 @@ async function createNestedObjectRecursively({
|
|
|
1866
1917
|
type: 'list',
|
|
1867
1918
|
items: arrayResult.map((result) => result.field)
|
|
1868
1919
|
},
|
|
1869
|
-
newRefDocuments: arrayResult.reduce((result:
|
|
1920
|
+
newRefDocuments: arrayResult.reduce((result: CSITypes.Document[], { newRefDocuments }) => result.concat(newRefDocuments), [])
|
|
1870
1921
|
};
|
|
1871
1922
|
}
|
|
1872
1923
|
return {
|
|
1873
1924
|
field: {
|
|
1874
1925
|
type: modelField.type,
|
|
1875
1926
|
value: value
|
|
1876
|
-
} as
|
|
1927
|
+
} as CSITypes.DocumentFieldNonLocalized,
|
|
1877
1928
|
newRefDocuments: []
|
|
1878
1929
|
};
|
|
1879
1930
|
};
|
|
1880
1931
|
|
|
1881
1932
|
object = object ?? {};
|
|
1882
1933
|
const result: {
|
|
1883
|
-
fields: Record<string,
|
|
1884
|
-
newRefDocuments:
|
|
1934
|
+
fields: Record<string, CSITypes.DocumentFieldNonLocalized>;
|
|
1935
|
+
newRefDocuments: CSITypes.Document[];
|
|
1885
1936
|
} = {
|
|
1886
1937
|
fields: {},
|
|
1887
1938
|
newRefDocuments: []
|
|
@@ -2040,8 +2091,8 @@ async function convertOperationField({
|
|
|
2040
2091
|
modelMap: Record<string, Model>;
|
|
2041
2092
|
locale?: string;
|
|
2042
2093
|
userContext: unknown;
|
|
2043
|
-
contentSourceInstance:
|
|
2044
|
-
}): Promise<
|
|
2094
|
+
contentSourceInstance: CSITypes.ContentSourceInterface;
|
|
2095
|
+
}): Promise<CSITypes.UpdateOperationField> {
|
|
2045
2096
|
let result;
|
|
2046
2097
|
switch (operationField.type) {
|
|
2047
2098
|
case 'object':
|
|
@@ -2078,7 +2129,7 @@ async function convertOperationField({
|
|
|
2078
2129
|
fields: result.fields
|
|
2079
2130
|
};
|
|
2080
2131
|
default:
|
|
2081
|
-
return operationField as
|
|
2132
|
+
return operationField as CSITypes.UpdateOperationField;
|
|
2082
2133
|
}
|
|
2083
2134
|
}
|
|
2084
2135
|
|
|
@@ -2110,3 +2161,25 @@ function isStackbitConfigFile(filePath: string) {
|
|
|
2110
2161
|
const isMainStackbitConfigFile = pathObject.name === 'stackbit' && ['yaml', 'yml'].includes(pathObject.ext.substring(1));
|
|
2111
2162
|
return isMainStackbitConfigFile || isInDotStackbitFolder;
|
|
2112
2163
|
}
|
|
2164
|
+
|
|
2165
|
+
function getCSIDocumentsAndAssetsFromContentSourceDataByIds(
|
|
2166
|
+
contentSourceData: ContentSourceData,
|
|
2167
|
+
objects: { srcObjectId: string }[]
|
|
2168
|
+
): {
|
|
2169
|
+
documents: CSITypes.Document[];
|
|
2170
|
+
assets: CSITypes.Asset[];
|
|
2171
|
+
} {
|
|
2172
|
+
const documents: CSITypes.Document[] = [];
|
|
2173
|
+
const assets: CSITypes.Asset[] = [];
|
|
2174
|
+
for (const object of objects) {
|
|
2175
|
+
if (object.srcObjectId in contentSourceData.csiDocumentMap) {
|
|
2176
|
+
documents.push(contentSourceData.csiDocumentMap[object.srcObjectId]!);
|
|
2177
|
+
} else if (object.srcObjectId in contentSourceData.csiAssetMap) {
|
|
2178
|
+
assets.push(contentSourceData.csiAssetMap[object.srcObjectId]!);
|
|
2179
|
+
}
|
|
2180
|
+
}
|
|
2181
|
+
return {
|
|
2182
|
+
documents,
|
|
2183
|
+
assets
|
|
2184
|
+
};
|
|
2185
|
+
}
|