@slicemachine/manager 0.24.14-alpha.jp-update-cr-links-remove-recursion.10 → 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,149 +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
- const matchedCustomTypeId = customType.id === previousCustomTypeId;
218
-
219
- if (customType.fields) {
220
- const newFields = customType.fields.map((fieldArg) => {
221
- const nestedField = shallowClone(fieldArg);
222
-
223
- if (typeof nestedField === "string") {
224
- if (
225
- matchedCustomTypeId &&
226
- nestedField === previousFieldId &&
227
- nestedField !== newFieldId
228
- ) {
229
- // We have reached a field id that matches the id that was renamed,
230
- // so we update it new one. The field is a string, so return the new
231
- // id.
232
- return newFieldId;
233
- }
234
-
235
- return nestedField;
236
- }
237
-
238
- if (nestedField.id === previousFieldId) {
239
- if (matchedCustomTypeId && nestedField.id !== newFieldId) {
240
- // We have reached a field id that matches the id that was renamed,
241
- // so we update it new one.
242
- // Since field is not a string, we don't exit, as we might have
243
- // something to update further down in customtypes.
244
- nestedField.id = newFieldId;
245
- }
246
- }
247
-
248
- return {
249
- ...nestedField,
250
- customtypes: nestedField.customtypes.map((customTypeArg) => {
251
- const customTypeField = shallowClone(customTypeArg);
252
-
253
- if (typeof customTypeField === "string") {
254
- return customTypeField; // we don't support custom type id renaming
255
- }
256
-
257
- const matchedNestedCustomTypeId =
258
- customTypeField.id === previousCustomTypeId;
259
-
260
- if (customTypeField.fields) {
261
- return {
262
- ...customTypeField,
263
- fields: customTypeField.fields.map((fieldArg) => {
264
- const nestedCustomTypeField = shallowClone(fieldArg);
265
-
266
- if (
267
- matchedNestedCustomTypeId &&
268
- nestedCustomTypeField === previousFieldId &&
269
- nestedCustomTypeField !== newFieldId
270
- ) {
271
- // Matches the previous id, so we update it and return because
272
- // it's the last level.
273
- return newFieldId;
274
- }
275
-
276
- return nestedCustomTypeField;
277
- }),
278
- };
279
- }
280
-
281
- return customTypeField;
282
- }),
283
- };
284
- });
285
-
286
- return { ...customType, fields: newFields };
287
- }
288
-
289
- return customType;
290
- }
291
-
292
- /**
293
- * Map over the custom types of a Content Relationship Link and update the API
294
- * IDs that were changed during the custom type update.
295
- */
296
- private updateCRCustomTypes(
297
- args: { customTypes: CrCustomTypes } & CustomTypeFieldIdChangedMeta,
298
- ): CrCustomTypes {
299
- const { customTypes, ...updateMeta } = args;
300
-
301
- return customTypes.map((customType) => {
302
- return this.updateCRCustomType({ customType, ...updateMeta });
303
- });
304
- }
305
-
306
- /**
307
- * Update the Content Relationship API IDs of a single field. The change is
308
- * determined by the `previousPath` and `newPath` properties.
309
- */
310
- private updateFieldContentRelationships<
311
- T extends UID | NestableWidget | Group | NestedGroup,
312
- >(args: { field: T } & CustomTypeFieldIdChangedMeta): T {
313
- const { field, ...updateMeta } = args;
314
- if (
315
- field.type !== "Link" ||
316
- field.config?.select !== "document" ||
317
- !field.config?.customtypes
318
- ) {
319
- // not a content relationship field
320
- return field;
321
- }
322
-
323
- const newCustomTypes = this.updateCRCustomTypes({
324
- ...updateMeta,
325
- customTypes: field.config.customtypes,
326
- });
327
-
328
- return {
329
- ...field,
330
- config: { ...field.config, customtypes: newCustomTypes },
331
- };
332
- }
333
-
334
191
  /**
335
192
  * Update the Content Relationship API IDs for all existing custom types and
336
193
  * slices. The change is determined by properties inside the `updateMeta`
@@ -356,24 +213,19 @@ export class CustomTypesManager extends BaseManager {
356
213
  // any custom type and update them to use the new one.
357
214
  const customTypes = await this.readAllCustomTypes();
358
215
 
359
- for (const customType of customTypes.models) {
360
- const updatedCustomTypeModel = traverseCustomType({
361
- customType: customType.model,
362
- onField: ({ field }) => {
363
- return this.updateFieldContentRelationships({
364
- field,
365
- previousPath,
366
- newPath,
367
- });
368
- },
369
- });
370
-
371
- crUpdates.push(
372
- this.sliceMachinePluginRunner.callHook("custom-type:update", {
373
- model: updatedCustomTypeModel,
374
- }),
375
- );
376
- }
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
+ });
377
229
 
378
230
  // Find existing slice with content relationships that link to the renamed
379
231
  // field id in all libraries and update them to use the new one.
@@ -384,26 +236,20 @@ export class CustomTypesManager extends BaseManager {
384
236
  libraryID: library.libraryID,
385
237
  });
386
238
 
387
- for (const slice of slices.models) {
388
- const updatedSliceModel = traverseSharedSlice({
389
- path: ["."],
390
- slice: slice.model,
391
- onField: ({ field }) => {
392
- return this.updateFieldContentRelationships({
393
- field,
394
- previousPath,
395
- newPath,
396
- });
397
- },
398
- });
399
-
400
- crUpdates.push(
401
- this.sliceMachinePluginRunner.callHook("slice:update", {
402
- libraryID: library.libraryID,
403
- model: updatedSliceModel,
404
- }),
405
- );
406
- }
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
+ });
407
253
  }
408
254
 
409
255
  // Process all the Content Relationship updates at once.
@@ -649,10 +495,206 @@ const InferSliceResponse = z.object({
649
495
  langSmithUrl: z.string().url().optional(),
650
496
  });
651
497
 
652
- 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 {
653
689
  if (typeof value === "object") {
654
690
  return { ...value };
655
691
  }
656
692
 
657
693
  return value;
658
694
  }
695
+
696
+ function pushIfDefined<T>(array: T[], value: T | undefined) {
697
+ if (value) {
698
+ array.push(value);
699
+ }
700
+ }