Squashed commit of the following: commit f4fe49b7e5602b7041573c70b85b22bd978d237b Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Thu Sep 16 18:01:24 2021 +0200 TV-407 cleanup commit 141cd9afa8d73e5ad4860456da926ad1468de874 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Thu Sep 16 16:32:07 2021 +0200 TV-407 updated heading text commit 2d778bd4dd7982e0736bc8c720a69499662a2164 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Thu Sep 16 14:33:04 2021 +0200 TV-407 fixed run test issues, some refactoring commit 9f4cc2c3ad2572eef80923ae465e4d2218acb0cc Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 18:51:58 2021 +0200 TV-407 removed afInvalid on timepicker commit f10923c92ebab2c51593682de4748fc443699c28 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 17:54:49 2021 +0200 TV 407 service name commit cdf02cd956e9f75d8415d525645a5d2ecbeb7e41 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 17:51:39 2021 +0200 TV-407 changed api service name to AvvikelseApiService commit 0ebaddc235ba506aec7cf23ebf96c03cb7a0a79f Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 16:51:02 2021 +0200 TV-407 renaming service name to DeltagareAvvikelseService commit 60e59881099c99a550b70e310930b1f708fa7f70 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 16:40:57 2021 +0200 TV-407 digi-ng timepicker, fixed some pr reviews comments commit b80e79e915d3baa11d7850ddf9ef506b4dd1d1cd Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 15 01:42:42 2021 +0200 TV-407 restructoring files, some cleaning commit 1c92ccefa2f3441c55fd4beacbf38b2ce465aee0 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Tue Sep 14 14:54:53 2021 +0200 TV-407 refactoring, cleaning commit 02904debb8f8dc70c817d2b18b79c0da1b9fd102 Merge: f1ad09a2a37566Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Tue Sep 14 11:51:31 2021 +0200 Merge branch 'develop' into feature/TV-407 commit f1ad09ac823a909b2d6503677314850235670838 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Tue Sep 14 11:50:50 2021 +0200 TV-407 updating report-api service commit 1f766fba089ac0e1f9bd3e00d2a7f81bb5b979e9 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Tue Sep 14 11:40:10 2021 +0200 TV-407 sorting franvaro, validating, cleaning commit f153e574007dfcfe7a65cbd5034370ed31202a34 Merge: ff4011c92272e4Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Mon Sep 13 14:42:38 2021 +0200 Merge branch 'develop' into feature/TV-407 commit ff4011c86f1222c677b2d027ef479b2f82aff5e3 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Mon Sep 13 13:58:05 2021 +0200 TV-407 structoring code to reactive style, validation implemnted commit 25f9debe6216a7bf0a3c6d9a59e6b4499699e380 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 8 22:46:33 2021 +0200 TV-407 changed file structure and renaming commit 9f6f6d4a9f63698d7912668db542eefe285a3b83 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 8 20:11:20 2021 +0200 TV-407 conditions fixes commit 29c1a9536f799795712ded15a3ac746b807f8f94 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 8 15:02:26 2021 +0200 TV-407 reactive error messages, sokandeId to model, cleaning commit ba7a031b3e253cb05ecd1a96920bc8439884dec1 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 8 09:17:16 2021 +0200 TV-407 setTimeout, timepicker condition commit 2eb2c48bc89bc310d2b2e9de4e909fbb5f3bdcb4 Author: fueno <nicolas.fuentes-maturana@arbetsformedlingen.se> Date: Wed Sep 8 09:03:41 2021 +0200 TV-407 corrections ... and 34 more commits
184 lines
5.6 KiB
JavaScript
184 lines
5.6 KiB
JavaScript
import commandLineArgs from 'command-line-args';
|
|
import pause from 'connect-pause';
|
|
import jsonServer from 'json-server';
|
|
|
|
const optionDefinitions = [
|
|
{
|
|
name: 'delay',
|
|
description: 'Add a delay in ms to the server. Defaults to 200.',
|
|
alias: 'd',
|
|
type: Number,
|
|
defaultValue: 200,
|
|
},
|
|
];
|
|
|
|
const options = commandLineArgs(optionDefinitions);
|
|
|
|
const server = jsonServer.create();
|
|
const router = jsonServer.router('api.json');
|
|
const middlewares = jsonServer.defaults();
|
|
|
|
server.use(pause(options.delay));
|
|
server.use(middlewares);
|
|
|
|
server.use(
|
|
jsonServer.rewriter({
|
|
'/api/*': '/$1',
|
|
'*sort=utforandeVerksamhet*': '$1sort=utforandeVerksamhet[0]$2',
|
|
'*sort=tjanst*': '$1sort=tjanst[0]$2',
|
|
'/users/invite*': '/invites$1',
|
|
'/employees*search=*': '/employees$1fullName_like=$2',
|
|
'/employees*onlyEmployeesWithoutAuthorization=*': '/employees$1roles.length_lte=1',
|
|
'/employees*': '/employees$1',
|
|
'/services*': '/tjanster$1',
|
|
'/participants': '/participants?_embed=employees',
|
|
'/participant/:id': '/participants/:id?_embed=employees',
|
|
'/auth/userinfo': '/userinfo',
|
|
'/auth/organizations': '/organizations',
|
|
'/users/currentUser': '/currentUser',
|
|
'/users*': '/employees$1',
|
|
'/avrop/tjanster*': '/avropTjanster$1',
|
|
'/avrop/handledare*': '/handledare$1',
|
|
'/avrop/utforandeverksamheter*': '/utforandeVerksamheter$1',
|
|
'/avrop/kommuner*': '/kommuner$1',
|
|
'/avrop*utforandeverksamhetId*': '/avrop$1utforandeVerksamhetId$2',
|
|
'/deltagare*onlyMyDeltagare=*': '/minaDeltagare',
|
|
'/avrop*tjanstKod*': '/avrop$1tjanstekod$2',
|
|
'/deltagare?*': '/avrop?$1',
|
|
'/deltagare/:sokandeId/avrop': '/avrop?sokandeId=:sokandeId',
|
|
'/deltagare/:sokandeId/*': '/deltagare/:sokandeId',
|
|
'/franvaro/franvaroOrsaker': '/franvaroOrsaker',
|
|
'/avvikelse/avvikelseOrsaker': '/avvikelseOrsaker',
|
|
'/andraKandaOrsaker': '/andraKandaOrsaker',
|
|
'/fragaForAvvikelseOrsaker': '/fragaForAvvikelseOrsaker',
|
|
'*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' });
|
|
}
|
|
|
|
if (method === 'PATCH') {
|
|
if (pathname === '/handledare/assign') {
|
|
// Returning status 204 as this is the same as in the API, BUT we're not actually updating the mock-api
|
|
return res.status(204).jsonp();
|
|
}
|
|
if (pathname === '/invites' && req.body?.emails) {
|
|
return res.status(200).jsonp({
|
|
data: {
|
|
invitedUsers: req.body.emails,
|
|
assignedUsers: [],
|
|
existingUsersInCurrentOrg: [],
|
|
failedInvite: [],
|
|
alreadyInvitedUsers: [],
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
// 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();
|
|
}
|
|
|
|
let data = res.locals.data;
|
|
const deltagareRegex = /(?:\/deltagare\/)(?:\d*\/)(contact|driverlicense|educationlevels\/highest|educations|translator|work\/disabilities|work\/languages|work\/experiences)/g;
|
|
const isDeltagarePath = deltagareRegex.exec(pathname);
|
|
const avropRegex = /(?:\/avrop\/(?:tjanster|utforandeverksamheter|kommuner|\d))|(?:\/deltagare\/\d\/(avrop))/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) => {
|
|
newData.push(
|
|
...data.filter(item => {
|
|
return 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];
|
|
}
|
|
}
|
|
|
|
if (pathname === '/deltagare') {
|
|
data = data.map(({ sokandeId, fornamn, efternamn, tjansteNamn, utforandeverksamhet, adress }) => ({
|
|
sokandeId: sokandeId.toString(),
|
|
fornamn,
|
|
efternamn,
|
|
tjanst: tjansteNamn,
|
|
utforandeVerksamhet: utforandeverksamhet,
|
|
utforandeAdress: adress,
|
|
}));
|
|
}
|
|
|
|
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 totalPage = Math.ceil(count / limit);
|
|
return {
|
|
meta: {
|
|
count,
|
|
limit,
|
|
page,
|
|
totalPage,
|
|
},
|
|
};
|
|
}
|
|
|
|
return null;
|
|
}
|
|
|
|
function getDeltagareSubPath(path) {
|
|
switch (path) {
|
|
case 'educationlevels/highest':
|
|
return 'highestEducation';
|
|
case 'work/disabilities':
|
|
return 'disabilities';
|
|
case 'work/languages':
|
|
return 'workLanguages';
|
|
case 'work/experiences':
|
|
return 'workExperiences';
|
|
default:
|
|
return path;
|
|
}
|
|
}
|