Skip to content

Commit

Permalink
Merge pull request #2178 from Inist-CNRS/2174-cannot-read-properties-…
Browse files Browse the repository at this point in the history
…of-undefined-reading-filter

avoid to break ithe app with a invalid config
  • Loading branch information
touv authored Oct 4, 2024
2 parents a35baea + 46a5681 commit b363268
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 22 deletions.
2 changes: 1 addition & 1 deletion src/api/controller/front.js
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ const getDefaultInitialState = (ctx, token, cookie, locale) => ({
token,
cookie,
},
breadcrumb: ctx.configTenant.front.breadcrumb,
breadcrumb: ctx.configTenant.breadcrumb,
menu: {
leftMenu: ctx.configTenant.leftMenu,
rightMenu: ctx.configTenant.rightMenu,
Expand Down
52 changes: 31 additions & 21 deletions src/api/controller/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Koa from 'koa';
import mount from 'koa-mount';
import route from 'koa-route';
import path from 'path';
import merge from 'lodash/merge';
import { simulatedLatency } from 'config';
import api from './api';
import front from './front';
Expand All @@ -15,6 +16,7 @@ import repositoryMiddleware, {
} from '../services/repositoryMiddleware';
import fs from 'fs';
import { DEFAULT_TENANT } from '../../common/tools/tenantTools';
import configTenantDefault from '../../../configTenant.json';

const app = new Koa();

Expand Down Expand Up @@ -77,33 +79,41 @@ app.use(async (ctx, next) => {
app.use(repositoryMiddleware);
const configTenantInstanceMiddleware = async (ctx, next) => {
const configTenant = await ctx.configTenantCollection.findLast();
if (!configTenant || !configTenant.front) {
await next();
return;
}

ctx.configTenant = configTenant;

ctx.configTenant.leftMenu = configTenant?.front.menu.filter(
({ position }) => position === 'left',
);
ctx.configTenant.rightMenu = configTenant?.front.menu.filter(
({ position }) => position === 'right',
);
ctx.configTenant.advancedMenu = configTenant?.front.menu.filter(
({ position }) =>
position === 'advanced' ||
position === 'top' ||
position === 'bottom',
);

ctx.configTenant.customRoutes = configTenant?.front.menu
ctx.configTenant = merge(configTenantDefault, configTenant || {});
ctx.configTenant.leftMenu = Array()
.concat(configTenant?.front?.menu)
.filter(Boolean)
.filter((item) => item.position)
.filter(({ position }) => position === 'left');
ctx.configTenant.rightMenu = Array()
.concat(configTenant?.front?.menu)
.filter(Boolean)
.filter((item) => item.position)
.filter(({ position }) => position === 'right');
ctx.configTenant.advancedMenu = Array()
.concat(configTenant?.front?.menu)
.filter(Boolean)
.filter((item) => item.position)
.filter(
({ position }) =>
position === 'advanced' ||
position === 'top' ||
position === 'bottom',
);
ctx.configTenant.customRoutes = Array()
.concat(configTenant?.front?.menu)
.filter(Boolean)
.filter((item) => item.role)
.filter(({ role }) => role === 'custom')
.map(({ link }) => link);

ctx.configTenant.advancedMenuButton =
configTenant?.front.advancedMenuButton;
configTenant?.front?.advancedMenuButton;

ctx.configTenant.breadcrumb = Array()
.concat(configTenant?.front?.breadcrumb)
.filter(Boolean);
await next();
};

Expand Down

0 comments on commit b363268

Please sign in to comment.