@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.
- package/.babelrc +7 -0
- package/.eslintrc +7 -0
- package/.github/workflows/.build.yml +39 -0
- package/.nvmrc +1 -0
- package/LICENSE +21 -0
- package/README.md +40 -0
- package/admin/src/.eslintrc +3 -0
- package/admin/src/components/Initializer/index.js +26 -0
- package/admin/src/components/PluginIcon/index.js +14 -0
- package/admin/src/i18n.js +37 -0
- package/admin/src/index.js +106 -0
- package/admin/src/modules/@common/components/Icons/Localazy/index.js +12 -0
- package/admin/src/modules/@common/components/SideNav/index.js +46 -0
- package/admin/src/modules/@common/components/TransferReport/index.js +54 -0
- package/admin/src/modules/@common/locale/en.js +13 -0
- package/admin/src/modules/@common/services/i18n-service.js +15 -0
- package/admin/src/modules/@common/services/project-service.js +15 -0
- package/admin/src/modules/@common/services/user-service.js +38 -0
- package/admin/src/modules/@common/utils/are-locales-compatible.js +42 -0
- package/admin/src/modules/@common/utils/functions/flatten-deep.js +23 -0
- package/admin/src/modules/@common/utils/get-default-locale.js +32 -0
- package/admin/src/modules/@common/utils/get-header-subtitle.js +21 -0
- package/admin/src/modules/@common/utils/get-header-title.js +24 -0
- package/admin/src/modules/@common/utils/get-nav.js +35 -0
- package/admin/src/modules/@common/utils/history.js +6 -0
- package/admin/src/modules/@common/utils/is-plugin-route.js +15 -0
- package/admin/src/modules/@common/utils/iso-locales-utils.js +15 -0
- package/admin/src/modules/@common/utils/redirect-to-plugin-route.js +22 -0
- package/admin/src/modules/@common/utils/sort-by-model-name.js +10 -0
- package/admin/src/modules/localazy-download/locale/en.js +20 -0
- package/admin/src/modules/localazy-download/services/localazy-download-service.js +16 -0
- package/admin/src/modules/localazy-upload/components/ReadDocsButton.js +21 -0
- package/admin/src/modules/localazy-upload/locale/en.js +18 -0
- package/admin/src/modules/localazy-upload/services/localazy-upload-service.js +16 -0
- package/admin/src/modules/login/components/LoginButton/index.js +56 -0
- package/admin/src/modules/login/components/LogoutButton/index.js +50 -0
- package/admin/src/modules/login/locale/en.js +7 -0
- package/admin/src/modules/login/services/localazy-login-service.js +29 -0
- package/admin/src/modules/plugin-settings/components/ContentTransferSetup/index.js +292 -0
- package/admin/src/modules/plugin-settings/components/Tree/index.js +120 -0
- package/admin/src/modules/plugin-settings/components/TreeItem/index.js +48 -0
- package/admin/src/modules/plugin-settings/functions/build-content-transfer-setup-schema.js +71 -0
- package/admin/src/modules/plugin-settings/functions/has-model-changed.js +85 -0
- package/admin/src/modules/plugin-settings/locale/en.js +19 -0
- package/admin/src/modules/plugin-settings/services/plugin-settings-service.js +32 -0
- package/admin/src/modules/plugin-settings/services/strapi-model-service.js +26 -0
- package/admin/src/modules/plugin-settings/utils/__tests__/get-models-tree.test.js +135 -0
- package/admin/src/modules/plugin-settings/utils/array-of-models-to-object.js +6 -0
- package/admin/src/modules/plugin-settings/utils/find-model-value-by-key.js +8 -0
- package/admin/src/modules/plugin-settings/utils/get-filtered-models-schemas.js +34 -0
- package/admin/src/modules/plugin-settings/utils/get-models-tree.js +73 -0
- package/admin/src/modules/settings/components/OverviewItem/index.js +44 -0
- package/admin/src/modules/settings/components/OverviewItemLink/index.js +34 -0
- package/admin/src/modules/settings/components/PrerequisitiesInfo/index.js +30 -0
- package/admin/src/modules/settings/locale/en.js +13 -0
- package/admin/src/modules/user/model/localazy-identity.js +14 -0
- package/admin/src/modules/user/services/localazy-user-service.js +38 -0
- package/admin/src/pages/App/index.js +152 -0
- package/admin/src/pages/Download/index.js +179 -0
- package/admin/src/pages/Index/index.js +8 -0
- package/admin/src/pages/Login/index.js +94 -0
- package/admin/src/pages/PluginSettings/index.js +42 -0
- package/admin/src/pages/Settings/index.js +120 -0
- package/admin/src/pages/Upload/index.js +187 -0
- package/admin/src/pluginId.js +5 -0
- package/admin/src/plugins/apolloClient.js +33 -0
- package/admin/src/state/localazy-identity.js +29 -0
- package/admin/src/tests/fixtures/all-models.js +877 -0
- package/admin/src/tests/fixtures/restaurant-model.js +230 -0
- package/admin/src/translations/en.json +4 -0
- package/admin/src/translations/fr.json +1 -0
- package/admin/src/utils/createAxiosInstance.js +52 -0
- package/admin/src/utils/getTrad.js +5 -0
- package/jest.config.js +0 -0
- package/package.json +60 -0
- package/server/bootstrap.js +5 -0
- package/server/config/development/index.js +15 -0
- package/server/config/example/index.js +15 -0
- package/server/config/index.js +15 -0
- package/server/config/production/index.js +16 -0
- package/server/config/schema.graphql.js +21 -0
- package/server/content-types/index.js +3 -0
- package/server/controllers/index.js +19 -0
- package/server/controllers/localazy-auth-controller.js +46 -0
- package/server/controllers/localazy-project-controller.js +21 -0
- package/server/controllers/localazy-transfer-controller.js +469 -0
- package/server/controllers/localazy-user-controller.js +24 -0
- package/server/controllers/my-controller.js +10 -0
- package/server/controllers/plugin-settings-controller.js +17 -0
- package/server/controllers/strapi-controller.js +16 -0
- package/server/db/model/content-transfer-setup.js +13 -0
- package/server/db/model/localazy-user.js +23 -0
- package/server/db/model/utils/get-strapi-store.js +9 -0
- package/server/destroy.js +5 -0
- package/server/index.js +25 -0
- package/server/middlewares/index.js +3 -0
- package/server/policies/index.js +3 -0
- package/server/register.js +5 -0
- package/server/routes/index.js +26 -0
- package/server/routes/localazy-auth.js +22 -0
- package/server/routes/localazy-project.js +14 -0
- package/server/routes/localazy-transfer.js +22 -0
- package/server/routes/localazy-user.js +30 -0
- package/server/routes/plugin-settings.js +22 -0
- package/server/routes/strapi.js +22 -0
- package/server/services/index.js +21 -0
- package/server/services/localazy-auth-service.js +38 -0
- package/server/services/localazy-download-service.js +54 -0
- package/server/services/localazy-pubapi-service.js +47 -0
- package/server/services/localazy-upload-service.js +95 -0
- package/server/services/localazy-user-service.js +29 -0
- package/server/services/plugin-settings-service.js +30 -0
- package/server/services/strapi-i18n-service.js +132 -0
- package/server/services/strapi-service.js +23 -0
- package/server/tests/fixtures/all-models-with-multimedia.js +1064 -0
- package/server/tests/fixtures/all-models.js +869 -0
- package/server/tests/fixtures/collection-flattened.js +76 -0
- package/server/tests/fixtures/restaurant-flattened.js +46 -0
- package/server/tests/fixtures/restaurant-model.js +230 -0
- package/server/tests/fixtures/restaurants-entries.js +152 -0
- package/server/tests/fixtures/restaurants-pick-paths.js +15 -0
- package/server/tests/fixtures/strapi-book-with-multimedia-components-cs.js +31 -0
- package/server/tests/fixtures/stringified-book-cs.js +3 -0
- package/server/tests/fixtures/stringified-book-with-multimedia-components-cs.js +3 -0
- package/server/tests/fixtures/stringified-restaurant-cs.js +3 -0
- package/server/tests/fixtures/stringified-restaurant-de.js +5 -0
- package/server/utils/__tests__/build-populate.test.js +85 -0
- package/server/utils/__tests__/flatten-object.test.js +171 -0
- package/server/utils/__tests__/get-models-tree.test.js +170 -0
- package/server/utils/__tests__/parsed-localazy-entry-to-create-entry.test.js +153 -0
- package/server/utils/__tests__/parsed-localazy-entry-to-update-entry.test.js +172 -0
- package/server/utils/__tests__/pick-entries.test.js +83 -0
- package/server/utils/build-populate.js +43 -0
- package/server/utils/delay.js +6 -0
- package/server/utils/flatten-object.js +52 -0
- package/server/utils/get-localazy-api.js +22 -0
- package/server/utils/get-main-keys-of-nested-array-objects.js +5 -0
- package/server/utils/get-models-tree.js +43 -0
- package/server/utils/intl-display-name.js +7 -0
- package/server/utils/is-generally-applicable-content-type.js +33 -0
- package/server/utils/is-localazy-applicable-content-type.js +36 -0
- package/server/utils/iso-locales-utils.js +22 -0
- package/server/utils/objectivise-entry.js +41 -0
- package/server/utils/omit-deep.js +17 -0
- package/server/utils/parsed-localazy-entry-to-create-entry.js +115 -0
- package/server/utils/parsed-localazy-entry-to-update-entry.js +31 -0
- package/server/utils/pick-entries.js +14 -0
- package/server/utils/transfer-setup-utils.js +39 -0
- package/strapi-admin.js +14 -0
- package/strapi-server.js +5 -0
|
@@ -0,0 +1,469 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const flattenObject = require("../utils/flatten-object");
|
|
4
|
+
const {
|
|
5
|
+
getCollectionsNames,
|
|
6
|
+
findSetupModelByCollectionName,
|
|
7
|
+
isCollectionTransferEnabled,
|
|
8
|
+
getPickPaths,
|
|
9
|
+
} = require("../utils/transfer-setup-utils");
|
|
10
|
+
const pickEntries = require("../utils/pick-entries");
|
|
11
|
+
const config = require("../config").default;
|
|
12
|
+
const {
|
|
13
|
+
isoLocalazyToStrapi,
|
|
14
|
+
isoStrapiToLocalazy,
|
|
15
|
+
} = require("../utils/iso-locales-utils");
|
|
16
|
+
const parsedLocalazyEntryToCreateEntry = require("../utils/parsed-localazy-entry-to-create-entry");
|
|
17
|
+
const parsedLocalazyEntryToUpdateEntry = require("../utils/parsed-localazy-entry-to-update-entry");
|
|
18
|
+
const set = require("lodash/set");
|
|
19
|
+
const merge = require("lodash/merge");
|
|
20
|
+
|
|
21
|
+
module.exports = {
|
|
22
|
+
async upload(ctx) {
|
|
23
|
+
try {
|
|
24
|
+
let success = true;
|
|
25
|
+
const messageReport = [];
|
|
26
|
+
|
|
27
|
+
// Strapi Service
|
|
28
|
+
const StrapiService = strapi.plugin("localazy").service("strapiService");
|
|
29
|
+
|
|
30
|
+
// Strapi i18n Service
|
|
31
|
+
const StrapiI18nService = strapi
|
|
32
|
+
.plugin("localazy")
|
|
33
|
+
.service("strapiI18nService");
|
|
34
|
+
|
|
35
|
+
// Localazy Upload Service
|
|
36
|
+
const LocalazyUploadService = strapi
|
|
37
|
+
.plugin("localazy")
|
|
38
|
+
.service("localazyUploadService");
|
|
39
|
+
|
|
40
|
+
// get content transfer setup
|
|
41
|
+
const contentTransferSetup = await strapi
|
|
42
|
+
.plugin("localazy")
|
|
43
|
+
.service("pluginSettingsService")
|
|
44
|
+
.getContentTransferSetup();
|
|
45
|
+
|
|
46
|
+
if (!contentTransferSetup.has_setup) {
|
|
47
|
+
const message = "Content transfer setup is not set up.";
|
|
48
|
+
strapi.log.info(message);
|
|
49
|
+
messageReport.push(message);
|
|
50
|
+
success = false;
|
|
51
|
+
ctx.body = {
|
|
52
|
+
success,
|
|
53
|
+
report: messageReport,
|
|
54
|
+
};
|
|
55
|
+
return;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
const { setup } = contentTransferSetup;
|
|
59
|
+
const collectionsNames = getCollectionsNames(setup);
|
|
60
|
+
const models = await StrapiService.getModels();
|
|
61
|
+
// flatten Strapi content
|
|
62
|
+
let flattenContent = {};
|
|
63
|
+
|
|
64
|
+
for (const collectionName of collectionsNames) {
|
|
65
|
+
const currentModel = models.find(
|
|
66
|
+
(model) => model.collectionName === collectionName
|
|
67
|
+
);
|
|
68
|
+
const modelUid = currentModel.uid;
|
|
69
|
+
if (!currentModel) {
|
|
70
|
+
strapi.log.warn(`Model with uid ${modelUid} is not found.`);
|
|
71
|
+
continue;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
const transferSetupModel = findSetupModelByCollectionName(
|
|
75
|
+
setup,
|
|
76
|
+
collectionName
|
|
77
|
+
);
|
|
78
|
+
|
|
79
|
+
if (!isCollectionTransferEnabled(setup, collectionName)) {
|
|
80
|
+
const message = `Collection ${collectionName} transfer is disabled.`;
|
|
81
|
+
messageReport.push(message);
|
|
82
|
+
strapi.log.info(message);
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// get only enabled fields paths
|
|
87
|
+
const pickPaths = getPickPaths(transferSetupModel[collectionName]);
|
|
88
|
+
if (!pickPaths.length) {
|
|
89
|
+
const message = `No fields for collection ${collectionName} transfer are enabled.`;
|
|
90
|
+
messageReport.push(message);
|
|
91
|
+
strapi.log.warn(message);
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const pickPathsWithUid = pickPaths.map(
|
|
95
|
+
(pickPath) => `${modelUid}.${pickPath}`
|
|
96
|
+
);
|
|
97
|
+
|
|
98
|
+
const populate = await StrapiService.getPopulateObject(modelUid);
|
|
99
|
+
|
|
100
|
+
let entries = await strapi.entityService.findMany(modelUid, {
|
|
101
|
+
populate,
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
if (!entries) {
|
|
105
|
+
strapi.log.info(`No entries found for model ${modelUid}`);
|
|
106
|
+
continue;
|
|
107
|
+
}
|
|
108
|
+
|
|
109
|
+
if (!Array.isArray(entries)) {
|
|
110
|
+
entries = [entries];
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
entries.forEach((entry) => {
|
|
114
|
+
const flatten = flattenObject({
|
|
115
|
+
[modelUid]: entry,
|
|
116
|
+
});
|
|
117
|
+
// get only enabled fields
|
|
118
|
+
const pickedFlatten = pickEntries(flatten, pickPathsWithUid);
|
|
119
|
+
|
|
120
|
+
flattenContent = {
|
|
121
|
+
...flattenContent,
|
|
122
|
+
...pickedFlatten,
|
|
123
|
+
};
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
// get Strapi default language and convert it to Localazy language code
|
|
128
|
+
const strapiLocales = await StrapiI18nService.getLocales(ctx);
|
|
129
|
+
const defaultLocale = strapiLocales.find((locale) => locale.isDefault);
|
|
130
|
+
|
|
131
|
+
const locale = defaultLocale
|
|
132
|
+
? isoStrapiToLocalazy(defaultLocale.code)
|
|
133
|
+
: config.LOCALAZY_DEFAULT_LOCALE;
|
|
134
|
+
|
|
135
|
+
const chunks = LocalazyUploadService.splitToChunks(flattenContent);
|
|
136
|
+
const importFile = LocalazyUploadService.createImportFileRepresentation(
|
|
137
|
+
config.LOCALAZY_DEFAULT_FILE_NAME,
|
|
138
|
+
config.LOCALAZY_DEFAULT_FILE_PATH,
|
|
139
|
+
config.LOCALAZY_DEFAULT_FILE_EXTENSION,
|
|
140
|
+
locale,
|
|
141
|
+
chunks
|
|
142
|
+
);
|
|
143
|
+
await LocalazyUploadService.upload(importFile);
|
|
144
|
+
|
|
145
|
+
ctx.body = {
|
|
146
|
+
success,
|
|
147
|
+
report: messageReport,
|
|
148
|
+
};
|
|
149
|
+
} catch (e) {
|
|
150
|
+
strapi.log.error(e.message);
|
|
151
|
+
ctx.throw(400, e);
|
|
152
|
+
}
|
|
153
|
+
},
|
|
154
|
+
|
|
155
|
+
async download(ctx) {
|
|
156
|
+
let success = true;
|
|
157
|
+
const messageReport = [];
|
|
158
|
+
|
|
159
|
+
// Strapi service
|
|
160
|
+
const StrapiService = strapi.plugin("localazy").service("strapiService");
|
|
161
|
+
|
|
162
|
+
// Strapi i18n Service
|
|
163
|
+
const StrapiI18nService = strapi
|
|
164
|
+
.plugin("localazy")
|
|
165
|
+
.service("strapiI18nService");
|
|
166
|
+
|
|
167
|
+
const LocalazyUserService = strapi
|
|
168
|
+
.plugin("localazy")
|
|
169
|
+
.service("localazyUserService");
|
|
170
|
+
const LocalazyPubApiService = strapi
|
|
171
|
+
.plugin("localazy")
|
|
172
|
+
.service("localazyPubApiService");
|
|
173
|
+
const LocalazyDownloadService = strapi
|
|
174
|
+
.plugin("localazy")
|
|
175
|
+
.service("localazyDownloadService");
|
|
176
|
+
|
|
177
|
+
const user = await LocalazyUserService.getUser();
|
|
178
|
+
|
|
179
|
+
/**
|
|
180
|
+
* Get Strapi File
|
|
181
|
+
*/
|
|
182
|
+
const strapiFile = await LocalazyPubApiService.getStrapiFile(
|
|
183
|
+
user.project.id
|
|
184
|
+
);
|
|
185
|
+
if (!strapiFile) {
|
|
186
|
+
strapi.log.error(`File ${config.LOCALAZY_DEFAULT_FILE_NAME} not found`);
|
|
187
|
+
ctx.throw(400, `File ${config.LOCALAZY_DEFAULT_FILE_NAME} not found`);
|
|
188
|
+
}
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Get Localazy Project (incl. languages)
|
|
192
|
+
*/
|
|
193
|
+
const project = await LocalazyPubApiService.getProject(
|
|
194
|
+
user.project.id,
|
|
195
|
+
false,
|
|
196
|
+
true
|
|
197
|
+
);
|
|
198
|
+
if (!project) {
|
|
199
|
+
success = false;
|
|
200
|
+
const message = `Project ${user.project.id} not found`;
|
|
201
|
+
strapi.log.error(message);
|
|
202
|
+
messageReport.push(message);
|
|
203
|
+
ctx.body = {
|
|
204
|
+
success,
|
|
205
|
+
report: messageReport,
|
|
206
|
+
};
|
|
207
|
+
return;
|
|
208
|
+
}
|
|
209
|
+
|
|
210
|
+
/**
|
|
211
|
+
* Get Localazy project languages
|
|
212
|
+
*/
|
|
213
|
+
const projectLanguages = project.languages;
|
|
214
|
+
const projectSourceLanguageId = project.sourceLanguage;
|
|
215
|
+
const sourceLanguage = project.languages.find(
|
|
216
|
+
(language) => language.id === projectSourceLanguageId
|
|
217
|
+
);
|
|
218
|
+
|
|
219
|
+
if (!sourceLanguage) {
|
|
220
|
+
success = false;
|
|
221
|
+
const message = "Source language not found";
|
|
222
|
+
strapi.log.error(message);
|
|
223
|
+
messageReport.push(message);
|
|
224
|
+
ctx.body = {
|
|
225
|
+
success,
|
|
226
|
+
report: messageReport,
|
|
227
|
+
};
|
|
228
|
+
return;
|
|
229
|
+
}
|
|
230
|
+
|
|
231
|
+
const projectSourceLanguageCode = sourceLanguage.code;
|
|
232
|
+
const projectLanguagesWithoutSourceLanguage = projectLanguages.filter(
|
|
233
|
+
(language) => language.code !== projectSourceLanguageCode
|
|
234
|
+
);
|
|
235
|
+
|
|
236
|
+
if (!projectLanguagesWithoutSourceLanguage.length) {
|
|
237
|
+
const message = "Your Localazy project is not translated to other languages.";
|
|
238
|
+
strapi.log.info(message);
|
|
239
|
+
messageReport.push(message);
|
|
240
|
+
ctx.body = {
|
|
241
|
+
success,
|
|
242
|
+
report: messageReport,
|
|
243
|
+
};
|
|
244
|
+
return;
|
|
245
|
+
}
|
|
246
|
+
|
|
247
|
+
/**
|
|
248
|
+
* Get project languages codes
|
|
249
|
+
*/
|
|
250
|
+
const languagesCodes = projectLanguagesWithoutSourceLanguage.map(
|
|
251
|
+
(language) => language.code
|
|
252
|
+
);
|
|
253
|
+
|
|
254
|
+
/**
|
|
255
|
+
* Iterate over languages and create the ones that are not present in Strapi
|
|
256
|
+
*/
|
|
257
|
+
const strapiUnsupportedLanguages = []; // do not iterate over these languages later
|
|
258
|
+
for (const isoLocalazy of languagesCodes) {
|
|
259
|
+
try {
|
|
260
|
+
const newLocaleCode = await StrapiI18nService.createStrapiLocale(
|
|
261
|
+
ctx,
|
|
262
|
+
isoLocalazy
|
|
263
|
+
);
|
|
264
|
+
messageReport.push(`Created locale ${newLocaleCode}`);
|
|
265
|
+
} catch (e) {
|
|
266
|
+
if (
|
|
267
|
+
e.name === "ApplicationError" &&
|
|
268
|
+
e.message === "This locale already exists"
|
|
269
|
+
) {
|
|
270
|
+
strapi.log.info(`The Localazy locale ${isoLocalazy} already exists.`);
|
|
271
|
+
} else {
|
|
272
|
+
strapi.log.error(e.message);
|
|
273
|
+
}
|
|
274
|
+
if (e.name === "ValidationError") {
|
|
275
|
+
// store unsupported language code
|
|
276
|
+
strapiUnsupportedLanguages.push(isoLocalazy);
|
|
277
|
+
messageReport.push(
|
|
278
|
+
`Language ${isoLocalazy} is not supported by Strapi`
|
|
279
|
+
);
|
|
280
|
+
}
|
|
281
|
+
}
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
/**
|
|
285
|
+
* Download all keys for Strapi-supported languages
|
|
286
|
+
*/
|
|
287
|
+
const supportedLanguages = languagesCodes.filter(
|
|
288
|
+
(languageCode) => !strapiUnsupportedLanguages.includes(languageCode)
|
|
289
|
+
);
|
|
290
|
+
const localazyContent = {};
|
|
291
|
+
for (const isoLocalazy of supportedLanguages) {
|
|
292
|
+
const result = await LocalazyDownloadService.download({
|
|
293
|
+
projectId: user.project.id,
|
|
294
|
+
fileId: strapiFile.id,
|
|
295
|
+
lang: isoLocalazy,
|
|
296
|
+
});
|
|
297
|
+
if (!result.success) {
|
|
298
|
+
messageReport.push(result.message);
|
|
299
|
+
}
|
|
300
|
+
const langKeys = result.data;
|
|
301
|
+
localazyContent[isoLocalazy] = langKeys;
|
|
302
|
+
}
|
|
303
|
+
|
|
304
|
+
/**
|
|
305
|
+
* Parse Localazy content
|
|
306
|
+
*/
|
|
307
|
+
const parsedLocalazyContent = {};
|
|
308
|
+
for (const [isoLocalazy, keys] of Object.entries(localazyContent)) {
|
|
309
|
+
const isoStrapi = isoLocalazyToStrapi(isoLocalazy);
|
|
310
|
+
if (!isoStrapi) {
|
|
311
|
+
messageReport.push(
|
|
312
|
+
`Language ${isoLocalazy} is not supported by Strapi`
|
|
313
|
+
);
|
|
314
|
+
continue;
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
for (const localazyEntry of keys) {
|
|
318
|
+
const key = localazyEntry.key[0];
|
|
319
|
+
const value = localazyEntry.value;
|
|
320
|
+
|
|
321
|
+
const parsedKey = StrapiI18nService.parseLocalazyKey(key);
|
|
322
|
+
const setKey = [
|
|
323
|
+
isoStrapi,
|
|
324
|
+
parsedKey.uid,
|
|
325
|
+
parsedKey.id,
|
|
326
|
+
...parsedKey.rest,
|
|
327
|
+
];
|
|
328
|
+
|
|
329
|
+
set(parsedLocalazyContent, setKey, value);
|
|
330
|
+
}
|
|
331
|
+
}
|
|
332
|
+
|
|
333
|
+
/**
|
|
334
|
+
* Iterate over parsed Localazy content and insert/update content in Strapi
|
|
335
|
+
*/
|
|
336
|
+
const strapiContentTypesModels = await StrapiService.getModels();
|
|
337
|
+
for (const [isoStrapi, contentTypes] of Object.entries(
|
|
338
|
+
parsedLocalazyContent
|
|
339
|
+
)) {
|
|
340
|
+
for (const [uid, models] of Object.entries(contentTypes)) {
|
|
341
|
+
/**
|
|
342
|
+
* Model could not exist anymore in Strapi
|
|
343
|
+
*/
|
|
344
|
+
const strapiContentTypeModel = strapiContentTypesModels.find(
|
|
345
|
+
(model) => model.uid === uid
|
|
346
|
+
);
|
|
347
|
+
if (!strapiContentTypeModel) {
|
|
348
|
+
strapi.log.info(
|
|
349
|
+
`Model ${uid} not found in Strapi, it has been deleted, skipping...`
|
|
350
|
+
);
|
|
351
|
+
continue;
|
|
352
|
+
}
|
|
353
|
+
|
|
354
|
+
for (const [id, translatedModel] of Object.entries(models)) {
|
|
355
|
+
try {
|
|
356
|
+
/**
|
|
357
|
+
* Get original source language entry
|
|
358
|
+
*/
|
|
359
|
+
const populate = await StrapiService.getPopulateObject(uid);
|
|
360
|
+
const baseEntryDeep = await strapi.entityService.findOne(uid, id, {
|
|
361
|
+
populate,
|
|
362
|
+
});
|
|
363
|
+
let baseEntry = await strapi.entityService.findOne(uid, id, {
|
|
364
|
+
populate: "*",
|
|
365
|
+
});
|
|
366
|
+
baseEntry = merge(baseEntry, baseEntryDeep);
|
|
367
|
+
|
|
368
|
+
if (!baseEntry) {
|
|
369
|
+
const message = `Source language entry ${uid}[${id}] does not exist anymore, skipping...`;
|
|
370
|
+
throw new Error(message);
|
|
371
|
+
}
|
|
372
|
+
|
|
373
|
+
/**
|
|
374
|
+
* Get current language entry
|
|
375
|
+
*/
|
|
376
|
+
const baseEntryLocalizations = baseEntry.localizations;
|
|
377
|
+
|
|
378
|
+
if (!Array.isArray(baseEntryLocalizations)) {
|
|
379
|
+
throw new Error(
|
|
380
|
+
`Content type ${uid} does not support localizations, skipping...`
|
|
381
|
+
);
|
|
382
|
+
}
|
|
383
|
+
|
|
384
|
+
const baseEntryCurrentLanguageLocalizationInfo =
|
|
385
|
+
baseEntryLocalizations.find(
|
|
386
|
+
(localization) => localization.locale === isoStrapi
|
|
387
|
+
);
|
|
388
|
+
|
|
389
|
+
if (!baseEntryCurrentLanguageLocalizationInfo) {
|
|
390
|
+
// create new entry
|
|
391
|
+
try {
|
|
392
|
+
const createEntry = parsedLocalazyEntryToCreateEntry(
|
|
393
|
+
strapiContentTypesModels,
|
|
394
|
+
translatedModel,
|
|
395
|
+
uid,
|
|
396
|
+
isoStrapi
|
|
397
|
+
);
|
|
398
|
+
const createdEntry =
|
|
399
|
+
await StrapiI18nService.createLocalizationForAnExistingEntry(
|
|
400
|
+
ctx,
|
|
401
|
+
uid,
|
|
402
|
+
baseEntry,
|
|
403
|
+
createEntry
|
|
404
|
+
);
|
|
405
|
+
messageReport.push(
|
|
406
|
+
`Created new entry ${uid}[${createdEntry.id}] in language ${isoStrapi}`
|
|
407
|
+
);
|
|
408
|
+
} catch (e) {
|
|
409
|
+
success = false;
|
|
410
|
+
strapi.log.error(e.message);
|
|
411
|
+
messageReport.push(
|
|
412
|
+
`Cannot create an entry in ${isoStrapi} for ${uid}[${baseEntry.id}]: ${e.message}`
|
|
413
|
+
);
|
|
414
|
+
}
|
|
415
|
+
} else {
|
|
416
|
+
// update existing entry
|
|
417
|
+
try {
|
|
418
|
+
const localizedEntryId =
|
|
419
|
+
baseEntryCurrentLanguageLocalizationInfo.id;
|
|
420
|
+
const populate = await StrapiService.getPopulateObject(uid); // not needed anymore here
|
|
421
|
+
const localizedEntry = await strapi.entityService.findOne(
|
|
422
|
+
uid,
|
|
423
|
+
localizedEntryId,
|
|
424
|
+
{
|
|
425
|
+
populate,
|
|
426
|
+
}
|
|
427
|
+
);
|
|
428
|
+
|
|
429
|
+
const updateEntry = parsedLocalazyEntryToUpdateEntry(
|
|
430
|
+
strapiContentTypesModels,
|
|
431
|
+
translatedModel,
|
|
432
|
+
localizedEntry,
|
|
433
|
+
uid
|
|
434
|
+
);
|
|
435
|
+
|
|
436
|
+
const updatedEntry =
|
|
437
|
+
await StrapiI18nService.updateLocalizationForAnExistingEntry(
|
|
438
|
+
uid,
|
|
439
|
+
localizedEntryId,
|
|
440
|
+
updateEntry
|
|
441
|
+
);
|
|
442
|
+
messageReport.push(
|
|
443
|
+
`Updated ${uid}[${updatedEntry.id}] (${isoStrapi})`
|
|
444
|
+
);
|
|
445
|
+
} catch (e) {
|
|
446
|
+
success = false;
|
|
447
|
+
strapi.log.error(e.message);
|
|
448
|
+
messageReport.push(
|
|
449
|
+
`Cannot update an ${uid}[${baseEntryCurrentLanguageLocalizationInfo.id}] (${isoStrapi}): ${e.message}`
|
|
450
|
+
);
|
|
451
|
+
}
|
|
452
|
+
}
|
|
453
|
+
} catch (e) {
|
|
454
|
+
success = false;
|
|
455
|
+
strapi.log.error(e.message);
|
|
456
|
+
messageReport.push(
|
|
457
|
+
`An error occured while processing download: ${e.message}`
|
|
458
|
+
);
|
|
459
|
+
}
|
|
460
|
+
}
|
|
461
|
+
}
|
|
462
|
+
}
|
|
463
|
+
|
|
464
|
+
ctx.body = {
|
|
465
|
+
success,
|
|
466
|
+
report: messageReport,
|
|
467
|
+
};
|
|
468
|
+
},
|
|
469
|
+
};
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async getUser(ctx) {
|
|
5
|
+
ctx.body = await strapi
|
|
6
|
+
.plugin("localazy")
|
|
7
|
+
.service("localazyUserService")
|
|
8
|
+
.getUser();
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
async updateUser(ctx) {
|
|
12
|
+
ctx.body = await strapi
|
|
13
|
+
.plugin("localazy")
|
|
14
|
+
.service("localazyUserService")
|
|
15
|
+
.updateUser(ctx.request.body);
|
|
16
|
+
},
|
|
17
|
+
|
|
18
|
+
async deleteUser(ctx) {
|
|
19
|
+
ctx.body = await strapi
|
|
20
|
+
.plugin("localazy")
|
|
21
|
+
.service("localazyUserService")
|
|
22
|
+
.deleteUser();
|
|
23
|
+
},
|
|
24
|
+
};
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async getContentTransferSetup(ctx) {
|
|
5
|
+
ctx.body = await strapi
|
|
6
|
+
.plugin("localazy")
|
|
7
|
+
.service("pluginSettingsService")
|
|
8
|
+
.getContentTransferSetup();
|
|
9
|
+
},
|
|
10
|
+
|
|
11
|
+
async updateContentTransferSetup(ctx) {
|
|
12
|
+
ctx.body = await strapi
|
|
13
|
+
.plugin("localazy")
|
|
14
|
+
.service("pluginSettingsService")
|
|
15
|
+
.updateContentTransferSetup(ctx.request.body);
|
|
16
|
+
},
|
|
17
|
+
};
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
module.exports = {
|
|
4
|
+
async getModels(ctx) {
|
|
5
|
+
ctx.body = await strapi
|
|
6
|
+
.plugin("localazy")
|
|
7
|
+
.service("strapiService")
|
|
8
|
+
.getModels();
|
|
9
|
+
},
|
|
10
|
+
async getLocalizableModels(ctx) {
|
|
11
|
+
ctx.body = await strapi
|
|
12
|
+
.plugin("localazy")
|
|
13
|
+
.service("strapiService")
|
|
14
|
+
.getLocalizableModels();
|
|
15
|
+
},
|
|
16
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const KEY = "identity";
|
|
4
|
+
|
|
5
|
+
const emptyIdentity = {
|
|
6
|
+
accessToken: "",
|
|
7
|
+
project: {
|
|
8
|
+
id: "",
|
|
9
|
+
image: "",
|
|
10
|
+
name: "",
|
|
11
|
+
url: "",
|
|
12
|
+
},
|
|
13
|
+
scope: "",
|
|
14
|
+
user: {
|
|
15
|
+
id: "",
|
|
16
|
+
name: "",
|
|
17
|
+
},
|
|
18
|
+
};
|
|
19
|
+
|
|
20
|
+
module.exports = {
|
|
21
|
+
KEY,
|
|
22
|
+
emptyIdentity,
|
|
23
|
+
};
|
package/server/index.js
ADDED
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const register = require("./register");
|
|
4
|
+
const bootstrap = require("./bootstrap");
|
|
5
|
+
const destroy = require("./destroy");
|
|
6
|
+
const config = require("./config");
|
|
7
|
+
const contentTypes = require("./content-types");
|
|
8
|
+
const controllers = require("./controllers");
|
|
9
|
+
const routes = require("./routes");
|
|
10
|
+
const middlewares = require("./middlewares");
|
|
11
|
+
const policies = require("./policies");
|
|
12
|
+
const services = require("./services");
|
|
13
|
+
|
|
14
|
+
module.exports = {
|
|
15
|
+
register,
|
|
16
|
+
bootstrap,
|
|
17
|
+
destroy,
|
|
18
|
+
config,
|
|
19
|
+
controllers,
|
|
20
|
+
routes,
|
|
21
|
+
services,
|
|
22
|
+
contentTypes,
|
|
23
|
+
policies,
|
|
24
|
+
middlewares,
|
|
25
|
+
};
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const localazyUserRoutes = require("./localazy-user");
|
|
4
|
+
const localazyAuthRoutes = require("./localazy-auth");
|
|
5
|
+
const pluginSettingsRoutes = require("./plugin-settings");
|
|
6
|
+
const strapiRoutes = require("./strapi");
|
|
7
|
+
const localazyTransferRoutes = require("./localazy-transfer");
|
|
8
|
+
const localazyProjectRoutes = require("./localazy-project");
|
|
9
|
+
|
|
10
|
+
module.exports = {
|
|
11
|
+
"content-api": {
|
|
12
|
+
type: "content-api",
|
|
13
|
+
routes: [...strapiRoutes],
|
|
14
|
+
},
|
|
15
|
+
admin: {
|
|
16
|
+
type: "admin",
|
|
17
|
+
routes: [
|
|
18
|
+
...localazyAuthRoutes,
|
|
19
|
+
...localazyUserRoutes,
|
|
20
|
+
...pluginSettingsRoutes,
|
|
21
|
+
...strapiRoutes,
|
|
22
|
+
...localazyTransferRoutes,
|
|
23
|
+
...localazyProjectRoutes,
|
|
24
|
+
],
|
|
25
|
+
},
|
|
26
|
+
};
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
|
|
3
|
+
const ROUTE_PREFIX = "/auth";
|
|
4
|
+
|
|
5
|
+
module.exports = [
|
|
6
|
+
{
|
|
7
|
+
method: "GET",
|
|
8
|
+
path: `${ROUTE_PREFIX}/generate-keys`,
|
|
9
|
+
handler: "localazyAuthController.generateKeys",
|
|
10
|
+
config: {
|
|
11
|
+
policies: [],
|
|
12
|
+
},
|
|
13
|
+
},
|
|
14
|
+
{
|
|
15
|
+
method: "GET",
|
|
16
|
+
path: `${ROUTE_PREFIX}/continuous-poll`,
|
|
17
|
+
handler: "localazyAuthController.continuousPoll",
|
|
18
|
+
config: {
|
|
19
|
+
policies: [],
|
|
20
|
+
},
|
|
21
|
+
},
|
|
22
|
+
];
|