Squashed commit of the following:
commit 36959e892dea1213659ba26ceba797b81e1c7769
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Fri Aug 6 14:28:04 2021 +0200
Added relationships to the mock-api data
commit e2b86b43908952cd4c46af7f58885c6e34b7eb2e
Merge: ece070a 229fb83
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Fri Aug 6 10:08:37 2021 +0200
Merge branch 'develop' into feature/TV-288-avrop-mock-and-models
commit ece070ac9f04a3aa1984611371586b32c9d5a222
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Fri Aug 6 09:25:59 2021 +0200
Removed duplicates from avrop mock-data
commit 005acd29f452316f1337132cf4721e4b3a409097
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Thu Aug 5 14:53:03 2021 +0200
Added more mock-data
commit 23ce076912c5b9e7dfa9615d28654d8d09622775
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Thu Aug 5 12:04:37 2021 +0200
Added example api-call to avrop-api service
commit 01629a1e1089e38914825408991276b5e847a709
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Thu Aug 5 11:43:48 2021 +0200
Added mappings
commit b489c2edb99e07929b84309f784eb097d43f31cd
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Thu Aug 5 11:22:49 2021 +0200
Added avrop models and mock-data
commit fe3d61962115e8e5b8bac3bade1609c2ef6a9bac
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date: Wed Aug 4 15:16:33 2021 +0200
WIP
122 lines
3.5 KiB
JavaScript
122 lines
3.5 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*': '/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',
|
|
'*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 pathname = req._parsedOriginalUrl.pathname;
|
|
// all paths except /auth/token requires Authorization header.
|
|
if (!pathname.includes('/auth/token') && !req.headers.authorization) {
|
|
return res.status(401).jsonp({ error: 'No valid access-token' });
|
|
}
|
|
|
|
if (res.statusCode === 404) {
|
|
return res.status(404).jsonp({ error: `Can't find path: ${pathname}` });
|
|
}
|
|
|
|
const params = req._parsedUrl.query ? new URLSearchParams(req._parsedUrl.query) : null;
|
|
|
|
// Add createdAt to the body
|
|
if (req.originalMethod === '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)/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);
|
|
}
|
|
}
|
|
|
|
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;
|
|
}
|
|
}
|