|
| 1 | +import _ from 'lodash' |
| 2 | +import { rest } from 'msw' |
| 3 | + |
| 4 | +import config from '_config' |
| 5 | +import subscriptionPlansData from './_data/subscriptionPlansData' |
| 6 | + |
| 7 | +const apiUrl = config.api.url |
| 8 | + |
| 9 | +const userMocks = [ |
| 10 | + rest.get(`${apiUrl}/subscriptionPlans`, async (req, res, ctx) => { |
| 11 | + let limit = parseInt(req.params.get('limit') || '0') |
| 12 | + let offset = parseInt(req.params.get('offset') || '10') |
| 13 | + let order = JSON.parse(req.params.get('order') || '{}') |
| 14 | + |
| 15 | + const subscriptionPlans = order |
| 16 | + ? _.orderBy(subscriptionPlansData.list, [order.orderBy], [order.order]) |
| 17 | + : subscriptionPlansData.list |
| 18 | + |
| 19 | + const result = { |
| 20 | + subscriptionPlans: subscriptionPlans.slice(offset, offset + limit), |
| 21 | + count: subscriptionPlans.length, |
| 22 | + } |
| 23 | + |
| 24 | + return res( |
| 25 | + // Set custom status |
| 26 | + ctx.status(200), |
| 27 | + // Delay the response |
| 28 | + ctx.delay(500), |
| 29 | + // send JSON response body |
| 30 | + ctx.json(result), |
| 31 | + ) |
| 32 | + }), |
| 33 | + |
| 34 | + rest.post(`${apiUrl}/subscriptionPlans`, (req, res, ctx) => { |
| 35 | + return res( |
| 36 | + ctx.status(200), |
| 37 | + ctx.delay(200), |
| 38 | + ctx.json({ |
| 39 | + // @ts-ignore |
| 40 | + ...req.body, |
| 41 | + }), |
| 42 | + ) |
| 43 | + }), |
| 44 | + |
| 45 | + rest.put(`${apiUrl}/subscriptionPlans/:subscriptionPlanId`, (req, res, ctx) => { |
| 46 | + const { subscriptionPlanId } = req.params |
| 47 | + const user = subscriptionPlansData.byId[subscriptionPlanId] |
| 48 | + |
| 49 | + if (user) { |
| 50 | + return res( |
| 51 | + ctx.status(200), |
| 52 | + ctx.delay(200), |
| 53 | + ctx.json({ |
| 54 | + ...user, |
| 55 | + // ...req.body, |
| 56 | + }), |
| 57 | + ) |
| 58 | + } else { |
| 59 | + return res(ctx.status(403), ctx.json({ message: 'Update not permitted' })) |
| 60 | + } |
| 61 | + }), |
| 62 | + |
| 63 | + rest.delete(`${apiUrl}/subscriptionPlans/:subscriptionPlanId`, (req, res, ctx) => { |
| 64 | + const { subscriptionPlanId } = req.params |
| 65 | + const user = subscriptionPlansData.byId[subscriptionPlanId] |
| 66 | + |
| 67 | + if (user) { |
| 68 | + return res( |
| 69 | + ctx.status(200), |
| 70 | + ctx.delay(200), |
| 71 | + ctx.json({ |
| 72 | + message: 'User removed', |
| 73 | + }), |
| 74 | + ) |
| 75 | + } else { |
| 76 | + return res(ctx.status(403), ctx.json({ message: 'User not found or forbidden' })) |
| 77 | + } |
| 78 | + }), |
| 79 | +] |
| 80 | + |
| 81 | +export default userMocks |
0 commit comments