@localazy/strapi-plugin 0.1.14 → 0.1.15

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@localazy/strapi-plugin",
3
- "version": "0.1.14",
3
+ "version": "0.1.15",
4
4
  "description": "The official Strapi Plugin by Localazy.",
5
5
  "strapi": {
6
6
  "name": "localazy",
@@ -6,7 +6,7 @@ const {
6
6
  findSetupModelByCollectionName,
7
7
  findSetupModelByCollectionUid,
8
8
  isCollectionTransferEnabled,
9
- getPickPaths,
9
+ getPickPathsWithComponents,
10
10
  } = require("../utils/transfer-setup-utils");
11
11
  const pickEntries = require("../utils/pick-entries");
12
12
  const config = require("../config").default;
@@ -113,7 +113,7 @@ module.exports = {
113
113
 
114
114
  // get only enabled fields paths
115
115
  const currentTransferSetupModel = transferSetupModel[collectionName];
116
- const pickPaths = getPickPaths(currentTransferSetupModel);
116
+ const pickPaths = getPickPathsWithComponents(currentTransferSetupModel);
117
117
  if (!pickPaths.length) {
118
118
  const message = `No fields for collection ${collectionName} transfer are enabled.`;
119
119
  messageReport.push(message);
@@ -152,7 +152,7 @@ module.exports = {
152
152
  [modelUid]: entry,
153
153
  });
154
154
  // get only enabled fields; "__component" will be filtered out inside of the function
155
- const pickedFlatten = pickEntries(flatten, pickPathsWithUid, currentTransferSetupModel);
155
+ const pickedFlatten = pickEntries(flatten, pickPathsWithUid);
156
156
 
157
157
  flattenContent = {
158
158
  ...flattenContent,
@@ -893,19 +893,19 @@ describe("pick entries test suite", () => {
893
893
  "api::lesson.lesson[1].start_time": "2022-09-28T09:00:00.000Z",
894
894
  "api::lesson.lesson[1].duration": "03:00:00.000",
895
895
  "api::lesson.lesson[1].description": "## Description\n\nIn this lesson, we'll learn about addition and what rules to follow...",
896
- "api::lesson.lesson[1].lesson_type[1].__component": "lesson.lecture",
897
- "api::lesson.lesson[1].lesson_type[1].content": "You need to follow these rules:\na) add one number to the other\nb) do not subtract nor multiply\nc) ...",
896
+ "api::lesson.lesson[1].lesson_type[1;lesson.lecture].__component": "lesson.lecture",
897
+ "api::lesson.lesson[1].lesson_type[1;lesson.lecture].content": "You need to follow these rules:\na) add one number to the other\nb) do not subtract nor multiply\nc) ...",
898
898
  };
899
899
 
900
900
  const pickPaths = [
901
901
  "api::lesson.lesson.title",
902
902
  "api::lesson.lesson.description",
903
- "api::lesson.lesson.lesson_type[0].content",
904
- "api::lesson.lesson.lesson_type[1].description",
905
- "api::lesson.lesson.lesson_type[1].questions.question",
906
- "api::lesson.lesson.lesson_type[2].title",
907
- "api::lesson.lesson.lesson_type[2].questions.question",
908
- "api::lesson.lesson.lesson_type[2].answers.correct_answer",
903
+ "api::lesson.lesson.lesson_type[lecture].content",
904
+ "api::lesson.lesson.lesson_type[survey].description",
905
+ "api::lesson.lesson.lesson_type[survey].questions.question",
906
+ "api::lesson.lesson.lesson_type[quiz].title",
907
+ "api::lesson.lesson.lesson_type[quiz].questions.question",
908
+ "api::lesson.lesson.lesson_type[quiz].answers.correct_answer",
909
909
  "api::lesson.lesson.id",
910
910
  ];
911
911
 
@@ -22,8 +22,13 @@ const flattenObject = (object, prefix = "") => {
22
22
  const flattenedArray = object[objectKey].reduce(
23
23
  (accumulator, item, index) => {
24
24
  // No id should use the index of the array item
25
- accumulator[`${objectKey}[${item.id || index}]`] =
26
- flattenObject(item);
25
+ let key = `${objectKey}[${item.id || index}]`;
26
+ // is Dynamic Zone
27
+ if (item.id && item.__component) {
28
+ key = `${objectKey}[${item.id};${item.__component}]`;
29
+ }
30
+
31
+ accumulator[key] = flattenObject(item);
27
32
  return accumulator;
28
33
  },
29
34
  {}
@@ -1,93 +1,12 @@
1
1
  "use strict";
2
2
 
3
- const map = require("lodash/map");
4
- const pickDeep = require("./pick-deep");
5
- const flattenObject = require("./flatten-object");
6
-
7
- const isDynamicZoneKey = (key, transferSetupModel) => {
8
- const matches = key.match(/\[\d+\]+/g);
9
-
10
- if (matches.length < 2) {
11
- return false;
12
- }
13
-
14
- const dynamicPropertyEntryIdWithSquareBrackets = matches[1];
15
- const dynamicPropertyNamePart = key
16
- .split(".")
17
- .find(
18
- (s, index) =>
19
- index > 1 && s.includes(dynamicPropertyEntryIdWithSquareBrackets)
20
- );
21
- const dynamicPropertyName = dynamicPropertyNamePart.replace(
22
- dynamicPropertyEntryIdWithSquareBrackets,
23
- ""
24
- );
25
-
26
- return Array.isArray(transferSetupModel[dynamicPropertyName]);
27
- };
28
-
29
- const getDynamicZoneEntryId = (key, transferSetupModel) => {
30
- if (!isDynamicZoneKey(key, transferSetupModel)) {
31
- return undefined;
32
- }
33
-
34
- const matches = key.match(/\[\d+\]+/g);
35
- // remove square brackets
36
- const id = matches[1].replace(/[\[\]']+/g, "");
37
- return id;
38
- };
39
-
40
- const getUniqueDynamicZoneEntryId = (key, transferSetupModel) => {
41
- if (!isDynamicZoneKey(key, transferSetupModel)) {
42
- return undefined;
43
- }
44
-
45
- const keySplittedByDot = key.split(".");
46
-
47
- return keySplittedByDot[2];
48
- };
49
-
50
- const doesPickPathsIncludeTheComponent = (pickPaths, dzParameterKey) => {
51
- return pickPaths.some((pp) => pp.includes(dzParameterKey));
3
+ const isDynamicZoneKey = (key) => {
4
+ const matches = key.match(/\[\d+;\w+\./g);
5
+ return matches !== null;
52
6
  };
53
7
 
54
- const pickEntries = (flatten, pickPaths, transferSetupModel) => {
55
- // * dynamic zones properties must! always be in 1st level (not in component; Strapi restrictions)
56
-
57
- const dzEntryIdComponentMap = {};
58
- const flattenComponentProps = Object.fromEntries(
59
- Object.entries(flatten).filter(([key]) => key.includes("__component"))
60
- );
61
-
62
- const pickedDeep = pickDeep(transferSetupModel, ["__component__"]);
63
- const flattenedPickedDeep = flattenObject(pickedDeep);
64
- map(flattenedPickedDeep, (v, i) => {
65
- const newI = i.replace(".__component__", "");
66
- flattenedPickedDeep[newI] = v;
67
- delete flattenedPickedDeep[i];
68
- });
69
-
70
- Object.keys(flattenComponentProps).map((key) => {
71
- if (!isDynamicZoneKey(key, transferSetupModel)) {
72
- return;
73
- }
74
-
75
- const keySplittedByDot = key.replace(".__component", "").split(".");
76
- const componentName = keySplittedByDot[keySplittedByDot.length - 1].replace(
77
- /\[.*\]/g,
78
- ""
79
- );
80
- const uniqueEntryId = getUniqueDynamicZoneEntryId(key, transferSetupModel);
81
-
82
- const keyOfEntry = Object.keys(flattenedPickedDeep)
83
- .filter((fpdk) => fpdk.replace(/\[.*\]/g, "") == componentName)
84
- .find((fpdk) => flattenedPickedDeep[fpdk] === flattenComponentProps[key]);
85
- dzEntryIdComponentMap[uniqueEntryId] = {
86
- component: flattenComponentProps[key],
87
- key: keyOfEntry,
88
- };
89
- });
90
-
8
+ const pickEntries = (flatten, pickPaths) => {
9
+ // * Dynamic zones properties must! always be in 1st level (not in component; Strapi restrictions)
91
10
  const pickedEntries = {};
92
11
  const mappedPickPaths = pickPaths.map((pickPath) =>
93
12
  pickPath.replace(/\[\d+\]/g, "")
@@ -98,44 +17,13 @@ const pickEntries = (flatten, pickPaths, transferSetupModel) => {
98
17
  Object.entries(flatten).filter(([key]) => !key.includes("__component"))
99
18
  );
100
19
  Object.keys(filteredFlatten).forEach((key) => {
101
- // decide whether it is a dynamic zone
102
- // handle dynamic zones, in case of similarly-named fields across more components
103
- const isDynamicZone = isDynamicZoneKey(key, transferSetupModel);
104
- if (isDynamicZone) {
105
- const uniqueDzEntryId = getUniqueDynamicZoneEntryId(
106
- key,
107
- transferSetupModel
108
- );
109
- const dzParameterKey = dzEntryIdComponentMap[uniqueDzEntryId].key;
110
-
111
- if (!doesPickPathsIncludeTheComponent(pickPaths, dzParameterKey)) {
112
- return;
113
- }
20
+ let filteredKey = key;
21
+ filteredKey = filteredKey.replace(/\[\d+\]/g, "");
22
+ if (isDynamicZoneKey(key)) {
23
+ filteredKey = filteredKey.replace(/\[\d+;\w+\./g, `[`);
114
24
  }
115
-
116
- const filteredKey = key.replace(/\[\d+\]/g, "");
117
25
  if (mappedPickPaths.includes(filteredKey)) {
118
- if (isDynamicZone) {
119
- const dzEntryId = getDynamicZoneEntryId(key, transferSetupModel);
120
- const uniqueDzEntryId = getUniqueDynamicZoneEntryId(
121
- key,
122
- transferSetupModel
123
- );
124
- const dzParameterKey = dzEntryIdComponentMap[uniqueDzEntryId].key;
125
-
126
- const dzParameterName = dzParameterKey.substring(
127
- 0,
128
- dzParameterKey.indexOf("[")
129
- );
130
- const toBeReplaced = `${dzParameterName}[${dzEntryId}]`;
131
- const dzParameterComponent =
132
- dzEntryIdComponentMap[uniqueDzEntryId].component;
133
- const componentAddition = `${dzParameterName}[${dzEntryId};${dzParameterComponent}]`;
134
- const replacedKey = key.replace(toBeReplaced, componentAddition);
135
- pickedEntries[replacedKey] = flatten[key];
136
- } else {
137
- pickedEntries[key] = flatten[key];
138
- }
26
+ pickedEntries[key] = flatten[key];
139
27
  }
140
28
  });
141
29
  return pickedEntries;
@@ -49,10 +49,44 @@ const getPickPaths = (transferSetupModelProps) => {
49
49
  return pickPaths;
50
50
  };
51
51
 
52
+ const getPickPathsWithComponents = (transferSetupModelProps) => {
53
+ const flattenedObject = flattenObject(transferSetupModelProps);
54
+ let pickComponents = {};
55
+ Object.keys(flattenedObject).forEach((key) => {
56
+ if (key.includes(".__component__")) {
57
+ const componentKey = key.split(".__component__")[0];
58
+ if (!pickComponents[componentKey]) {
59
+ const splitByDot = flattenedObject[key].split(".");
60
+ pickComponents[key.split(".__component__")[0]] = `${splitByDot[0]}[${splitByDot[1]}]`;
61
+ }
62
+ }
63
+ });
64
+ const pickPaths = Object.keys(flattenedObject)
65
+ .filter(
66
+ (key) => flattenedObject[key] === true && key.indexOf("__model__") === -1
67
+ );
68
+
69
+ // id needs to be always included
70
+ pickPaths.push("id");
71
+
72
+ let pickPathsWithComponents = [];
73
+ Object.entries(pickPaths).forEach(([, value]) => {
74
+ const componentKey = value.split(".")[0];
75
+ let pushValue = value;
76
+ if (pickComponents[componentKey]) {
77
+ pushValue = value.replace(componentKey, pickComponents[componentKey]);
78
+ }
79
+ pickPathsWithComponents.push(pushValue);
80
+ });
81
+
82
+ return pickPathsWithComponents;
83
+ };
84
+
52
85
  module.exports = {
53
86
  findSetupModelByCollectionName,
54
87
  findSetupModelByCollectionUid,
55
88
  getCollectionsNames,
56
89
  isCollectionTransferEnabled,
57
90
  getPickPaths,
91
+ getPickPathsWithComponents,
58
92
  };