Skip to content

Commit 6c07005

Browse files
committed
Setup subscription plans
1 parent bcd0b89 commit 6c07005

File tree

3 files changed

+128
-0
lines changed

3 files changed

+128
-0
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import { get as _get, keyBy as _keyBy } from 'lodash'
2+
import moment from 'moment'
3+
import Plan from '../../_types/SubscriptionPlan'
4+
5+
const list: Plan[] = [
6+
{
7+
id: 1,
8+
name: 'Trial',
9+
price: 0,
10+
status: 'active',
11+
},
12+
{
13+
id: 2,
14+
name: 'Pro',
15+
price: 0,
16+
status: 'active',
17+
},
18+
{
19+
id: 2,
20+
name: 'Gold',
21+
price: 0,
22+
status: 'active',
23+
},
24+
]
25+
26+
const byId: { [key: number]: Plan } = _keyBy(list, 'id')
27+
28+
const subscriptionPlansData = {
29+
list,
30+
byId,
31+
current: byId[1],
32+
}
33+
34+
export default subscriptionPlansData

src-ts/_api/_mocks/plansMocks.ts

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
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
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
import Entity, { EntityId } from './Entity'
2+
3+
export type SubscriptionPlanId = EntityId
4+
5+
export interface SubscriptionPlanSubmissionData {
6+
name?: string
7+
price: number
8+
status?: 'active' | 'disabled'
9+
}
10+
11+
export default interface SubscriptionPlan extends SubscriptionPlanSubmissionData, Entity {
12+
id: SubscriptionPlanId
13+
}

0 commit comments

Comments
 (0)