Squashed commit of the following: commit f67c9cd63e51bdc84a7c9da4c66b413213c33117 Merge: 84f00ea 0753d39 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 1 12:00:15 2021 +0200 Merge branch 'feature/TV-405' of ssh://bitbucket.arbetsformedlingen.se:7999/tea/dafa-web-monorepo into feature/TV-405 commit 84f00eab3e211a5102d7abc9902be523f2e5b450 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 1 11:58:37 2021 +0200 Updated mock-api and removed some unused code commit 0753d39b7bd51163ddf1d2cf91f39a84321e8bb2 Merge: 47c363fa4bc9aaAuthor: Cecilia Varnava <cecilia.varnava@arbetsformedlingen.se> Date: Wed Sep 1 11:46:37 2021 +0200 Merge branch 'develop' into feature/TV-405 commit 14164ca7b01c0954d6f3efd441fef124a564edec Merge: 47c363fa4bc9aaAuthor: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Wed Sep 1 10:33:10 2021 +0200 Merge branch 'develop' into feature/TV-405 commit 47c363f6b984d1c46ed2f2bb054b3e541aa01cc3 Author: Cecilia Varnava <cecilia.varnava@arbetsformedlingen.se> Date: Sun Aug 29 19:14:43 2021 +0200 TV-405 cleanup commit 241d67de57ffc5d83b1dfd8e00de9cad7a6b985a Merge: 40f30ffeee20a3Author: Cecilia Varnava <cecilia.varnava@arbetsformedlingen.se> Date: Sun Aug 29 18:54:55 2021 +0200 Merge branch 'develop' into feature/TV-405 # Conflicts: # apps/mina-sidor-fa/src/app/shared/services/api/deltagare.service.ts # mock-api/mina-sidor-fa/server.js commit 40f30ff385022dcac88a8cd3046d01bcd92760bf Author: Cecilia Varnava <cecilia.varnava@arbetsformedlingen.se> Date: Sun Aug 29 18:45:37 2021 +0200 TV-405 temporary empty 'mina deltagare' in mockapi until assign handledare is implemented commit 9bdeefc57dcb7d409abb1d7fe88865392a1b7516 Author: Cecilia Varnava <cecilia.varnava@arbetsformedlingen.se> Date: Wed Aug 25 16:24:08 2021 +0200 TV-405 filter mina deltagare
177 lines
5.4 KiB
JavaScript
177 lines
5.4 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',
|
|
'/deltagare*onlyMyDeltagare=*': '/minaDeltagare',
|
|
'/avrop*tjanstKod*': '/avrop$1tjanstekod$2',
|
|
'/deltagare?*': '/avrop?$1',
|
|
'/deltagare/:sokandeId/avrop': '/avrop?sokandeId=:sokandeId',
|
|
'/deltagare/:sokandeId/*': '/deltagare/:sokandeId',
|
|
'/employees/invite': '/invites',
|
|
'*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;
|
|
}
|
|
}
|