@raketa-cloud/admin 1.10.10 → 1.10.12
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 +1 -1
- package/src/cloud/ResourceChanges.js +1 -1
- package/src/handlers/resources/TranslationsHandler.js +30 -1
- package/src/tests/cloud/ResourceChanges.test.js +25 -0
- package/src/tests/handlers/resources/TranslationsHandler.test/create.test.js +32 -0
- package/src/tests/handlers/resources/TranslationsHandler.test/edge_cases.test.js +4 -1
package/package.json
CHANGED
|
@@ -1,3 +1,21 @@
|
|
|
1
|
+
function matchResourceIdAndLocale(resources) {
|
|
2
|
+
const idLocaleMap = new Map();
|
|
3
|
+
|
|
4
|
+
resources.forEach((r) => {
|
|
5
|
+
Object.keys(r.translations).forEach((locale) => {
|
|
6
|
+
const translation = r.translations[locale];
|
|
7
|
+
|
|
8
|
+
if (!translation.id) {
|
|
9
|
+
throw new Error("Missing id in translation!");
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
idLocaleMap.set(translation.id.toString(), locale);
|
|
13
|
+
});
|
|
14
|
+
});
|
|
15
|
+
|
|
16
|
+
return idLocaleMap;
|
|
17
|
+
}
|
|
18
|
+
|
|
1
19
|
const index = async (ctx) => {
|
|
2
20
|
const { parent_id } = ctx.params;
|
|
3
21
|
const { translations } = ctx.query;
|
|
@@ -222,12 +240,23 @@ const create = async (ctx) => {
|
|
|
222
240
|
}
|
|
223
241
|
|
|
224
242
|
const revisionInputs = [];
|
|
243
|
+
const resourceIdLocaleMap =
|
|
244
|
+
resources.length === selectedLocales.length
|
|
245
|
+
? null
|
|
246
|
+
: matchResourceIdAndLocale(resources);
|
|
225
247
|
|
|
226
248
|
for (let i = 0; i < resources.length; i++) {
|
|
227
249
|
const translated = resources[i];
|
|
228
|
-
const locale = selectedLocales[i];
|
|
229
250
|
|
|
230
251
|
if (translated) {
|
|
252
|
+
const locale = resourceIdLocaleMap
|
|
253
|
+
? resourceIdLocaleMap.get(translated.id.toString())
|
|
254
|
+
: selectedLocales[i];
|
|
255
|
+
|
|
256
|
+
if (!selectedLocales.includes(locale)) {
|
|
257
|
+
continue;
|
|
258
|
+
}
|
|
259
|
+
|
|
231
260
|
blueprint.translations = {
|
|
232
261
|
...blueprint.translations,
|
|
233
262
|
[locale]: {
|
|
@@ -0,0 +1,25 @@
|
|
|
1
|
+
import ResourceChanges from "../../cloud/ResourceChanges";
|
|
2
|
+
import { ResourceHistoryDocument } from "../../cloud/documents/resourcesDocuments";
|
|
3
|
+
|
|
4
|
+
describe("ResourceChanges", () => {
|
|
5
|
+
describe("list", () => {
|
|
6
|
+
test("uses camelCase pageSize when requesting resource history", async () => {
|
|
7
|
+
const client = new ResourceChanges(() => ({}), "token");
|
|
8
|
+
const query = jest
|
|
9
|
+
.spyOn(client, "query")
|
|
10
|
+
.mockResolvedValue({ resourceHistory: { list: [] } });
|
|
11
|
+
|
|
12
|
+
await client.list({ page: "2" }, "resource-1", "en", "page");
|
|
13
|
+
|
|
14
|
+
expect(query).toHaveBeenCalledWith(ResourceHistoryDocument, {
|
|
15
|
+
input: {
|
|
16
|
+
id: "resource-1",
|
|
17
|
+
locale: "en",
|
|
18
|
+
resourceType: "page",
|
|
19
|
+
filters: {},
|
|
20
|
+
pagination: { page: 2, pageSize: 50 },
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
});
|
|
24
|
+
});
|
|
25
|
+
});
|
|
@@ -324,6 +324,38 @@ describe("Resource Translations Handler", () => {
|
|
|
324
324
|
});
|
|
325
325
|
});
|
|
326
326
|
|
|
327
|
+
test("creates revisions and redirect pairs from returned locales when the backend omits a requested locale", async () => {
|
|
328
|
+
const resource = Factory.build("resource", {
|
|
329
|
+
translations: {},
|
|
330
|
+
categories: [{ id: "cat1" }],
|
|
331
|
+
});
|
|
332
|
+
const ctx = buildCtx({ languages: ["de", "en-us", "fr"] }, resource);
|
|
333
|
+
|
|
334
|
+
jest.spyOn(ctx.client.resources, "find").mockImplementation(() => resource);
|
|
335
|
+
jest
|
|
336
|
+
.spyOn(ctx.client.resources, "multiTranslate")
|
|
337
|
+
.mockImplementation(() => ({
|
|
338
|
+
resources: [
|
|
339
|
+
{ id: "de-1", translations: { fr: { id: "fr-1" } } },
|
|
340
|
+
{ id: "fr-1", translations: { de: { id: "de-1" } } },
|
|
341
|
+
],
|
|
342
|
+
errors: null,
|
|
343
|
+
}));
|
|
344
|
+
const createBatch = jest
|
|
345
|
+
.spyOn(ctx.client.resourceRevisions, "createBatch")
|
|
346
|
+
.mockImplementation(() => [{ errors: null }, { errors: null }]);
|
|
347
|
+
|
|
348
|
+
const result = await TranslationsHandler.create(ctx);
|
|
349
|
+
|
|
350
|
+
expect(createBatch).toHaveBeenCalledWith([
|
|
351
|
+
{ resourceId: "de-1", resourceType: "article", locale: "de" },
|
|
352
|
+
{ resourceId: "fr-1", resourceType: "article", locale: "fr" },
|
|
353
|
+
]);
|
|
354
|
+
expect(result).toEqual({
|
|
355
|
+
redirect: `/en/articles/${resource.id}/translations?translations=de:de-1;fr:fr-1`,
|
|
356
|
+
});
|
|
357
|
+
});
|
|
358
|
+
|
|
327
359
|
test("create translation fails with revision errors", async () => {
|
|
328
360
|
const resource = Factory.build("resource", {
|
|
329
361
|
translations: {},
|
|
@@ -156,7 +156,10 @@ describe("Resource Translations Handler create", () => {
|
|
|
156
156
|
jest
|
|
157
157
|
.spyOn(ctx.client.resources, "multiTranslate")
|
|
158
158
|
.mockImplementation(() => ({
|
|
159
|
-
resources: [
|
|
159
|
+
resources: [
|
|
160
|
+
{ id: "en-1", translations: { fr: { id: "fr-1" } } },
|
|
161
|
+
{ id: "fr-1", translations: { en: { id: "en-1" } } },
|
|
162
|
+
],
|
|
160
163
|
errors: null,
|
|
161
164
|
}));
|
|
162
165
|
const mockRevisions = jest.spyOn(
|