@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-remove-recursion.9 → 0.24.14-alpha.jp-update-cr-links-unit.10

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.
@@ -188,140 +188,6 @@ export class CustomTypesManager extends BaseManager {
188
188
  };
189
189
  }
190
190
 
191
- private updateCRCustomType(
192
- args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
193
- ): CrCustomType {
194
- const { previousPath, newPath } = args;
195
-
196
- const customType = shallowClone(args.customType);
197
-
198
- const [previousCustomTypeId, previousFieldId] = previousPath;
199
- const [newCustomTypeId, newFieldId] = newPath;
200
-
201
- if (!previousCustomTypeId || !newCustomTypeId) {
202
- throw new Error(
203
- "Didn't find any customtype id, which should not be possible.",
204
- );
205
- }
206
-
207
- if (!previousFieldId || !newFieldId) {
208
- throw new Error(
209
- "Didn't find any field id, which should not be possible.",
210
- );
211
- }
212
-
213
- if (typeof customType === "string") {
214
- return customType; // we don't support custom type id renaming
215
- }
216
-
217
- if (customType.fields) {
218
- const newFields = customType.fields.map((fieldArg) => {
219
- const nestedField = shallowClone(fieldArg);
220
-
221
- if (typeof nestedField === "string") {
222
- if (nestedField === previousFieldId && nestedField !== newFieldId) {
223
- // We have reached a field id that matches the id that was renamed,
224
- // so we update it new one. The field is a string, so return the new
225
- // id.
226
- return newFieldId;
227
- }
228
-
229
- return nestedField;
230
- }
231
-
232
- if (
233
- nestedField.id === previousFieldId &&
234
- nestedField.id !== newFieldId
235
- ) {
236
- // We have reached a field id that matches the id that was renamed,
237
- // so we update it new one.
238
- // Since field is not a string, we don't exit, as we might have
239
- // something to update further down in customtypes.
240
- nestedField.id = newFieldId;
241
- }
242
-
243
- return {
244
- ...nestedField,
245
- customtypes: nestedField.customtypes.map((customTypeArg) => {
246
- const customTypeField = shallowClone(customTypeArg);
247
-
248
- if (typeof customTypeField === "string") {
249
- return customTypeField; // we don't support custom type id renaming
250
- }
251
-
252
- if (customTypeField.fields) {
253
- return {
254
- ...customTypeField,
255
- fields: customTypeField.fields.map((fieldArg) => {
256
- const nestedCustomTypeField = shallowClone(fieldArg);
257
-
258
- if (
259
- nestedCustomTypeField === previousFieldId &&
260
- nestedCustomTypeField !== newFieldId
261
- ) {
262
- // Matches the previous id, so we update it and return because
263
- // it's the last level.
264
- return newFieldId;
265
- }
266
-
267
- return nestedCustomTypeField;
268
- }),
269
- };
270
- }
271
-
272
- return customTypeField;
273
- }),
274
- };
275
- });
276
-
277
- return { ...customType, fields: newFields };
278
- }
279
-
280
- return customType;
281
- }
282
-
283
- /**
284
- * Map over the custom types of a Content Relationship Link and update the API
285
- * IDs that were changed during the custom type update.
286
- */
287
- private updateCRCustomTypes(
288
- args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
289
- ): CrCustomTypes {
290
- const { customTypes, ...updateMeta } = args;
291
-
292
- return customTypes.map((customType) => {
293
- return this.updateCRCustomType({ customType, ...updateMeta });
294
- });
295
- }
296
-
297
- /**
298
- * Update the Content Relationship API IDs of a single field. The change is
299
- * determined by the `previousPath` and `newPath` properties.
300
- */
301
- private updateFieldContentRelationships<
302
- T extends UID | NestableWidget | Group | NestedGroup,
303
- >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
304
- const { field, ...updateMeta } = args;
305
- if (
306
- field.type !== "Link" ||
307
- field.config?.select !== "document" ||
308
- !field.config?.customtypes
309
- ) {
310
- // not a content relationship field
311
- return field;
312
- }
313
-
314
- const newCustomTypes = this.updateCRCustomTypes({
315
- ...updateMeta,
316
- customTypes: field.config.customtypes,
317
- });
318
-
319
- return {
320
- ...field,
321
- config: { ...field.config, customtypes: newCustomTypes },
322
- };
323
- }
324
-
325
191
  /**
326
192
  * Update the Content Relationship API IDs for all existing custom types and
327
193
  * slices. The change is determined by properties inside the `updateMeta`
@@ -347,24 +213,19 @@ export class CustomTypesManager extends BaseManager {
347
213
  // any custom type and update them to use the new one.
348
214
  const customTypes = await this.readAllCustomTypes();
349
215
 
350
- for (const customType of customTypes.models) {
351
- const updatedCustomTypeModel = traverseCustomType({
352
- customType: customType.model,
353
- onField: ({ field }) => {
354
- return this.updateFieldContentRelationships({
355
- field,
356
- previousPath,
357
- newPath,
358
- });
359
- },
360
- });
361
-
362
- crUpdates.push(
363
- this.sliceMachinePluginRunner.callHook("custom-type:update", {
364
- model: updatedCustomTypeModel,
365
- }),
366
- );
367
- }
216
+ updateCustomTypeContentRelationships({
217
+ models: customTypes.models,
218
+ onUpdate: (model) => {
219
+ pushIfDefined(
220
+ crUpdates,
221
+ this.sliceMachinePluginRunner?.callHook("custom-type:update", {
222
+ model,
223
+ }),
224
+ );
225
+ },
226
+ previousPath,
227
+ newPath,
228
+ });
368
229
 
369
230
  // Find existing slice with content relationships that link to the renamed
370
231
  // field id in all libraries and update them to use the new one.
@@ -375,26 +236,20 @@ export class CustomTypesManager extends BaseManager {
375
236
  libraryID: library.libraryID,
376
237
  });
377
238
 
378
- for (const slice of slices.models) {
379
- const updatedSliceModel = traverseSharedSlice({
380
- path: ["."],
381
- slice: slice.model,
382
- onField: ({ field }) => {
383
- return this.updateFieldContentRelationships({
384
- field,
385
- previousPath,
386
- newPath,
387
- });
388
- },
389
- });
390
-
391
- crUpdates.push(
392
- this.sliceMachinePluginRunner.callHook("slice:update", {
393
- libraryID: library.libraryID,
394
- model: updatedSliceModel,
395
- }),
396
- );
397
- }
239
+ updateSharedSliceContentRelationships({
240
+ models: slices.models,
241
+ onUpdate: (model) => {
242
+ pushIfDefined(
243
+ crUpdates,
244
+ this.sliceMachinePluginRunner?.callHook("slice:update", {
245
+ libraryID: library.libraryID,
246
+ model,
247
+ }),
248
+ );
249
+ },
250
+ previousPath,
251
+ newPath,
252
+ });
398
253
  }
399
254
 
400
255
  // Process all the Content Relationship updates at once.
@@ -640,10 +495,206 @@ const InferSliceResponse = z.object({
640
495
  langSmithUrl: z.string().url().optional(),
641
496
  });
642
497
 
643
- function shallowClone<T>(value: T): T {
498
+ function updateCRCustomType(
499
+ args: { customType: CrCustomType } & CustomTypeFieldIdChangedMeta,
500
+ ): CrCustomType {
501
+ const { previousPath, newPath } = args;
502
+
503
+ const customType = shallowCloneIfObject(args.customType);
504
+
505
+ const [previousCustomTypeId, previousFieldId] = previousPath;
506
+ const [newCustomTypeId, newFieldId] = newPath;
507
+
508
+ if (!previousCustomTypeId || !newCustomTypeId) {
509
+ throw new Error(
510
+ "Didn't find any customtype id, which should not be possible.",
511
+ );
512
+ }
513
+
514
+ if (!previousFieldId || !newFieldId) {
515
+ throw new Error("Didn't find any field id, which should not be possible.");
516
+ }
517
+
518
+ if (typeof customType === "string") {
519
+ return customType; // we don't support custom type id renaming
520
+ }
521
+
522
+ const matchedCustomTypeId = customType.id === previousCustomTypeId;
523
+
524
+ if (customType.fields) {
525
+ const newFields = customType.fields.map((fieldArg) => {
526
+ const nestedField = shallowCloneIfObject(fieldArg);
527
+
528
+ if (typeof nestedField === "string") {
529
+ if (
530
+ matchedCustomTypeId &&
531
+ nestedField === previousFieldId &&
532
+ nestedField !== newFieldId
533
+ ) {
534
+ // We have reached a field id that matches the id that was renamed,
535
+ // so we update it new one. The field is a string, so return the new
536
+ // id.
537
+ return newFieldId;
538
+ }
539
+
540
+ return nestedField;
541
+ }
542
+
543
+ if (nestedField.id === previousFieldId) {
544
+ if (matchedCustomTypeId && nestedField.id !== newFieldId) {
545
+ // We have reached a field id that matches the id that was renamed,
546
+ // so we update it new one.
547
+ // Since field is not a string, we don't exit, as we might have
548
+ // something to update further down in customtypes.
549
+ nestedField.id = newFieldId;
550
+ }
551
+ }
552
+
553
+ return {
554
+ ...nestedField,
555
+ customtypes: nestedField.customtypes.map((customTypeArg) => {
556
+ const customTypeField = shallowCloneIfObject(customTypeArg);
557
+
558
+ if (typeof customTypeField === "string") {
559
+ return customTypeField; // we don't support custom type id renaming
560
+ }
561
+
562
+ const matchedNestedCustomTypeId =
563
+ customTypeField.id === previousCustomTypeId;
564
+
565
+ if (customTypeField.fields) {
566
+ return {
567
+ ...customTypeField,
568
+ fields: customTypeField.fields.map((fieldArg) => {
569
+ const nestedCustomTypeField = shallowCloneIfObject(fieldArg);
570
+
571
+ if (
572
+ matchedNestedCustomTypeId &&
573
+ nestedCustomTypeField === previousFieldId &&
574
+ nestedCustomTypeField !== newFieldId
575
+ ) {
576
+ // Matches the previous id, so we update it and return because
577
+ // it's the last level.
578
+ return newFieldId;
579
+ }
580
+
581
+ return nestedCustomTypeField;
582
+ }),
583
+ };
584
+ }
585
+
586
+ return customTypeField;
587
+ }),
588
+ };
589
+ });
590
+
591
+ return { ...customType, fields: newFields };
592
+ }
593
+
594
+ return customType;
595
+ }
596
+
597
+ /**
598
+ * Map over the custom types of a Content Relationship Link and update the API
599
+ * IDs that were changed during the custom type update.
600
+ */
601
+ function updateCRCustomTypes(
602
+ args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
603
+ ): CrCustomTypes {
604
+ const { customTypes, ...updateMeta } = args;
605
+
606
+ return customTypes.map((customType) => {
607
+ return updateCRCustomType({ customType, ...updateMeta });
608
+ });
609
+ }
610
+
611
+ /**
612
+ * Update the Content Relationship API IDs of a single field. The change is
613
+ * determined by the `previousPath` and `newPath` properties.
614
+ */
615
+ function updateFieldContentRelationships<
616
+ T extends UID | NestableWidget | Group | NestedGroup,
617
+ >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
618
+ const { field, ...updateMeta } = args;
619
+ if (
620
+ field.type !== "Link" ||
621
+ field.config?.select !== "document" ||
622
+ !field.config?.customtypes
623
+ ) {
624
+ // not a content relationship field
625
+ return field;
626
+ }
627
+
628
+ const newCustomTypes = updateCRCustomTypes({
629
+ ...updateMeta,
630
+ customTypes: field.config.customtypes,
631
+ });
632
+
633
+ return {
634
+ ...field,
635
+ config: { ...field.config, customtypes: newCustomTypes },
636
+ };
637
+ }
638
+
639
+ export function updateCustomTypeContentRelationships(
640
+ args: {
641
+ models: { model: CustomType }[];
642
+ onUpdate: (model: CustomType) => void;
643
+ } & CustomTypeFieldIdChangedMeta,
644
+ ): void {
645
+ const { models, previousPath, newPath, onUpdate } = args;
646
+
647
+ for (const customType of models) {
648
+ const updatedCustomTypeModel = traverseCustomType({
649
+ customType: customType.model,
650
+ onField: ({ field }) => {
651
+ return updateFieldContentRelationships({
652
+ field,
653
+ previousPath,
654
+ newPath,
655
+ });
656
+ },
657
+ });
658
+
659
+ onUpdate(updatedCustomTypeModel);
660
+ }
661
+ }
662
+
663
+ export function updateSharedSliceContentRelationships(
664
+ args: {
665
+ models: { model: SharedSlice }[];
666
+ onUpdate: (model: SharedSlice) => void;
667
+ } & CustomTypeFieldIdChangedMeta,
668
+ ): void {
669
+ const { models, previousPath, newPath, onUpdate } = args;
670
+
671
+ for (const slice of models) {
672
+ const updatedSliceModel = traverseSharedSlice({
673
+ path: ["."],
674
+ slice: slice.model,
675
+ onField: ({ field }) => {
676
+ return updateFieldContentRelationships({
677
+ field,
678
+ previousPath,
679
+ newPath,
680
+ });
681
+ },
682
+ });
683
+
684
+ onUpdate(updatedSliceModel);
685
+ }
686
+ }
687
+
688
+ function shallowCloneIfObject<T>(value: T): T {
644
689
  if (typeof value === "object") {
645
690
  return { ...value };
646
691
  }
647
692
 
648
693
  return value;
649
694
  }
695
+
696
+ function pushIfDefined<T>(array: T[], value: T | undefined) {
697
+ if (value) {
698
+ array.push(value);
699
+ }
700
+ }