Files
mina-sidor-fa-web/mock-api/mina-sidor-fa/server.js
Erik Tiekstra 1f26e80cb3 feat(avrop): Added additional functionality to avrop-flow. (TV-411)
Squashed commit of the following:

commit b174dd7480baa5e5e4b7f4bea4c9fc674d344c0c
Merge: e1f9d2d 0661d22
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Thu Aug 26 11:50:11 2021 +0200

    Merged develop and resolved conflicts

commit e1f9d2d49e279e704b760a3cbe45941cdcfb81d9
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Thu Aug 26 11:41:45 2021 +0200

    Now fetching handledare and patching through API

commit cc017fdc6eb5d9620399eee011341a0307fe5658
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Aug 25 16:02:20 2021 +0200

    Updated some functionality

commit 345712842c12af08dd4a956d0d2fdfd2592ab3de
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Aug 25 15:32:22 2021 +0200

    Implmented pagination and select all

commit 95f9be3fae6d3f3b258897be9b78f49442ee0747
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Aug 25 13:03:41 2021 +0200

    Renamed avrop-table instances to avrop-list and avrop-table-row to avrop-row

commit 5f1e11bff74c942e2c8b9e62892f043dc299f612
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Aug 25 12:54:56 2021 +0200

    Added some changes to mock-api related to parameter changes inside avrop

commit 71b199744a31b2a4b8bcaa6870094fd900851030
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Wed Aug 25 12:23:57 2021 +0200

    added qp to avrop requests

commit dc0e34b4971ddfd3d683d482502439b961df8852
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 16:02:31 2021 +0200

    Renamed multiple variablenames inside avrop-api

commit 8a4d5471cf637db7d90c6659c893f6841ec9c961
Merge: be9e9b3 50a83f7
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 13:57:50 2021 +0200

    Merged develop and fixed conflicts

commit be9e9b323aee76493b5035cd79f6058781ae4c1a
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 13:27:16 2021 +0200

    Moved around elements inside avrop component

commit 7ede2d00cd7ed105ef12be88e2ab788841329f00
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 09:34:57 2021 +0200

    Moved around some components and other files to match project-structure

commit 7d1396216de643388a5690f2fa2733f127623b6c
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 09:04:28 2021 +0200

    Fixed issues with utforande verksamheter model and mock-data

commit 22baca18c25bd4ce8dcc713e91126214882cf017
Merge: 4ba3c1c 59ce393
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 08:56:02 2021 +0200

    Merged develop and fixed conflicts

commit 4ba3c1ce9dac206602de9651a98aecfd5857a0e5
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 08:48:21 2021 +0200

    Fixed issues with tjanst model and mock-data

commit 50d8c698778fa64cedd4249f6852715d038b450c
Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se>
Date:   Tue Aug 24 08:40:25 2021 +0200

    Fixed issues with kommun-model and mock-data
2021-08-26 13:46:17 +02:00

175 lines
5.3 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=utforandeVerksamhet*': '$1sort=utforandeVerksamhet[0]$2',
'*sort=tjanst*': '$1sort=tjanst[0]$2',
'/users/:id': '/employees?ciamUserId=:id',
'/users*': '/employees$1',
'/employees*search=*': '/employees$1fullName_like=$2',
'/employees*onlyEmployeesWithoutAuthorization=*': '/employees$1roles.length_lte=1',
'/employees/invite': '/invites',
'/employees*': '/employees$1',
'/services*': '/tjanster$1',
'/participants': '/participants?_embed=employees',
'/participant/:id': '/participants/:id?_embed=employees',
'/auth/userinfo': '/currentUser',
'/auth/organizations': '/currentUser',
'/avrop/handledare/assign*': '/avrop$1',
'/avrop/tjanster*': '/avropTjanster$1',
'/avrop/handledare*': '/handledare$1',
'/avrop/utforandeverksamheter*': '/utforandeVerksamheter$1',
'/avrop/kommuner*': '/kommuner$1',
'/avrop*utforandeverksamhetId*': '/avrop$1utforandeVerksamhetId$2',
'/avrop*tjanstKod*': '/avrop$1tjanstekod$2',
'/deltagare?*': '/avrop?$1',
'/deltagare/:sokandeId/avrop': '/avrop?sokandeId=:sokandeId',
'/deltagare/:sokandeId/*': '/deltagare/: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' });
}
if (method === 'PATCH') {
// 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();
}
// 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 employeeRegex = /(?:\/users\/)(.*)/;
const isEmployeePath = employeeRegex.exec(pathname);
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);
const authRegex = /(?:\/auth\/)(userinfo|organizations)/g;
const isAuthPath = authRegex.exec(pathname);
if (isEmployeePath) {
data = data[0];
}
if (isAuthPath) {
const authSubPath = isAuthPath[1];
if (authSubPath === 'organizations') {
data = res.locals.data[authSubPath].map(organization => ({
name: organization.name,
organizationnumber: organization.organizationNumber,
}));
}
}
if (isDeltagarePath) {
const deltagareSubPath = getDeltagareSubPath(isDeltagarePath[1]);
data = res.locals.data[deltagareSubPath] || {};
}
if (isAvropPath) {
if (params) {
const newData = [];
params.forEach((value, key) => {
// if (key === 'kommunKod') {
// value = +value;
// }
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;
}
}