@localazy/strapi-plugin 0.1.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.
Files changed (150) hide show
  1. package/.babelrc +7 -0
  2. package/.eslintrc +7 -0
  3. package/.github/workflows/.build.yml +39 -0
  4. package/.nvmrc +1 -0
  5. package/LICENSE +21 -0
  6. package/README.md +40 -0
  7. package/admin/src/.eslintrc +3 -0
  8. package/admin/src/components/Initializer/index.js +26 -0
  9. package/admin/src/components/PluginIcon/index.js +14 -0
  10. package/admin/src/i18n.js +37 -0
  11. package/admin/src/index.js +106 -0
  12. package/admin/src/modules/@common/components/Icons/Localazy/index.js +12 -0
  13. package/admin/src/modules/@common/components/SideNav/index.js +46 -0
  14. package/admin/src/modules/@common/components/TransferReport/index.js +54 -0
  15. package/admin/src/modules/@common/locale/en.js +13 -0
  16. package/admin/src/modules/@common/services/i18n-service.js +15 -0
  17. package/admin/src/modules/@common/services/project-service.js +15 -0
  18. package/admin/src/modules/@common/services/user-service.js +38 -0
  19. package/admin/src/modules/@common/utils/are-locales-compatible.js +42 -0
  20. package/admin/src/modules/@common/utils/functions/flatten-deep.js +23 -0
  21. package/admin/src/modules/@common/utils/get-default-locale.js +32 -0
  22. package/admin/src/modules/@common/utils/get-header-subtitle.js +21 -0
  23. package/admin/src/modules/@common/utils/get-header-title.js +24 -0
  24. package/admin/src/modules/@common/utils/get-nav.js +35 -0
  25. package/admin/src/modules/@common/utils/history.js +6 -0
  26. package/admin/src/modules/@common/utils/is-plugin-route.js +15 -0
  27. package/admin/src/modules/@common/utils/iso-locales-utils.js +15 -0
  28. package/admin/src/modules/@common/utils/redirect-to-plugin-route.js +22 -0
  29. package/admin/src/modules/@common/utils/sort-by-model-name.js +10 -0
  30. package/admin/src/modules/localazy-download/locale/en.js +20 -0
  31. package/admin/src/modules/localazy-download/services/localazy-download-service.js +16 -0
  32. package/admin/src/modules/localazy-upload/components/ReadDocsButton.js +21 -0
  33. package/admin/src/modules/localazy-upload/locale/en.js +18 -0
  34. package/admin/src/modules/localazy-upload/services/localazy-upload-service.js +16 -0
  35. package/admin/src/modules/login/components/LoginButton/index.js +56 -0
  36. package/admin/src/modules/login/components/LogoutButton/index.js +50 -0
  37. package/admin/src/modules/login/locale/en.js +7 -0
  38. package/admin/src/modules/login/services/localazy-login-service.js +29 -0
  39. package/admin/src/modules/plugin-settings/components/ContentTransferSetup/index.js +292 -0
  40. package/admin/src/modules/plugin-settings/components/Tree/index.js +120 -0
  41. package/admin/src/modules/plugin-settings/components/TreeItem/index.js +48 -0
  42. package/admin/src/modules/plugin-settings/functions/build-content-transfer-setup-schema.js +71 -0
  43. package/admin/src/modules/plugin-settings/functions/has-model-changed.js +85 -0
  44. package/admin/src/modules/plugin-settings/locale/en.js +19 -0
  45. package/admin/src/modules/plugin-settings/services/plugin-settings-service.js +32 -0
  46. package/admin/src/modules/plugin-settings/services/strapi-model-service.js +26 -0
  47. package/admin/src/modules/plugin-settings/utils/__tests__/get-models-tree.test.js +135 -0
  48. package/admin/src/modules/plugin-settings/utils/array-of-models-to-object.js +6 -0
  49. package/admin/src/modules/plugin-settings/utils/find-model-value-by-key.js +8 -0
  50. package/admin/src/modules/plugin-settings/utils/get-filtered-models-schemas.js +34 -0
  51. package/admin/src/modules/plugin-settings/utils/get-models-tree.js +73 -0
  52. package/admin/src/modules/settings/components/OverviewItem/index.js +44 -0
  53. package/admin/src/modules/settings/components/OverviewItemLink/index.js +34 -0
  54. package/admin/src/modules/settings/components/PrerequisitiesInfo/index.js +30 -0
  55. package/admin/src/modules/settings/locale/en.js +13 -0
  56. package/admin/src/modules/user/model/localazy-identity.js +14 -0
  57. package/admin/src/modules/user/services/localazy-user-service.js +38 -0
  58. package/admin/src/pages/App/index.js +152 -0
  59. package/admin/src/pages/Download/index.js +179 -0
  60. package/admin/src/pages/Index/index.js +8 -0
  61. package/admin/src/pages/Login/index.js +94 -0
  62. package/admin/src/pages/PluginSettings/index.js +42 -0
  63. package/admin/src/pages/Settings/index.js +120 -0
  64. package/admin/src/pages/Upload/index.js +187 -0
  65. package/admin/src/pluginId.js +5 -0
  66. package/admin/src/plugins/apolloClient.js +33 -0
  67. package/admin/src/state/localazy-identity.js +29 -0
  68. package/admin/src/tests/fixtures/all-models.js +877 -0
  69. package/admin/src/tests/fixtures/restaurant-model.js +230 -0
  70. package/admin/src/translations/en.json +4 -0
  71. package/admin/src/translations/fr.json +1 -0
  72. package/admin/src/utils/createAxiosInstance.js +52 -0
  73. package/admin/src/utils/getTrad.js +5 -0
  74. package/jest.config.js +0 -0
  75. package/package.json +60 -0
  76. package/server/bootstrap.js +5 -0
  77. package/server/config/development/index.js +15 -0
  78. package/server/config/example/index.js +15 -0
  79. package/server/config/index.js +15 -0
  80. package/server/config/production/index.js +16 -0
  81. package/server/config/schema.graphql.js +21 -0
  82. package/server/content-types/index.js +3 -0
  83. package/server/controllers/index.js +19 -0
  84. package/server/controllers/localazy-auth-controller.js +46 -0
  85. package/server/controllers/localazy-project-controller.js +21 -0
  86. package/server/controllers/localazy-transfer-controller.js +469 -0
  87. package/server/controllers/localazy-user-controller.js +24 -0
  88. package/server/controllers/my-controller.js +10 -0
  89. package/server/controllers/plugin-settings-controller.js +17 -0
  90. package/server/controllers/strapi-controller.js +16 -0
  91. package/server/db/model/content-transfer-setup.js +13 -0
  92. package/server/db/model/localazy-user.js +23 -0
  93. package/server/db/model/utils/get-strapi-store.js +9 -0
  94. package/server/destroy.js +5 -0
  95. package/server/index.js +25 -0
  96. package/server/middlewares/index.js +3 -0
  97. package/server/policies/index.js +3 -0
  98. package/server/register.js +5 -0
  99. package/server/routes/index.js +26 -0
  100. package/server/routes/localazy-auth.js +22 -0
  101. package/server/routes/localazy-project.js +14 -0
  102. package/server/routes/localazy-transfer.js +22 -0
  103. package/server/routes/localazy-user.js +30 -0
  104. package/server/routes/plugin-settings.js +22 -0
  105. package/server/routes/strapi.js +22 -0
  106. package/server/services/index.js +21 -0
  107. package/server/services/localazy-auth-service.js +38 -0
  108. package/server/services/localazy-download-service.js +54 -0
  109. package/server/services/localazy-pubapi-service.js +47 -0
  110. package/server/services/localazy-upload-service.js +95 -0
  111. package/server/services/localazy-user-service.js +29 -0
  112. package/server/services/plugin-settings-service.js +30 -0
  113. package/server/services/strapi-i18n-service.js +132 -0
  114. package/server/services/strapi-service.js +23 -0
  115. package/server/tests/fixtures/all-models-with-multimedia.js +1064 -0
  116. package/server/tests/fixtures/all-models.js +869 -0
  117. package/server/tests/fixtures/collection-flattened.js +76 -0
  118. package/server/tests/fixtures/restaurant-flattened.js +46 -0
  119. package/server/tests/fixtures/restaurant-model.js +230 -0
  120. package/server/tests/fixtures/restaurants-entries.js +152 -0
  121. package/server/tests/fixtures/restaurants-pick-paths.js +15 -0
  122. package/server/tests/fixtures/strapi-book-with-multimedia-components-cs.js +31 -0
  123. package/server/tests/fixtures/stringified-book-cs.js +3 -0
  124. package/server/tests/fixtures/stringified-book-with-multimedia-components-cs.js +3 -0
  125. package/server/tests/fixtures/stringified-restaurant-cs.js +3 -0
  126. package/server/tests/fixtures/stringified-restaurant-de.js +5 -0
  127. package/server/utils/__tests__/build-populate.test.js +85 -0
  128. package/server/utils/__tests__/flatten-object.test.js +171 -0
  129. package/server/utils/__tests__/get-models-tree.test.js +170 -0
  130. package/server/utils/__tests__/parsed-localazy-entry-to-create-entry.test.js +153 -0
  131. package/server/utils/__tests__/parsed-localazy-entry-to-update-entry.test.js +172 -0
  132. package/server/utils/__tests__/pick-entries.test.js +83 -0
  133. package/server/utils/build-populate.js +43 -0
  134. package/server/utils/delay.js +6 -0
  135. package/server/utils/flatten-object.js +52 -0
  136. package/server/utils/get-localazy-api.js +22 -0
  137. package/server/utils/get-main-keys-of-nested-array-objects.js +5 -0
  138. package/server/utils/get-models-tree.js +43 -0
  139. package/server/utils/intl-display-name.js +7 -0
  140. package/server/utils/is-generally-applicable-content-type.js +33 -0
  141. package/server/utils/is-localazy-applicable-content-type.js +36 -0
  142. package/server/utils/iso-locales-utils.js +22 -0
  143. package/server/utils/objectivise-entry.js +41 -0
  144. package/server/utils/omit-deep.js +17 -0
  145. package/server/utils/parsed-localazy-entry-to-create-entry.js +115 -0
  146. package/server/utils/parsed-localazy-entry-to-update-entry.js +31 -0
  147. package/server/utils/pick-entries.js +14 -0
  148. package/server/utils/transfer-setup-utils.js +39 -0
  149. package/strapi-admin.js +14 -0
  150. package/strapi-server.js +5 -0
@@ -0,0 +1,14 @@
1
+ "use strict";
2
+
3
+ const ROUTE_PREFIX = "/project";
4
+
5
+ module.exports = [
6
+ {
7
+ method: "GET",
8
+ path: `${ROUTE_PREFIX}`,
9
+ handler: "localazyProjectController.getConnectedProject",
10
+ config: {
11
+ policies: [],
12
+ },
13
+ },
14
+ ];
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ const ROUTE_PREFIX = "/transfer";
4
+
5
+ module.exports = [
6
+ {
7
+ method: "POST",
8
+ path: `${ROUTE_PREFIX}/upload`,
9
+ handler: "localazyTransferController.upload",
10
+ config: {
11
+ policies: [],
12
+ },
13
+ },
14
+ {
15
+ method: "POST",
16
+ path: `${ROUTE_PREFIX}/download`,
17
+ handler: "localazyTransferController.download",
18
+ config: {
19
+ policies: [],
20
+ },
21
+ },
22
+ ];
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ const ROUTE_PREFIX = "/user";
4
+
5
+ module.exports = [
6
+ {
7
+ method: "GET",
8
+ path: `${ROUTE_PREFIX}`,
9
+ handler: "localazyUserController.getUser",
10
+ config: {
11
+ policies: [],
12
+ },
13
+ },
14
+ {
15
+ method: "PUT",
16
+ path: `${ROUTE_PREFIX}`,
17
+ handler: "localazyUserController.updateUser",
18
+ config: {
19
+ policies: [],
20
+ },
21
+ },
22
+ {
23
+ method: "DELETE",
24
+ path: `${ROUTE_PREFIX}`,
25
+ handler: "localazyUserController.deleteUser",
26
+ config: {
27
+ policies: [],
28
+ },
29
+ },
30
+ ];
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ const ROUTE_PREFIX = "/plugin-settings";
4
+
5
+ module.exports = [
6
+ {
7
+ method: "GET",
8
+ path: `${ROUTE_PREFIX}/content-transfer-setup`,
9
+ handler: "pluginSettingsController.getContentTransferSetup",
10
+ config: {
11
+ policies: [],
12
+ },
13
+ },
14
+ {
15
+ method: "PUT",
16
+ path: `${ROUTE_PREFIX}/content-transfer-setup`,
17
+ handler: "pluginSettingsController.updateContentTransferSetup",
18
+ config: {
19
+ policies: [],
20
+ },
21
+ },
22
+ ];
@@ -0,0 +1,22 @@
1
+ "use strict";
2
+
3
+ const ROUTE_PREFIX = "/strapi";
4
+
5
+ module.exports = [
6
+ {
7
+ method: "GET",
8
+ path: `${ROUTE_PREFIX}/models`,
9
+ handler: "strapiController.getModels",
10
+ config: {
11
+ policies: [],
12
+ },
13
+ },
14
+ {
15
+ method: "GET",
16
+ path: `${ROUTE_PREFIX}/localizable-models`,
17
+ handler: "strapiController.getLocalizableModels",
18
+ config: {
19
+ policies: [],
20
+ },
21
+ },
22
+ ];
@@ -0,0 +1,21 @@
1
+ "use strict";
2
+
3
+ const localazyUserService = require("./localazy-user-service");
4
+ const localazyAuthService = require("./localazy-auth-service");
5
+ const strapiService = require("./strapi-service");
6
+ const pluginSettingsService = require("./plugin-settings-service");
7
+ const localazyUploadService = require("./localazy-upload-service");
8
+ const localazyDownloadService = require("./localazy-download-service");
9
+ const localazyPubApiService = require("./localazy-pubapi-service");
10
+ const strapiI18nService = require("./strapi-i18n-service");
11
+
12
+ module.exports = {
13
+ localazyUserService,
14
+ localazyAuthService,
15
+ strapiService,
16
+ pluginSettingsService,
17
+ localazyUploadService,
18
+ localazyDownloadService,
19
+ localazyPubApiService,
20
+ strapiI18nService,
21
+ };
@@ -0,0 +1,38 @@
1
+ "use strict";
2
+
3
+ const axios = require("axios");
4
+ const config = require("../config");
5
+
6
+ const ROOT_URL = config.default.LOCALAZY_PLUGIN_CONNECTOR_API_URL;
7
+ const PLUGIN_ID = config.default.LOCALAZY_PLUGIN_ID;
8
+
9
+ module.exports = (/* { strapi } */) => ({
10
+ async generateKeys() {
11
+ try {
12
+ const result = await axios.get(`${ROOT_URL}/public/keys`, {
13
+ headers: {
14
+ "X-Localazy-Plugin-Id": PLUGIN_ID,
15
+ },
16
+ });
17
+ return result.data;
18
+ } catch (e) {
19
+ throw e.data;
20
+ }
21
+ },
22
+
23
+ async completeLogin(readKey) {
24
+ try {
25
+ const result = await axios.get(
26
+ `${ROOT_URL}/oauth/poll?readKey=${readKey}`,
27
+ {
28
+ headers: {
29
+ "X-Localazy-Plugin-Id": PLUGIN_ID,
30
+ },
31
+ }
32
+ );
33
+ return result.data;
34
+ } catch (e) {
35
+ throw e.data;
36
+ }
37
+ },
38
+ });
@@ -0,0 +1,54 @@
1
+ "use strict";
2
+
3
+ const getLocalazyApi = require("../utils/get-localazy-api");
4
+ const delay = require("../utils/delay");
5
+
6
+ const fetchChunkResult = async (projectId, fileId, lang, next) => {
7
+ try {
8
+ const LocalazyApi = await getLocalazyApi();
9
+ const chunkResult = await LocalazyApi.listKeysInFileForLanguage({
10
+ projectId,
11
+ fileId,
12
+ lang,
13
+ next,
14
+ });
15
+
16
+ return chunkResult;
17
+ } catch (e) {
18
+ strapi.log.error(e);
19
+ return {
20
+ keys: [],
21
+ };
22
+ }
23
+ };
24
+
25
+ module.exports = ({ strapi }) => ({
26
+ async download(config = {}) {
27
+ try {
28
+ const data = [];
29
+ let chunkResult;
30
+ do {
31
+ const next = (chunkResult && chunkResult.next) || "";
32
+ chunkResult = await fetchChunkResult(
33
+ config.projectId,
34
+ config.fileId,
35
+ config.lang,
36
+ next
37
+ );
38
+ await delay();
39
+ data.push(chunkResult);
40
+ } while (chunkResult.next);
41
+
42
+ return {
43
+ success: true,
44
+ data: data.map((chunk) => chunk.keys).flat(),
45
+ };
46
+ } catch (e) {
47
+ strapi.log.error(e);
48
+ return {
49
+ success: false,
50
+ message: e.message,
51
+ };
52
+ }
53
+ },
54
+ });
@@ -0,0 +1,47 @@
1
+ "use strict";
2
+
3
+ const getLocalazyApi = require("../utils/get-localazy-api");
4
+ const config = require("../config").default;
5
+
6
+ module.exports = ({ strapi }) => ({
7
+ async listFiles(projectId) {
8
+ try {
9
+ const LocalazyApi = await getLocalazyApi();
10
+ const result = await LocalazyApi.listFiles({ projectId });
11
+
12
+ return result;
13
+ } catch (e) {
14
+ strapi.console.error(e);
15
+ return [];
16
+ }
17
+ },
18
+ async getStrapiFile(projectId) {
19
+ const files = await this.listFiles(projectId);
20
+ const strapiFile = files.find(
21
+ (file) =>
22
+ file.type === config.LOCALAZY_DEFAULT_FILE_EXTENSION &&
23
+ file.name === config.LOCALAZY_DEFAULT_FILE_NAME
24
+ );
25
+
26
+ return strapiFile;
27
+ },
28
+ async listProjects(addOrganization = true, addLanguages = true) {
29
+ try {
30
+ const LocalazyApi = await getLocalazyApi();
31
+ const result = await LocalazyApi.listProjects({
32
+ organization: addOrganization,
33
+ languages: addLanguages,
34
+ });
35
+
36
+ return result;
37
+ } catch (e) {
38
+ strapi.console.error(e);
39
+ return [];
40
+ }
41
+ },
42
+ async getProject(projectId, addOrganization = true, addLanguages = true) {
43
+ const projects = await this.listProjects(addOrganization, addLanguages);
44
+
45
+ return projects.find((project) => project.id === projectId);
46
+ },
47
+ });
@@ -0,0 +1,95 @@
1
+ /* eslint-disable no-await-in-loop */
2
+
3
+ "use strict";
4
+
5
+ const getLocalazyApi = require("../utils/get-localazy-api");
6
+ const delay = require("../utils/delay");
7
+
8
+ module.exports = ({ strapi }) => ({
9
+ /**
10
+ * Upload data to Localazy.
11
+ * Use config to adjust the upload process.
12
+ * Returns status and id of a last chunk
13
+ */
14
+ async upload(files, config = {}) {
15
+ let ret = {
16
+ success: false,
17
+ message: "No data was uploaded",
18
+ };
19
+ try {
20
+ for (const file of files) {
21
+ const LocalazyApi = await getLocalazyApi();
22
+ const user = await strapi
23
+ .plugin("localazy")
24
+ .service("localazyUserService")
25
+ .getUser();
26
+
27
+ const result = await LocalazyApi.import({
28
+ projectId: user.project.id,
29
+ files: file,
30
+ ...config,
31
+ });
32
+ await delay();
33
+ ret = {
34
+ success: true,
35
+ result: result.result,
36
+ };
37
+ }
38
+ return ret;
39
+ } catch (e) {
40
+ strapi.log.error(e);
41
+ return {
42
+ success: false,
43
+ message: e.message,
44
+ };
45
+ }
46
+ },
47
+
48
+ splitToChunks(data, CHUNK_LIMIT = 1000) {
49
+ const chunks = [];
50
+ const keys = Object.keys(data);
51
+ const keysCount = keys.length;
52
+ const chunksCount = Math.ceil(keysCount / CHUNK_LIMIT);
53
+ for (let i = 0; i < chunksCount; i += 1) {
54
+ const chunkStrings = {};
55
+ const from = CHUNK_LIMIT * i;
56
+ const to = CHUNK_LIMIT * (i + 1);
57
+
58
+ const currentKeys = keys.slice(from, to);
59
+ currentKeys.forEach((key) => {
60
+ chunkStrings[key] = data[key];
61
+ });
62
+ chunks.push(chunkStrings);
63
+ }
64
+
65
+ return chunks;
66
+ },
67
+
68
+ createImportFileRepresentation(
69
+ filename,
70
+ path,
71
+ type,
72
+ sourceLang,
73
+ stringsChunks
74
+ ) {
75
+ const files = [];
76
+
77
+ for (const strings of stringsChunks) {
78
+ const file = [
79
+ {
80
+ name: filename,
81
+ path,
82
+ content: {
83
+ type,
84
+ [sourceLang]: {
85
+ ...strings,
86
+ },
87
+ },
88
+ },
89
+ ];
90
+ files.push(file);
91
+ }
92
+
93
+ return files;
94
+ },
95
+ });
@@ -0,0 +1,29 @@
1
+ "use strict";
2
+
3
+ const { KEY, emptyIdentity } = require("../db/model/localazy-user");
4
+ const getStrapiStore = require("../db/model/utils/get-strapi-store");
5
+
6
+ module.exports = ({ strapi }) => ({
7
+ async getUser() {
8
+ const pluginStore = getStrapiStore(strapi);
9
+ const user = await pluginStore.get({ key: KEY });
10
+
11
+ return user;
12
+ },
13
+
14
+ async updateUser(identity) {
15
+ const pluginStore = getStrapiStore(strapi);
16
+ await pluginStore.set({
17
+ key: KEY,
18
+ value: identity,
19
+ });
20
+
21
+ return identity;
22
+ },
23
+
24
+ async deleteUser() {
25
+ await this.updateUser(emptyIdentity);
26
+
27
+ return emptyIdentity;
28
+ },
29
+ });
@@ -0,0 +1,30 @@
1
+ "use strict";
2
+
3
+ const {
4
+ KEY,
5
+ emptyContentTransferSetup,
6
+ } = require("../db/model/content-transfer-setup");
7
+ const getStrapiStore = require("../db/model/utils/get-strapi-store");
8
+
9
+ module.exports = ({ strapi }) => ({
10
+ async getContentTransferSetup() {
11
+ const pluginStore = getStrapiStore(strapi);
12
+ const setup = await pluginStore.get({ key: KEY });
13
+
14
+ return setup || emptyContentTransferSetup;
15
+ },
16
+
17
+ async updateContentTransferSetup(setup) {
18
+ const newSetup = {
19
+ has_setup: true,
20
+ setup,
21
+ };
22
+ const pluginStore = getStrapiStore(strapi);
23
+ await pluginStore.set({
24
+ key: KEY,
25
+ value: newSetup,
26
+ });
27
+
28
+ return newSetup;
29
+ },
30
+ });
@@ -0,0 +1,132 @@
1
+ "use strict";
2
+
3
+ const { isoLocalazyToStrapi } = require("../utils/iso-locales-utils.js");
4
+ const intlDisplayName = require("../utils/intl-display-name.js");
5
+ const merge = require("lodash/merge");
6
+ const omitDeep = require("../utils/omit-deep.js");
7
+
8
+ module.exports = ({ strapi }) => ({
9
+ async getLocales(ctx) {
10
+ await strapi.controller("plugin::i18n.locales").listLocales(ctx);
11
+ return ctx.body;
12
+ },
13
+ async createStrapiLocale(ctx, isoLocalazy) {
14
+ try {
15
+ const isoStrapi = isoLocalazyToStrapi(isoLocalazy);
16
+ const localeName = intlDisplayName(isoLocalazy);
17
+
18
+ if (!localeName || !isoStrapi) {
19
+ throw new Error("Invalid locale");
20
+ }
21
+
22
+ const newLocaleCtx = { ...ctx };
23
+ newLocaleCtx.request.body = {
24
+ isDefault: false,
25
+ code: isoStrapi,
26
+ name: localeName,
27
+ };
28
+
29
+ await strapi
30
+ .controller("plugin::i18n.locales")
31
+ .createLocale(newLocaleCtx);
32
+
33
+ return isoStrapi;
34
+ } catch (e) {
35
+ strapi.log.error(e);
36
+ throw e;
37
+ }
38
+ },
39
+ parseLocalazyKey(key) {
40
+ const split = key.split(/\.|\[|\]/);
41
+ const filteredSplit = split.filter((item) => item !== "");
42
+ // return split;
43
+
44
+ return {
45
+ uid: `${filteredSplit[0]}.${filteredSplit[1]}`,
46
+ id: filteredSplit[2],
47
+ rest: filteredSplit.slice(3),
48
+ };
49
+ },
50
+ async getEntryInLocale(modelUid, baseEntryId, isoStrapi, populate = "*") {
51
+ const StrapiService = strapi.plugin("localazy").service("strapiService");
52
+ const entry = await strapi.entityService.findOne(modelUid, baseEntryId, {
53
+ populate,
54
+ });
55
+ const localizations = entry.localizations;
56
+ if (entry.locale === isoStrapi) {
57
+ return entry;
58
+ }
59
+
60
+ if (Array.isArray(localizations)) {
61
+ const localeEntry = localizations.find(
62
+ (locale) => locale.locale === isoStrapi
63
+ );
64
+ if (localeEntry) {
65
+ const populate = await StrapiService.getPopulateObject(modelUid);
66
+ return this.getEntryInLocale(
67
+ modelUid,
68
+ localeEntry.id,
69
+ isoStrapi,
70
+ populate
71
+ );
72
+ }
73
+ }
74
+
75
+ return null;
76
+ },
77
+ async createLocalizationForAnExistingEntry(ctx, uid, baseEntry, newEntry) {
78
+ try {
79
+ // this might not be localizable (then method is missing, skip)
80
+ const contentTypeController = strapi.controller(uid);
81
+ const newLocalizationCtx = { ...ctx };
82
+ newLocalizationCtx.is = () => false; // else throws an error in createLocalization
83
+ newLocalizationCtx.params.id = baseEntry.id;
84
+
85
+ const newEntryLocale = newEntry.locale;
86
+ const filteredNewEntry = omitDeep(newEntry, [
87
+ "locale",
88
+ "id",
89
+ "createdAt",
90
+ "publishedAt", // sets as draft (no timestamp)
91
+ "updatedAt",
92
+ ]);
93
+ filteredNewEntry.locale = newEntryLocale;
94
+
95
+ newLocalizationCtx.request.body = filteredNewEntry;
96
+
97
+ await contentTypeController.createLocalization(newLocalizationCtx);
98
+
99
+ const insertedEntry = await this.getEntryInLocale(
100
+ uid,
101
+ baseEntry.id,
102
+ newEntryLocale
103
+ );
104
+ const mergedEntry = merge(baseEntry, insertedEntry);
105
+
106
+ return mergedEntry;
107
+ } catch (e) {
108
+ strapi.log.error(e);
109
+ throw e;
110
+ }
111
+ },
112
+ async updateLocalizationForAnExistingEntry(uid, updateEntryId, data) {
113
+ try {
114
+ const StrapiService = strapi.plugin("localazy").service("strapiService");
115
+ const populate = await StrapiService.getPopulateObject(uid);
116
+
117
+ const updatedEntry = await strapi.entityService.update(
118
+ uid,
119
+ updateEntryId,
120
+ {
121
+ data,
122
+ populate,
123
+ }
124
+ );
125
+
126
+ return updatedEntry;
127
+ } catch (e) {
128
+ strapi.log.error(e);
129
+ throw e;
130
+ }
131
+ },
132
+ });
@@ -0,0 +1,23 @@
1
+ "use strict";
2
+
3
+ const isLocalazyApplicableContentType = require("../utils/is-localazy-applicable-content-type");
4
+ const isGenerallyApplicableContentType = require("../utils/is-generally-applicable-content-type");
5
+ const buildPopulate = require("../utils/build-populate");
6
+
7
+ module.exports = ({ strapi }) => ({
8
+ // possible help source https://stackoverflow.com/questions/66180042/is-there-a-way-to-get-a-structure-of-a-strapi-cms-content-type
9
+ async getModels() {
10
+ return strapi.db.config.models.filter((model) =>
11
+ isGenerallyApplicableContentType(model)
12
+ );
13
+ },
14
+ async getLocalizableModels() {
15
+ return strapi.db.config.models.filter((model) =>
16
+ isLocalazyApplicableContentType(model)
17
+ );
18
+ },
19
+ async getPopulateObject(modelUid) {
20
+ const models = strapi.db.config.models;
21
+ return buildPopulate(models, modelUid);
22
+ },
23
+ });