Skip to content

Commit

Permalink
v1.149.0
Browse files Browse the repository at this point in the history
  • Loading branch information
varovaro committed Jan 15, 2024
2 parents 89d5d14 + d26dac8 commit f6399c4
Show file tree
Hide file tree
Showing 87 changed files with 3,837 additions and 3,035 deletions.
41 changes: 41 additions & 0 deletions app/api/migrations/migrations/152-update_translations/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
const newKeys = [{ key: 'Updated' }, { key: 'No Group' }, { key: 'Using URLs' }];

const deletedKeys = [];

export default {
delta: 152,

reindex: false,

name: 'update_translations',

description: 'Updates some translations for new Menu UI in settings',

async up(db) {
const settings = await db.collection('settings').findOne();
const languages = settings.languages
.map(l => l.key)
.filter((value, index, array) => array.indexOf(value) === index);

await db.collection('translationsV2').deleteMany({
key: { $in: deletedKeys.concat(newKeys).map(k => k.key) },
'context.id': 'System',
});

const insertMany = languages.map(l =>
db.collection('translationsV2').insertMany(
newKeys.map(k => ({
key: k.key,
value: k.key,
language: l,
context: { id: 'System', type: 'Uwazi UI', label: 'User Interface' },
}))
)
);
await Promise.all(insertMany);

process.stdout.write(`${this.name}...\r\n`);
},
};

export { newKeys, deletedKeys };
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
import testingDB from 'api/utils/testing_db';
import migration, { newKeys, deletedKeys } from '../index.js';
import fixtures from './fixtures.js';

describe('migration update translations of settings new Users/Groups UI', () => {
beforeAll(async () => {
jest.spyOn(process.stdout, 'write').mockImplementation(() => {});
await testingDB.setupFixturesAndContext(fixtures);
await migration.up(testingDB.mongodb);
});

afterAll(async () => {
await testingDB.disconnect();
});

it('should have a delta number', () => {
expect(migration.delta).toBe(152);
});

it('should delete old translations', async () => {
const translations = await testingDB.mongodb
.collection('translationsV2')
.find({ key: { $in: deletedKeys.map(k => k.key) } })
.toArray();

expect(translations).toEqual([]);
});

it('should NOT delete other translations', async () => {
const translations = await testingDB.mongodb
.collection('translationsV2')
.find({ key: 'Im cool' })
.toArray();

expect(translations.length).toBe(2);
});

it('should add new translations per language', async () => {
const translations = await testingDB.mongodb
.collection('translationsV2')
.find({ key: { $in: newKeys.map(k => k.key) } })
.toArray();

expect(translations.length).toBe(6);
});

it('should be idempotent (do not throw an error on multiple runs)', async () => {
await expect(migration.up(testingDB.mongodb)).resolves.toBe(undefined);
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
import db from 'api/utils/testing_db';

const fixturesDB = {
settings: [{ _id: db.id(), languages: [{ key: 'en' }, { key: 'es' }] }],
translationsV2: [
{
_id: db.id(),
language: 'en',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Match / Label',
value: 'Match / Label',
},
{
_id: db.id(),
language: 'en',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Reviewing',
value: 'Reviewing',
},
{
_id: db.id(),
language: 'es',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Match / Label',
value: 'Match / Label',
},
{
_id: db.id(),
language: 'es',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Reviewing',
value: 'Reviewing',
},
{
_id: db.id(),
language: 'en',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Im cool',
value: 'Im cool',
},
{
_id: db.id(),
language: 'es',
context: { id: 'System', label: 'User Interface', type: 'Uwazi UI' },
key: 'Im cool',
value: 'Im cool',
},
],
};

export default fixturesDB;
21 changes: 18 additions & 3 deletions app/api/settings/routes.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,32 @@ import { Application } from 'express';
import needsAuthorization from '../auth/authMiddleware';

export default (app: Application) => {
app.get('/api/settings', (req, res, next) => {
const select = req.user && req.user.role === 'admin' ? '+publicFormDestination' : {};
settings
.get({}, select)
.then(response => res.json(response))
.catch(next);
});

app.post('/api/settings', needsAuthorization(), (req, res, next) => {
settings
.save(req.body)
.then(response => res.json(response))
.catch(next);
});

app.get('/api/settings', (req, res, next) => {
const select = req.user && req.user.role === 'admin' ? '+publicFormDestination' : {};
// eslint-disable-next-line @typescript-eslint/no-unused-vars
app.get('/api/settings/links', (req, res, next) => {
settings
.get({}, select)
.get()
.then(response => res.json(response.links))
.catch(next);
});

app.post('/api/settings/links', needsAuthorization(), (req, res, next) => {
settings
.save({ links: req.body })
.then(response => res.json(response))
.catch(next);
});
Expand Down
53 changes: 41 additions & 12 deletions app/api/settings/settings.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
import translations from 'api/i18n/translations';

import { Settings, SettingsLinkSchema, SettingsFilterSchema } from 'shared/types/settingsType';
import {
Settings,
SettingsLinkSchema,
SettingsFilterSchema,
SettingsSublinkSchema,
} from 'shared/types/settingsType';
import { ensure } from 'shared/tsUtils';
import templates from 'api/templates';
import { LanguageSchema, LatLonSchema, ObjectIdSchema } from 'shared/types/commonTypes';
Expand All @@ -12,30 +17,41 @@ import { settingsModel } from './settingsModel';

const DEFAULT_MAP_STARTING_POINT: LatLonSchema[] = [{ lon: 6, lat: 46 }];

const getUpdatesAndDeletes = (
matchProperty: keyof (SettingsLinkSchema & SettingsFilterSchema),
propertyName: keyof (SettingsLinkSchema & SettingsFilterSchema),
newValues: (SettingsLinkSchema & SettingsFilterSchema)[] = [],
currentValues: (SettingsLinkSchema & SettingsFilterSchema)[] = []
type FilterOrLink = SettingsFilterSchema | SettingsLinkSchema;

const isLink = (item: any): item is SettingsLinkSchema => item.type && item.title;

type SubLinkKeyType = keyof SettingsSublinkSchema;

const subLinkProperties: SubLinkKeyType[] = ['title' as 'title', 'url' as 'url'];

const isSubLinkKey = (key: string | number | symbol): key is SubLinkKeyType =>
subLinkProperties.includes(key as any);

const getUpdatesAndDeletes = <T extends FilterOrLink>(
matchProperty: keyof T,
propertyName: keyof T,
newValues: T[] = [],
currentValues: T[] = []
) => {
const updatedValues: { [k: string]: any } = {};
const deletedValues: string[] = [];

currentValues.forEach((value: SettingsFilterSchema & SettingsLinkSchema) => {
currentValues.forEach(value => {
const matchValue = newValues.find(
v => v[matchProperty] && v[matchProperty]?.toString() === value[matchProperty]?.toString()
);

if (value[propertyName] && matchValue && matchValue[propertyName] !== value[propertyName]) {
if (value.sublinks) {
if (isLink(value) && value.sublinks && isSubLinkKey(propertyName)) {
value.sublinks.forEach(sublink => {
updatedValues[ensure<string>(sublink[propertyName])] = sublink[propertyName];
});
}
updatedValues[ensure<string>(value[propertyName])] = matchValue[propertyName];
}
if (!matchValue) {
if (value.sublinks) {
if (isLink(value) && value.sublinks && isSubLinkKey(propertyName)) {
value.sublinks.forEach(sublink => {
if (sublink[propertyName]) {
deletedValues.push(ensure<string>(sublink[propertyName] as string));
Expand All @@ -48,9 +64,11 @@ const getUpdatesAndDeletes = (

const values = newValues.reduce<{ [k: string]: string }>((result, value) => {
const sublinkResults: { [key: string]: string | unknown } = {};
value.sublinks?.map(sublink => {
sublinkResults[ensure<string>(sublink[propertyName])] = sublink[propertyName];
});
if (isLink(value) && value.sublinks && isSubLinkKey(propertyName)) {
value.sublinks.forEach(sublink => {
sublinkResults[ensure<string>(sublink[propertyName])] = sublink[propertyName];
});
}
return {
...result,
[ensure<string>(value[propertyName])]: value[propertyName],
Expand Down Expand Up @@ -227,4 +245,15 @@ export default {

return this.save(settings);
},

async getLinks() {
const settings = await this.get();
return settings.links || [];
},

async saveLinks(links: Settings['links']) {
const currentSettings = await this.get();
const newSettings = { ...currentSettings, links };
return this.save(newSettings);
},
};
Loading

0 comments on commit f6399c4

Please sign in to comment.