2022-11-24 18:03:50 +01:00
|
|
|
import { setupServer } from 'msw/node';
|
|
|
|
import { rest } from 'msw';
|
2023-01-20 16:01:18 +01:00
|
|
|
import { buildHistoryItem } from '../__fixtures__/settings';
|
|
|
|
import FixturesLib from '../__lib__/fixtures';
|
|
|
|
|
|
|
|
let fixtures = null;
|
2022-11-24 18:03:50 +01:00
|
|
|
|
2022-11-28 17:01:17 +01:00
|
|
|
export const server = setupServer(
|
2022-11-24 18:03:50 +01:00
|
|
|
rest.get('/api/groups', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.groups));
|
2022-11-24 18:03:50 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/plan_categories', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.planCategories));
|
2022-11-24 18:03:50 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/users', (req, res, ctx) => {
|
2022-11-29 10:48:23 +01:00
|
|
|
switch (new URLSearchParams(req.url.search).get('role')) {
|
|
|
|
case 'partner':
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.partners));
|
2022-11-29 10:48:23 +01:00
|
|
|
case 'manager':
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.managers));
|
2022-11-29 10:48:23 +01:00
|
|
|
default:
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.users));
|
2022-11-29 10:48:23 +01:00
|
|
|
}
|
2022-11-24 18:03:50 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/plans', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.plans));
|
2022-11-24 18:03:50 +01:00
|
|
|
}),
|
2022-11-29 10:48:23 +01:00
|
|
|
rest.post('/api/plans', (req, res, ctx) => {
|
|
|
|
return res(ctx.json(req.body));
|
|
|
|
}),
|
2023-01-26 10:04:47 +01:00
|
|
|
rest.put('/api/plans/:id', (req, res, ctx) => {
|
|
|
|
return res(ctx.json(req.body));
|
|
|
|
}),
|
2022-11-24 18:03:50 +01:00
|
|
|
rest.post('/api/users', (req, res, ctx) => {
|
|
|
|
/* eslint-disable camelcase */
|
|
|
|
const { user: { first_name, last_name, email } } = req.body;
|
|
|
|
return res(ctx.status(201), ctx.json({
|
|
|
|
id: Math.ceil(Math.random() * 100),
|
|
|
|
email,
|
|
|
|
profile_attributes: { first_name, last_name }
|
|
|
|
}));
|
|
|
|
/* eslint-enable camelcase */
|
|
|
|
}),
|
|
|
|
rest.get('/api/settings/:name', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
const setting = fixtures.settings.find(s => s.name === req.params.name);
|
2022-12-23 15:52:10 +01:00
|
|
|
const history = new URLSearchParams(req.url.search).get('history');
|
|
|
|
const result = { setting };
|
|
|
|
if (history) {
|
|
|
|
result.setting.history = [buildHistoryItem(setting)];
|
|
|
|
}
|
|
|
|
return res(ctx.json(result));
|
2022-11-24 18:03:50 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/settings', (req, res, ctx) => {
|
2022-12-20 16:47:46 +01:00
|
|
|
const names = new URLSearchParams(req.url.search).get('names');
|
2023-01-20 16:01:18 +01:00
|
|
|
const foundSettings = fixtures.settings.filter(setting => names.replace(/[[\]']/g, '').split(',').includes(setting.name));
|
2022-12-20 16:47:46 +01:00
|
|
|
return res(ctx.json(Object.fromEntries(foundSettings.map(s => [s.name, s.value]))));
|
2022-11-28 17:39:37 +01:00
|
|
|
}),
|
|
|
|
rest.patch('/api/settings/bulk_update', (req, res, ctx) => {
|
|
|
|
return res(ctx.json(req.body));
|
2022-12-05 17:09:42 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/product_categories', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.productCategories));
|
2022-12-05 17:09:42 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/products', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.products));
|
2022-12-05 17:09:42 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/products/:id/stock_movements', (req, res, ctx) => {
|
|
|
|
const { id } = req.params;
|
|
|
|
return res(ctx.json({
|
|
|
|
page: 1,
|
2023-01-20 16:01:18 +01:00
|
|
|
total_pages: Math.ceil(fixtures.productStockMovements.length / 10),
|
2022-12-05 17:09:42 +01:00
|
|
|
page_size: 10,
|
2023-01-20 16:01:18 +01:00
|
|
|
total_count: fixtures.productStockMovements.length,
|
|
|
|
data: fixtures.productStockMovements.filter(m => String(m.product_id) === id)
|
2022-12-05 17:09:42 +01:00
|
|
|
}));
|
|
|
|
}),
|
|
|
|
rest.get('/api/machines', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.machines));
|
2022-12-20 16:47:46 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/auth_providers/active', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.providers[0]));
|
2022-12-20 16:47:46 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/profile_custom_fields', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.profileCustomFields));
|
2022-12-20 16:47:46 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/members/current', (req, res, ctx) => {
|
|
|
|
return res(ctx.json(global.loggedUser));
|
2022-12-22 12:11:14 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/spaces', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.spaces));
|
2023-01-19 10:29:23 +01:00
|
|
|
}),
|
|
|
|
rest.get('/api/statuses', (req, res, ctx) => {
|
2023-01-20 16:01:18 +01:00
|
|
|
return res(ctx.json(fixtures.statuses));
|
|
|
|
}),
|
|
|
|
rest.delete('api/statuses/:id', (req, res, ctx) => {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
const statusIndex = fixtures.statuses.findIndex((status) => status.id === id);
|
|
|
|
fixtures.statuses.splice(statusIndex, 1);
|
|
|
|
return res(ctx.json());
|
|
|
|
}),
|
|
|
|
rest.patch('api/statuses/:id', async (req, res, ctx) => {
|
|
|
|
const id = parseInt(req.params.id);
|
|
|
|
const reqBody = await req.json();
|
|
|
|
const status = fixtures.statuses.find((status) => status.id === id);
|
|
|
|
status.label = reqBody.status.label;
|
|
|
|
return res(ctx.json(status));
|
|
|
|
}),
|
|
|
|
rest.post('/api/statuses', async (req, res, ctx) => {
|
|
|
|
const reqBody = await req.json();
|
|
|
|
const status = reqBody.status;
|
|
|
|
status.id = fixtures.statuses.length + 1;
|
|
|
|
fixtures.statuses.push(status);
|
|
|
|
return res(ctx.json({ status }));
|
2022-11-24 18:03:50 +01:00
|
|
|
})
|
|
|
|
);
|
|
|
|
|
2023-01-20 16:01:18 +01:00
|
|
|
beforeAll(() => {
|
|
|
|
server.listen();
|
|
|
|
fixtures = FixturesLib.init();
|
|
|
|
}
|
|
|
|
);
|
|
|
|
afterEach(() => {
|
|
|
|
server.resetHandlers();
|
|
|
|
fixtures = FixturesLib.init();
|
|
|
|
});
|
2022-11-24 18:03:50 +01:00
|
|
|
afterAll(() => server.close());
|