feat(config): make data structure horizontal
This commit is contained in:
@@ -3,19 +3,17 @@ import { renderObject } from '../../../../../helpers/renderer.js';
|
|||||||
import Config from '../../../../../models/config.js';
|
import Config from '../../../../../models/config.js';
|
||||||
|
|
||||||
export default async (request, response) => {
|
export default async (request, response) => {
|
||||||
const config = configParams(request);
|
const updatedConfig = await Config.update(configParams(request));
|
||||||
|
|
||||||
await Config.batchUpdate(config);
|
renderObject(response, updatedConfig);
|
||||||
|
|
||||||
renderObject(response, config);
|
|
||||||
};
|
};
|
||||||
|
|
||||||
const configParams = (request) => {
|
const configParams = (request) => {
|
||||||
const updatableConfigurationKeys = [
|
const updatableConfigurationKeys = [
|
||||||
'logo.svgData',
|
'logoSvgData',
|
||||||
'palette.primary.dark',
|
'palettePrimaryDark',
|
||||||
'palette.primary.light',
|
'palettePrimaryLight',
|
||||||
'palette.primary.main',
|
'palettePrimaryMain',
|
||||||
'title',
|
'title',
|
||||||
];
|
];
|
||||||
|
|
||||||
|
@@ -5,7 +5,7 @@ import app from '../../../../../app.js';
|
|||||||
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
|
import createAuthTokenByUserId from '../../../../../helpers/create-auth-token-by-user-id.js';
|
||||||
import { createUser } from '../../../../../../test/factories/user.js';
|
import { createUser } from '../../../../../../test/factories/user.js';
|
||||||
import { createRole } from '../../../../../../test/factories/role.js';
|
import { createRole } from '../../../../../../test/factories/role.js';
|
||||||
import { createBulkConfig } from '../../../../../../test/factories/config.js';
|
import { updateConfig } from '../../../../../../test/factories/config.js';
|
||||||
import * as license from '../../../../../helpers/license.ee.js';
|
import * as license from '../../../../../helpers/license.ee.js';
|
||||||
|
|
||||||
describe('PATCH /api/v1/admin/config', () => {
|
describe('PATCH /api/v1/admin/config', () => {
|
||||||
@@ -30,13 +30,13 @@ describe('PATCH /api/v1/admin/config', () => {
|
|||||||
|
|
||||||
const appConfig = {
|
const appConfig = {
|
||||||
title,
|
title,
|
||||||
'palette.primary.main': palettePrimaryMain,
|
palettePrimaryMain: palettePrimaryMain,
|
||||||
'palette.primary.dark': palettePrimaryDark,
|
palettePrimaryDark: palettePrimaryDark,
|
||||||
'palette.primary.light': palettePrimaryLight,
|
palettePrimaryLight: palettePrimaryLight,
|
||||||
'logo.svgData': logoSvgData,
|
logoSvgData: logoSvgData,
|
||||||
};
|
};
|
||||||
|
|
||||||
await createBulkConfig(appConfig);
|
await updateConfig(appConfig);
|
||||||
|
|
||||||
const newTitle = 'Updated title';
|
const newTitle = 'Updated title';
|
||||||
|
|
||||||
@@ -51,7 +51,7 @@ describe('PATCH /api/v1/admin/config', () => {
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
expect(response.body.data.title).toEqual(newTitle);
|
expect(response.body.data.title).toEqual(newTitle);
|
||||||
expect(response.body.meta.type).toEqual('Object');
|
expect(response.body.meta.type).toEqual('Config');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return created config for unexisting config', async () => {
|
it('should return created config for unexisting config', async () => {
|
||||||
@@ -68,7 +68,7 @@ describe('PATCH /api/v1/admin/config', () => {
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
expect(response.body.data.title).toEqual(newTitle);
|
expect(response.body.data.title).toEqual(newTitle);
|
||||||
expect(response.body.meta.type).toEqual('Object');
|
expect(response.body.meta.type).toEqual('Config');
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return null for deleted config entry', async () => {
|
it('should return null for deleted config entry', async () => {
|
||||||
@@ -83,6 +83,6 @@ describe('PATCH /api/v1/admin/config', () => {
|
|||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
expect(response.body.data.title).toBeNull();
|
expect(response.body.data.title).toBeNull();
|
||||||
expect(response.body.meta.type).toEqual('Object');
|
expect(response.body.meta.type).toEqual('Config');
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
@@ -3,7 +3,7 @@ import Config from '../../../../models/config.js';
|
|||||||
import { renderObject } from '../../../../helpers/renderer.js';
|
import { renderObject } from '../../../../helpers/renderer.js';
|
||||||
|
|
||||||
export default async (request, response) => {
|
export default async (request, response) => {
|
||||||
const defaultConfig = {
|
const staticConfig = {
|
||||||
disableNotificationsPage: appConfig.disableNotificationsPage,
|
disableNotificationsPage: appConfig.disableNotificationsPage,
|
||||||
disableFavicon: appConfig.disableFavicon,
|
disableFavicon: appConfig.disableFavicon,
|
||||||
additionalDrawerLink: appConfig.additionalDrawerLink,
|
additionalDrawerLink: appConfig.additionalDrawerLink,
|
||||||
@@ -11,15 +11,12 @@ export default async (request, response) => {
|
|||||||
additionalDrawerLinkText: appConfig.additionalDrawerLinkText,
|
additionalDrawerLinkText: appConfig.additionalDrawerLinkText,
|
||||||
};
|
};
|
||||||
|
|
||||||
let config = await Config.query().orderBy('key', 'asc');
|
const dynamicConfig = await Config.get();
|
||||||
|
|
||||||
config = config.reduce((computedConfig, configEntry) => {
|
const dynamicAndStaticConfig = {
|
||||||
const { key, value } = configEntry;
|
...dynamicConfig,
|
||||||
|
...staticConfig,
|
||||||
|
};
|
||||||
|
|
||||||
computedConfig[key] = value?.data;
|
renderObject(response, dynamicAndStaticConfig);
|
||||||
|
|
||||||
return computedConfig;
|
|
||||||
}, defaultConfig);
|
|
||||||
|
|
||||||
renderObject(response, config);
|
|
||||||
};
|
};
|
||||||
|
@@ -1,6 +1,6 @@
|
|||||||
import { vi, expect, describe, it } from 'vitest';
|
import { vi, expect, describe, it } from 'vitest';
|
||||||
import request from 'supertest';
|
import request from 'supertest';
|
||||||
import { createConfig } from '../../../../../test/factories/config.js';
|
import { updateConfig } from '../../../../../test/factories/config.js';
|
||||||
import app from '../../../../app.js';
|
import app from '../../../../app.js';
|
||||||
import configMock from '../../../../../test/mocks/rest/api/v1/automatisch/config.js';
|
import configMock from '../../../../../test/mocks/rest/api/v1/automatisch/config.js';
|
||||||
import * as license from '../../../../helpers/license.ee.js';
|
import * as license from '../../../../helpers/license.ee.js';
|
||||||
@@ -10,52 +10,35 @@ describe('GET /api/v1/automatisch/config', () => {
|
|||||||
it('should return Automatisch config', async () => {
|
it('should return Automatisch config', async () => {
|
||||||
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
|
vi.spyOn(license, 'hasValidLicense').mockResolvedValue(true);
|
||||||
|
|
||||||
const logoConfig = await createConfig({
|
const logoConfig = await updateConfig({
|
||||||
key: 'logo.svgData',
|
logoSvgData: '<svg>Sample</svg>',
|
||||||
value: { data: '<svg>Sample</svg>' },
|
palettePrimaryDark: '#001f52',
|
||||||
});
|
palettePrimrayLight: '#4286FF',
|
||||||
|
palettePrimaryMain: '#0059F7',
|
||||||
const primaryDarkConfig = await createConfig({
|
title: 'Sample Title',
|
||||||
key: 'palette.primary.dark',
|
|
||||||
value: { data: '#001F52' },
|
|
||||||
});
|
|
||||||
|
|
||||||
const primaryLightConfig = await createConfig({
|
|
||||||
key: 'palette.primary.light',
|
|
||||||
value: { data: '#4286FF' },
|
|
||||||
});
|
|
||||||
|
|
||||||
const primaryMainConfig = await createConfig({
|
|
||||||
key: 'palette.primary.main',
|
|
||||||
value: { data: '#0059F7' },
|
|
||||||
});
|
|
||||||
|
|
||||||
const titleConfig = await createConfig({
|
|
||||||
key: 'title',
|
|
||||||
value: { data: 'Sample Title' },
|
|
||||||
});
|
});
|
||||||
|
|
||||||
const response = await request(app)
|
const response = await request(app)
|
||||||
.get('/api/v1/automatisch/config')
|
.get('/api/v1/automatisch/config')
|
||||||
.expect(200);
|
.expect(200);
|
||||||
|
|
||||||
const expectedPayload = configMock(
|
const expectedPayload = configMock(logoConfig);
|
||||||
logoConfig,
|
|
||||||
primaryDarkConfig,
|
|
||||||
primaryLightConfig,
|
|
||||||
primaryMainConfig,
|
|
||||||
titleConfig
|
|
||||||
);
|
|
||||||
|
|
||||||
expect(response.body).toEqual(expectedPayload);
|
expect(response.body).toEqual(expectedPayload);
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should return additional environment variables', async () => {
|
it('should return additional environment variables', async () => {
|
||||||
vi.spyOn(appConfig, 'disableNotificationsPage', 'get').mockReturnValue(true);
|
vi.spyOn(appConfig, 'disableNotificationsPage', 'get').mockReturnValue(
|
||||||
|
true
|
||||||
|
);
|
||||||
vi.spyOn(appConfig, 'disableFavicon', 'get').mockReturnValue(true);
|
vi.spyOn(appConfig, 'disableFavicon', 'get').mockReturnValue(true);
|
||||||
vi.spyOn(appConfig, 'additionalDrawerLink', 'get').mockReturnValue('link');
|
vi.spyOn(appConfig, 'additionalDrawerLink', 'get').mockReturnValue('link');
|
||||||
vi.spyOn(appConfig, 'additionalDrawerLinkIcon', 'get').mockReturnValue('icon');
|
vi.spyOn(appConfig, 'additionalDrawerLinkIcon', 'get').mockReturnValue(
|
||||||
vi.spyOn(appConfig, 'additionalDrawerLinkText', 'get').mockReturnValue('text');
|
'icon'
|
||||||
|
);
|
||||||
|
vi.spyOn(appConfig, 'additionalDrawerLinkText', 'get').mockReturnValue(
|
||||||
|
'text'
|
||||||
|
);
|
||||||
|
|
||||||
expect(appConfig.disableNotificationsPage).toEqual(true);
|
expect(appConfig.disableNotificationsPage).toEqual(true);
|
||||||
expect(appConfig.disableFavicon).toEqual(true);
|
expect(appConfig.disableFavicon).toEqual(true);
|
||||||
|
@@ -5,7 +5,7 @@ import Config from '../../../../../models/config.js';
|
|||||||
import User from '../../../../../models/user.js';
|
import User from '../../../../../models/user.js';
|
||||||
import { createRole } from '../../../../../../test/factories/role';
|
import { createRole } from '../../../../../../test/factories/role';
|
||||||
import { createUser } from '../../../../../../test/factories/user';
|
import { createUser } from '../../../../../../test/factories/user';
|
||||||
import { createInstallationCompletedConfig } from '../../../../../../test/factories/config';
|
import { markInstallationCompleted } from '../../../../../../test/factories/config';
|
||||||
|
|
||||||
describe('POST /api/v1/installation/users', () => {
|
describe('POST /api/v1/installation/users', () => {
|
||||||
let adminRole;
|
let adminRole;
|
||||||
@@ -59,7 +59,7 @@ describe('POST /api/v1/installation/users', () => {
|
|||||||
|
|
||||||
describe('for completed installations', () => {
|
describe('for completed installations', () => {
|
||||||
beforeEach(async () => {
|
beforeEach(async () => {
|
||||||
await createInstallationCompletedConfig();
|
await markInstallationCompleted();
|
||||||
});
|
});
|
||||||
|
|
||||||
it('should respond with HTTP 403 when installation completed', async () => {
|
it('should respond with HTTP 403 when installation completed', async () => {
|
||||||
|
@@ -0,0 +1,102 @@
|
|||||||
|
function getValueForKey(rows, key) {
|
||||||
|
const row = rows.find((row) => row.key === key);
|
||||||
|
|
||||||
|
return row?.value?.data || null;
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function up(knex) {
|
||||||
|
await knex.schema.alterTable('config', (table) => {
|
||||||
|
table.dropPrimary();
|
||||||
|
table.dropUnique('key');
|
||||||
|
});
|
||||||
|
|
||||||
|
await knex.schema.renameTable('config', 'config_old');
|
||||||
|
|
||||||
|
await knex.schema.createTable('config', (table) => {
|
||||||
|
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'));
|
||||||
|
table.boolean('installation_completed').defaultTo(false);
|
||||||
|
table.text('logo_svg_data');
|
||||||
|
table.text('palette_primary_dark');
|
||||||
|
table.text('palette_primary_light');
|
||||||
|
table.text('palette_primary_main');
|
||||||
|
table.string('title');
|
||||||
|
|
||||||
|
table.timestamps(true, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
const oldConfig = await knex('config_old').select('key', 'value');
|
||||||
|
|
||||||
|
const singletonData = {
|
||||||
|
logo_svg_data: getValueForKey(oldConfig, 'logo.svgData'),
|
||||||
|
palette_primary_dark: getValueForKey(oldConfig, 'palette.primary.dark'),
|
||||||
|
palette_primary_light: getValueForKey(oldConfig, 'palette.primary.light'),
|
||||||
|
palette_primary_main: getValueForKey(oldConfig, 'palette.primary.main'),
|
||||||
|
title: getValueForKey(oldConfig, 'title'),
|
||||||
|
installation_completed: getValueForKey(oldConfig, 'installation.completed'),
|
||||||
|
};
|
||||||
|
|
||||||
|
await knex('config').insert(singletonData);
|
||||||
|
|
||||||
|
await knex.schema.dropTable('config_old');
|
||||||
|
}
|
||||||
|
|
||||||
|
export async function down(knex) {
|
||||||
|
await knex.schema.alterTable('config', (table) => {
|
||||||
|
table.dropPrimary();
|
||||||
|
});
|
||||||
|
|
||||||
|
await knex.schema.renameTable('config', 'config_old');
|
||||||
|
|
||||||
|
await knex.schema.createTable('config', (table) => {
|
||||||
|
table.uuid('id').primary().defaultTo(knex.raw('gen_random_uuid()'));
|
||||||
|
table.string('key').unique().notNullable();
|
||||||
|
table.jsonb('value').notNullable().defaultTo({});
|
||||||
|
|
||||||
|
table.timestamps(true, true);
|
||||||
|
});
|
||||||
|
|
||||||
|
const configRow = await knex('config_old').first();
|
||||||
|
|
||||||
|
const config = [
|
||||||
|
{
|
||||||
|
key: 'logo.svgData',
|
||||||
|
value: {
|
||||||
|
data: configRow.logo_svg_data,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'palette.primary.dark',
|
||||||
|
value: {
|
||||||
|
data: configRow.palette_primary_dark,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'palette.primary.light',
|
||||||
|
value: {
|
||||||
|
data: configRow.palette_primary_light,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'palette.primary.main',
|
||||||
|
value: {
|
||||||
|
data: configRow.palette_primary_main,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'title',
|
||||||
|
value: {
|
||||||
|
data: configRow.title,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
key: 'installation.completed',
|
||||||
|
value: {
|
||||||
|
data: configRow.installation_completed,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
];
|
||||||
|
|
||||||
|
await knex('config').insert(config);
|
||||||
|
|
||||||
|
await knex.schema.dropTable('config_old');
|
||||||
|
}
|
@@ -5,68 +5,47 @@ class Config extends Base {
|
|||||||
|
|
||||||
static jsonSchema = {
|
static jsonSchema = {
|
||||||
type: 'object',
|
type: 'object',
|
||||||
required: ['key', 'value'],
|
|
||||||
|
|
||||||
properties: {
|
properties: {
|
||||||
id: { type: 'string', format: 'uuid' },
|
id: { type: 'string', format: 'uuid' },
|
||||||
key: { type: 'string', minLength: 1 },
|
installationCompleted: { type: 'boolean' },
|
||||||
value: { type: 'object' },
|
logoSvgData: { type: ['string', 'null'] },
|
||||||
|
palettePrimaryDark: { type: ['string', 'null'] },
|
||||||
|
palettePrimaryLight: { type: ['string', 'null'] },
|
||||||
|
palettePrimaryMain: { type: ['string', 'null'] },
|
||||||
|
title: { type: ['string', 'null'] },
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
static async get() {
|
||||||
|
const existingConfig = await this.query().limit(1).first();
|
||||||
|
|
||||||
|
if (!existingConfig) {
|
||||||
|
return await this.query().insertAndFetch({});
|
||||||
|
}
|
||||||
|
|
||||||
|
return existingConfig;
|
||||||
|
}
|
||||||
|
|
||||||
|
static async update(config) {
|
||||||
|
const configEntry = await this.get();
|
||||||
|
|
||||||
|
return await configEntry.$query().patchAndFetch(config);
|
||||||
|
}
|
||||||
|
|
||||||
static async isInstallationCompleted() {
|
static async isInstallationCompleted() {
|
||||||
const installationCompletedEntry = await this.query()
|
const config = await this.get();
|
||||||
.where({
|
|
||||||
key: 'installation.completed',
|
|
||||||
})
|
|
||||||
.first();
|
|
||||||
|
|
||||||
const installationCompleted =
|
return config.installationCompleted;
|
||||||
installationCompletedEntry?.value?.data === true;
|
|
||||||
|
|
||||||
return installationCompleted;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static async markInstallationCompleted() {
|
static async markInstallationCompleted() {
|
||||||
return await this.query().insert({
|
const config = await this.get();
|
||||||
key: 'installation.completed',
|
|
||||||
value: {
|
return await config.$query().patchAndFetch({
|
||||||
data: true,
|
installationCompleted: true,
|
||||||
},
|
|
||||||
});
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
static async batchUpdate(config) {
|
|
||||||
const configKeys = Object.keys(config);
|
|
||||||
const updates = [];
|
|
||||||
|
|
||||||
for (const key of configKeys) {
|
|
||||||
const newValue = config[key];
|
|
||||||
|
|
||||||
if (newValue) {
|
|
||||||
const entryUpdate = Config.query()
|
|
||||||
.insert({
|
|
||||||
key,
|
|
||||||
value: {
|
|
||||||
data: newValue,
|
|
||||||
},
|
|
||||||
})
|
|
||||||
.onConflict('key')
|
|
||||||
.merge({
|
|
||||||
value: {
|
|
||||||
data: newValue,
|
|
||||||
},
|
|
||||||
});
|
|
||||||
|
|
||||||
updates.push(entryUpdate);
|
|
||||||
} else {
|
|
||||||
const entryUpdate = Config.query().findOne({ key }).delete();
|
|
||||||
updates.push(entryUpdate);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return await Promise.all(updates);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
export default Config;
|
export default Config;
|
||||||
|
@@ -1,35 +1,11 @@
|
|||||||
import { faker } from '@faker-js/faker';
|
|
||||||
import Config from '../../src/models/config';
|
import Config from '../../src/models/config';
|
||||||
|
|
||||||
export const createConfig = async (params = {}) => {
|
export const updateConfig = async (params = {}) => {
|
||||||
const configData = {
|
return await Config.update(params);
|
||||||
key: params?.key || faker.lorem.word(),
|
|
||||||
value: params?.value || { data: 'sampleConfig' },
|
|
||||||
};
|
|
||||||
|
|
||||||
const config = await Config.query().insertAndFetch(configData);
|
|
||||||
|
|
||||||
return config;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
export const createBulkConfig = async (params = {}) => {
|
export const markInstallationCompleted = async () => {
|
||||||
const updateQueries = Object.entries(params).map(([key, value]) => {
|
return await updateConfig({
|
||||||
const config = {
|
installationCompleted: true,
|
||||||
key,
|
|
||||||
value: { data: value },
|
|
||||||
};
|
|
||||||
|
|
||||||
return createConfig(config);
|
|
||||||
});
|
|
||||||
|
|
||||||
await Promise.all(updateQueries);
|
|
||||||
|
|
||||||
return await Config.query().whereIn('key', Object.keys(params));
|
|
||||||
};
|
|
||||||
|
|
||||||
export const createInstallationCompletedConfig = async () => {
|
|
||||||
return await createConfig({
|
|
||||||
key: 'installation.completed',
|
|
||||||
value: { data: true },
|
|
||||||
});
|
});
|
||||||
};
|
};
|
||||||
|
@@ -1,19 +1,13 @@
|
|||||||
const infoMock = (
|
const configMock = (config) => {
|
||||||
logoConfig,
|
|
||||||
primaryDarkConfig,
|
|
||||||
primaryLightConfig,
|
|
||||||
primaryMainConfig,
|
|
||||||
titleConfig
|
|
||||||
) => {
|
|
||||||
return {
|
return {
|
||||||
data: {
|
data: {
|
||||||
disableFavicon: false,
|
disableFavicon: false,
|
||||||
disableNotificationsPage: false,
|
disableNotificationsPage: false,
|
||||||
'logo.svgData': logoConfig.value.data,
|
logoSvgData: config.logoSvgData,
|
||||||
'palette.primary.dark': primaryDarkConfig.value.data,
|
palettePrimaryDark: config.palettePrimaryDark,
|
||||||
'palette.primary.light': primaryLightConfig.value.data,
|
palettePrimaryMain: config.palettePrimaryMain,
|
||||||
'palette.primary.main': primaryMainConfig.value.data,
|
palettePrimaryLight: config.palettePrimaryLight,
|
||||||
title: titleConfig.value.data,
|
title: config.title,
|
||||||
},
|
},
|
||||||
meta: {
|
meta: {
|
||||||
count: 1,
|
count: 1,
|
||||||
@@ -25,4 +19,4 @@ const infoMock = (
|
|||||||
};
|
};
|
||||||
};
|
};
|
||||||
|
|
||||||
export default infoMock;
|
export default configMock;
|
||||||
|
Reference in New Issue
Block a user