Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[HotFix] Sync route preserving translations for menu and filters #3991

Merged
merged 1 commit into from
Oct 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions app/api/sync/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import { Request, Application } from 'express';
import { FileType } from 'shared/types/fileType';
import { uploadsPath, customUploadsPath, uploadMiddleware } from 'api/files';
import { needsAuthorization } from '../auth';
import { TranslationType } from 'shared/translationType';

const storage = multer.diskStorage({
filename(_req, file, cb) {
Expand Down Expand Up @@ -47,6 +48,22 @@ const deleteFromIndex = async (req: Request, file: FileType) => {
}
};

const preserveTranslations = async (syncData: TranslationType): Promise<TranslationType> => {
const [translation] = (await models.translations.get({ _id: syncData._id })) as TranslationType[];
if (!translation) {
return syncData;
}
const menu = translation.contexts?.find(c => c.id === 'Menu');
const filters = translation.contexts?.find(c => c.id === 'Filters');
if (menu) {
syncData.contexts?.push(menu);
}
if (filters) {
syncData.contexts?.push(filters);
}
return syncData;
};

export default (app: Application) => {
app.post('/api/sync', needsAuthorization(['admin']), async (req, res, next) => {
try {
Expand All @@ -55,6 +72,10 @@ export default (app: Application) => {
req.body.data._id = settings._id;
}

if (req.body.namespace === 'translations') {
req.body.data = await preserveTranslations(req.body.data);
}

await (Array.isArray(req.body.data)
? models[req.body.namespace].saveMultiple(req.body.data)
: models[req.body.namespace].save(req.body.data));
Expand Down
38 changes: 38 additions & 0 deletions app/api/sync/specs/routes.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -123,6 +123,44 @@ describe('sync', () => {
expect(routes._post('/api/sync/upload', {})).toNeedAuthorization();
});
});

describe('when namespace is translations', () => {
it('should respect menu and filters translations', async () => {
models.translations = {
save: jasmine.createSpy('entities.save'),
delete: jasmine.createSpy('entities.delete'),
get: jasmine.createSpy('entities.get').and.returnValue(
Promise.resolve([
{
_id: 'id',
contexts: [
{ id: 'Menu', values: [{ key: 'About us', value: 'About us' }] },
{ id: 'Filters', values: [{ key: 'Cause', value: 'Cause' }] },
],
},
])
),
};

req.body = {
namespace: 'translations',
data: {
_id: 'id',
contexts: [{ id: 'System', values: [{ key: 'Search', value: 'Search' }] }],
},
};

await routes.post('/api/sync', req);
expect(models.translations.save).toHaveBeenCalledWith({
_id: 'id',
contexts: [
{ id: 'System', values: [{ key: 'Search', value: 'Search' }] },
{ id: 'Menu', values: [{ key: 'About us', value: 'About us' }] },
{ id: 'Filters', values: [{ key: 'Cause', value: 'Cause' }] },
],
});
});
});
});

describe('DELETE', () => {
Expand Down
51 changes: 51 additions & 0 deletions app/shared/translationSchema.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import Ajv from 'ajv';
import { objectIdSchema } from 'shared/types/commonSchemas';
import { wrapValidator } from 'shared/tsUtils';
import { TranslationType } from './translationType';

export const emitSchemaTypes = true;

const ajv = Ajv({ allErrors: true, removeAdditional: true });

export const translationSchema = {
$schema: 'http://json-schema.org/schema#',
$async: true,
type: 'object',
additionalProperties: false,
title: 'TranslationType',
definitions: { objectIdSchema },
properties: {
_id: objectIdSchema,
locale: { type: 'string', minLength: 1 },
contexts: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
_id: objectIdSchema,
id: { type: 'string', minLength: 1 },
label: { type: 'string', minLength: 1 },
type: { type: 'string', minLength: 1 },
values: {
type: 'array',
items: {
type: 'object',
additionalProperties: false,
properties: {
_id: objectIdSchema,
key: { type: 'string', minLength: 1 },
value: { type: 'string', minLength: 1 },
},
},
},
},
},
},
},
};

const validate = wrapValidator(ajv.compile(translationSchema));

export const validateTranslation = async (translation: TranslationType): Promise<TranslationType> =>
validate({ ...translation });
20 changes: 20 additions & 0 deletions app/shared/translationType.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/* eslint-disable */
/**AUTO-GENERATED. RUN yarn emit-types to update.*/

import { ObjectIdSchema } from 'shared/types/commonTypes';

export interface TranslationType {
_id?: ObjectIdSchema;
locale?: string;
contexts?: {
_id?: ObjectIdSchema;
id?: string;
label?: string;
type?: string;
values?: {
_id?: ObjectIdSchema;
key?: string;
value?: string;
}[];
}[];
}