Squashed commit of the following: commit 2602b9205ef86307fa7c85ecb6ce87cb51b71150 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Thu Sep 2 15:40:49 2021 +0200 Added error-handling commit 4a3f79948b992e5ad7278328957380215ad21ed7 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Thu Sep 2 14:48:54 2021 +0200 Removed some unused variables commit f8e69dce84fee2b1fe2a06d6f0960511c286e2b5 Merge: b155865b80bf22Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Thu Sep 2 14:39:44 2021 +0200 Merged develop and fixed conflicts commit b15586559dab669d652d3a44625bacae4620ed40 Author: Erik Tiekstra <erik.tiekstra@arbetsformedlingen.se> Date: Thu Sep 2 14:34:58 2021 +0200 Added separate component for employee deletion commit 1720b4954ebbc2868db6f0304fc57b0f3ff07216 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Tue Aug 31 07:50:56 2021 +0200 added the showing of errror- and succsess messages when employee is deleted (TV-352) commit 2556d53802249fff545a2d256a178479103108e7 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Mon Aug 30 13:33:22 2021 +0200 minor change (TV-352) commit 035dbbe67486392a7a9c656ac73103e938953b24 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Mon Aug 30 12:38:07 2021 +0200 added close and open methods for better handling of modal (TV-352) commit abc9ce0f8580ae1b9d784bc7591f095faeddcc33 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Mon Aug 30 12:27:07 2021 +0200 feat(edit-employee): added functionality to delete employee, and catch error if such exists commit 23603dd2d84b0e694a19c2131c9c842cf730d97b Merge: 56a4a6a02cf0f6Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Mon Aug 30 11:19:18 2021 +0200 Merge branch 'develop' into feature/TV-352-add-delete-employee-button commit 56a4a6a9c9195542f51578bd4ee937086ace8df8 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Fri Aug 27 12:58:23 2021 +0200 added the basic http-delete request method commit 2f61ff7d6ac2c42a7972fc88548d3d59172cbcc1 Author: Aden Hassan <aden.hassan@arbetsformedlingen.se> Date: Fri Aug 27 09:18:13 2021 +0200 added the basic html/css and functionality for deleting an employee from edit-page
181 lines
5.5 KiB
JavaScript
181 lines
5.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=utforandeVerksamhet*': '$1sort=utforandeVerksamhet[0]$2',
|
|
'*sort=tjanst*': '$1sort=tjanst[0]$2',
|
|
'/users/invite*': '/invites$1',
|
|
'/users*': '/employees$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': '/currentUser',
|
|
'/auth/organizations': '/currentUser',
|
|
'/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',
|
|
'*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: [],
|
|
},
|
|
});
|
|
}
|
|
}
|
|
|
|
// 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 (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;
|
|
}
|
|
}
|