Squashed commit of the following:
commit e4a56f5e8afd00382810babfca258d31dbb30a15
Author: arbetsformedlingen_garcn <christian.gardebrink@arbetsformedlingen.se>
Date: Mon Aug 16 02:48:58 2021 +0200
TV-338 lade in lite justeringar av mockAPI:et för att det ska gå att se om en användare har behörigheter eller inte i gränssnittet när man filtrerar listan
commit 52aacae80777358b2ffab8d03dc0664c974803dc
Merge: eacbb04 466abd1
Author: arbetsformedlingen_garcn <christian.gardebrink@arbetsformedlingen.se>
Date: Fri Aug 13 06:21:16 2021 +0200
Merge branch 'develop-remote' into feature/TV-338
commit eacbb04bf17b87970efa2cfd1f27372928c1ca93
Author: arbetsformedlingen_garcn <christian.gardebrink@arbetsformedlingen.se>
Date: Fri Aug 13 06:20:42 2021 +0200
TV-338 har lagt till en query-param i anropet för att hämta en lista av personal/anställda, får kolla med backend vad den bör heta. I formuläret finns den representerad som en checkbox.
131 lines
3.9 KiB
JavaScript
131 lines
3.9 KiB
JavaScript
import jsonServer from 'json-server';
|
|
|
|
const server = jsonServer.create();
|
|
const router = jsonServer.router('api.json');
|
|
const middlewares = jsonServer.defaults();
|
|
|
|
server.use(middlewares);
|
|
|
|
server.use(
|
|
jsonServer.rewriter({
|
|
'/api/*': '/$1',
|
|
'*sort=services*': '$1sort=services[0].name$2',
|
|
'*sort=organizations*': '$1sort=organizations[0].address.city$2',
|
|
'/employee*search=*': '/employee$1fullName_like=$2',
|
|
'/employee*onlyEmployeesWithoutAuthorization=*': '/employee$1authorizations.length_gte=1',
|
|
'/employee*': '/employees$1',
|
|
'/participants': '/participants?_embed=employees',
|
|
'/participant/:id': '/participants/:id?_embed=employees',
|
|
'/auth': '/currentUser',
|
|
'/customerinfo/*/*': '/deltagare/$2',
|
|
'/customerinfo': '/deltagare',
|
|
'/avrop/tjanster*': '/tjanster$1',
|
|
'/avrop/utforandeverksamheter*': '/organizations$1',
|
|
'/avrop/kommuner*': '/kommuner$1',
|
|
'/avrop/:sokandeId': '/avrop?sokandeId=:sokandeId',
|
|
'*page=*': '$1_page=$2',
|
|
'*limit=*': '$1_limit=$2',
|
|
'*sort=*': '$1_sort=$2',
|
|
'*order=*': '$1_order=$2',
|
|
'/auth/token?accessCode=auth_code_from_CIAM_with_all_permissions': '/getTokenFullAccess',
|
|
})
|
|
);
|
|
|
|
router.render = (req, res) => {
|
|
const method = req.originalMethod;
|
|
const parsedUrl = method === 'GET' ? req._parsedOriginalUrl : req._parsedUrl;
|
|
const pathname = parsedUrl.pathname;
|
|
const params = parsedUrl.query ? new URLSearchParams(parsedUrl.query) : null;
|
|
const requestHeaders = req.headers;
|
|
|
|
// all paths except /auth/token requires Authorization header.
|
|
if (!pathname.includes('/auth/token') && !requestHeaders.authorization) {
|
|
return res.status(401).jsonp({ error: 'No valid access-token' });
|
|
}
|
|
|
|
// Return custom error when status is 404.
|
|
if (res.statusCode === 404) {
|
|
return res.status(404).jsonp({ error: `Can't find path: ${pathname}` });
|
|
}
|
|
|
|
// Add createdAt to the body
|
|
if (method === 'POST') {
|
|
req.body.createdAt = Date.now();
|
|
}
|
|
|
|
if (pathname.includes('/auth/token')) {
|
|
res.jsonp(res.locals.data);
|
|
} else {
|
|
let data = res.locals.data;
|
|
const deltagareRegex = /(?:\/customerinfo\/)(contact|driverlicense|education\/highest|education|translator|work\/disability|work\/languages|work\/experience)/g;
|
|
const isDeltagarePath = deltagareRegex.exec(pathname);
|
|
const avropRegex = /(?:\/avrop\/)(tjanster|utforandeverksamheter|kommuner|\d)/g;
|
|
const isAvropPath = avropRegex.exec(pathname);
|
|
|
|
if (isDeltagarePath) {
|
|
const deltagareSubPath = getDeltagareSubPath(isDeltagarePath[1]);
|
|
data = res.locals.data[deltagareSubPath] || {};
|
|
}
|
|
if (isAvropPath) {
|
|
if (params) {
|
|
const newData = [];
|
|
params.forEach((value, key) => {
|
|
if (key === 'kommunCodes') {
|
|
value = +value;
|
|
}
|
|
|
|
newData.push(...data.filter(item => item[`related_${key}`].includes(value)));
|
|
});
|
|
|
|
data = newData.filter((value, index, arr) => arr.findIndex(item => item.code === value.code) === index);
|
|
} else if (isAvropPath[1]) {
|
|
data = data[0];
|
|
}
|
|
}
|
|
|
|
res.jsonp({
|
|
data,
|
|
...appendMetaData(params, res),
|
|
});
|
|
}
|
|
};
|
|
|
|
server.use(router);
|
|
server.listen(8000, () => {
|
|
console.info('JSON Server is running');
|
|
});
|
|
|
|
function appendMetaData(params, res) {
|
|
if (params && params.has('page')) {
|
|
const limit = +params.get('limit');
|
|
const page = +params.get('page');
|
|
const count = res.get('X-Total-Count');
|
|
const totalPages = Math.ceil(count / limit);
|
|
return {
|
|
meta: {
|
|
count,
|
|
limit,
|
|
page,
|
|
totalPages,
|
|
},
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function getDeltagareSubPath(path) {
|
|
switch (path) {
|
|
case 'education/highest':
|
|
return 'highestEducation';
|
|
case 'work/disability':
|
|
return 'disabilities';
|
|
case 'work/languages':
|
|
return 'workLanguages';
|
|
case 'work/experience':
|
|
return 'workExperiences';
|
|
default:
|
|
return path;
|
|
}
|
|
}
|