-
Notifications
You must be signed in to change notification settings - Fork 312
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1205 from automatisch/white-labelling
feat: introduce dynamic configuration
- Loading branch information
Showing
18 changed files
with
296 additions
and
47 deletions.
There are no files selected for viewing
15 changes: 15 additions & 0 deletions
15
packages/backend/src/db/migrations/20230810124730_create_config.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import { Knex } from 'knex'; | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
return 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); | ||
}); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
return knex.schema.dropTable('config'); | ||
} |
30 changes: 30 additions & 0 deletions
30
packages/backend/src/db/migrations/20230810134714_seed_update_config_permissions_to_admin.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,30 @@ | ||
import { Knex } from 'knex'; | ||
|
||
const getPermissionForRole = ( | ||
roleId: string, | ||
subject: string, | ||
actions: string[] | ||
) => | ||
actions.map((action) => ({ | ||
role_id: roleId, | ||
subject, | ||
action, | ||
conditions: [], | ||
})); | ||
|
||
export async function up(knex: Knex): Promise<void> { | ||
const role = (await knex('roles') | ||
.first(['id', 'key']) | ||
.where({ key: 'admin' }) | ||
.limit(1)) as { id: string; key: string }; | ||
|
||
await knex('permissions').insert( | ||
getPermissionForRole(role.id, 'Config', [ | ||
'update', | ||
]) | ||
); | ||
} | ||
|
||
export async function down(knex: Knex): Promise<void> { | ||
await knex('permissions').where({ subject: 'Config' }).delete(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
import type { IJSONValue } from '@automatisch/types'; | ||
import Config from '../../models/config'; | ||
import Context from '../../types/express/context'; | ||
|
||
type Params = { | ||
input: { | ||
[index: string]: IJSONValue; | ||
}; | ||
}; | ||
|
||
const updateConfig = async (_parent: unknown, params: Params, context: Context) => { | ||
context.currentUser.can('update', 'Config'); | ||
|
||
const config = params.input; | ||
const configKeys = Object.keys(config); | ||
const updates = []; | ||
|
||
for (const key of configKeys) { | ||
const newValue = config[key]; | ||
|
||
const entryUpdate = Config | ||
.query() | ||
.insert({ | ||
key, | ||
value: { | ||
data: newValue | ||
} | ||
}) | ||
.onConflict('key') | ||
.merge({ | ||
value: { | ||
data: newValue | ||
} | ||
}); | ||
|
||
updates.push(entryUpdate); | ||
} | ||
|
||
await Promise.all(updates); | ||
|
||
return config; | ||
}; | ||
|
||
export default updateConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import Context from '../../types/express/context'; | ||
import Config from '../../models/config'; | ||
|
||
type Params = { | ||
keys: string[]; | ||
}; | ||
|
||
const getConfig = async ( | ||
_parent: unknown, | ||
params: Params, | ||
context: Context | ||
) => { | ||
const configQuery = Config | ||
.query(); | ||
|
||
if (Array.isArray(params.keys)) { | ||
configQuery.whereIn('key', params.keys); | ||
} | ||
|
||
const config = await configQuery; | ||
|
||
return config.reduce((computedConfig, configEntry) => { | ||
const { key, value } = configEntry; | ||
|
||
computedConfig[key] = value?.data; | ||
|
||
return computedConfig; | ||
}, {} as Record<string, unknown>); | ||
}; | ||
|
||
export default getConfig; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import { IJSONValue } from '@automatisch/types'; | ||
import Base from './base'; | ||
|
||
class Config extends Base { | ||
id!: string; | ||
key!: string; | ||
value!: { data: IJSONValue }; | ||
|
||
static tableName = 'config'; | ||
|
||
static jsonSchema = { | ||
type: 'object', | ||
required: ['key', 'value'], | ||
|
||
properties: { | ||
id: { type: 'string', format: 'uuid' }, | ||
key: { type: 'string', minLength: 1 }, | ||
value: { type: 'object' }, | ||
}, | ||
}; | ||
} | ||
|
||
export default Config; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
import useConfig from 'hooks/useConfig'; | ||
|
||
const CustomLogo = () => { | ||
const { config, loading } = useConfig(['logo.svgData']); | ||
|
||
if (loading || !config?.['logo.svgData']) return null; | ||
|
||
const logoSvgData = config['logo.svgData'] as string; | ||
|
||
return ( | ||
<img src={`data:image/svg+xml;utf8,${encodeURIComponent(logoSvgData)}`} /> | ||
); | ||
}; | ||
|
||
export default CustomLogo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
import Typography from '@mui/material/Typography'; | ||
import * as React from 'react'; | ||
import { FormattedMessage } from 'react-intl'; | ||
|
||
import CustomLogo from 'components/CustomLogo/index.ee'; | ||
import useConfig from 'hooks/useConfig'; | ||
|
||
const Logo = () => { | ||
const { config, loading } = useConfig(['logo.svgData']); | ||
|
||
const logoSvgData = config?.['logo.svgData'] as string; | ||
if (loading && !logoSvgData) return (<React.Fragment />); | ||
|
||
if (logoSvgData) return <CustomLogo />; | ||
|
||
return ( | ||
<Typography variant="h6" component="h1" noWrap> | ||
<FormattedMessage id="brandText" /> | ||
</Typography> | ||
); | ||
}; | ||
|
||
export default Logo; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
import { styled } from '@mui/material/styles'; | ||
import { Link as RouterLink } from 'react-router-dom'; | ||
|
||
export const Link = styled(RouterLink)(() => ({ | ||
textDecoration: 'none', | ||
color: 'inherit', | ||
display: 'inline-flex', | ||
})); |
Oops, something went wrong.