|
1 | | -import { http, HttpHandler, HttpResponse } from 'msw' |
| 1 | +import { http, type HttpHandler, HttpResponse } from 'msw' |
| 2 | +import { getSubMetadata, setSubMetadata } from '../../Login' |
2 | 3 | import { subscriptionProducts } from '../../purchases/CartTab' |
3 | | -import { updateSubscriptionPage } from '../../purchases/SubscriptionsSubtab' |
4 | 4 |
|
5 | | -interface Subscription { |
6 | | - provider: 'stripe' | 'patreon' | 'paypal' |
7 | | - tier: 1 | 2 | 3 | 4 |
8 | | -} |
9 | | - |
10 | | -export let mockSubscription: Subscription | null = null |
11 | | - |
12 | | -function getSubscriptionTier(productId: string): number | null { |
| 5 | +function getSubscriptionTier (productId: string): number | null { |
13 | 6 | const product = subscriptionProducts.find((p) => p.id === productId) |
14 | 7 | return product?.tier ?? null |
15 | 8 | } |
16 | 9 |
|
17 | 10 | export const subscriptionHandlers: HttpHandler[] = [ |
18 | 11 | http.post('https://synergism.cc/paypal/subscriptions/create', async ({ request }) => { |
19 | | - try { |
20 | | - const url = new URL(request.url) |
21 | | - const productId = url.searchParams.get('product') |
22 | | - if (!productId) { |
23 | | - return HttpResponse.json({ error: 'Product ID is required' }, { status: 400 }) |
24 | | - } |
25 | | - const tier = getSubscriptionTier(productId) |
26 | | - if (tier === null) { |
27 | | - return HttpResponse.json({ error: 'Invalid product ID' }, { status: 400 }) |
28 | | - } |
29 | | - if (mockSubscription !== null) { |
30 | | - return HttpResponse.json({ error: 'User already has an active subscription' }, { status: 409 }) |
31 | | - } |
32 | | - |
33 | | - mockSubscription = { |
34 | | - provider: 'paypal', |
35 | | - tier: tier as 1 | 2 | 3 | 4 |
36 | | - } |
37 | | - |
38 | | - // TODO: This should really be done somewhere in the frontend |
39 | | - updateSubscriptionPage() |
40 | | - return HttpResponse.json({}, { status: 204 }) |
41 | | - } catch (error) { |
42 | | - console.error('Error creating subscription:', error) |
43 | | - return HttpResponse.json({ error: 'Failed to create subscription' }, { status: 500 }) |
| 12 | + const url = new URL(request.url) |
| 13 | + const productId = url.searchParams.get('product') |
| 14 | + if (!productId) { |
| 15 | + return HttpResponse.json({ error: 'Product ID is required' }, { status: 400 }) |
44 | 16 | } |
| 17 | + const tier = getSubscriptionTier(productId) |
| 18 | + if (tier === null) { |
| 19 | + return HttpResponse.json({ error: 'Invalid product ID' }, { status: 400 }) |
| 20 | + } |
| 21 | + |
| 22 | + const currentSub = getSubMetadata() |
| 23 | + |
| 24 | + if (currentSub !== null) { |
| 25 | + return HttpResponse.json({ error: 'User already has an active subscription' }, { status: 409 }) |
| 26 | + } |
| 27 | + |
| 28 | + setSubMetadata({ |
| 29 | + provider: 'paypal', |
| 30 | + tier |
| 31 | + }) |
| 32 | + |
| 33 | + return HttpResponse.json({ id: 'paypal-id' }) |
45 | 34 | }), |
46 | 35 |
|
47 | 36 | // Revise subscription |
48 | 37 | http.post('https://synergism.cc/paypal/subscriptions/revise', async ({ request }) => { |
49 | | - try { |
50 | | - // Extract product ID from URL query parameter (?product=...) |
51 | | - const url = new URL(request.url) |
52 | | - const productId = url.searchParams.get('key') |
53 | | - |
54 | | - if (!productId) { |
55 | | - return HttpResponse.json({ error: 'Product ID is required' }, { status: 400 }) |
56 | | - } |
57 | | - |
58 | | - const newTier = getSubscriptionTier(productId) |
59 | | - if (newTier === null) { |
60 | | - return HttpResponse.json({ error: 'Invalid product ID or tier' }, { status: 400 }) |
61 | | - } |
62 | | - |
63 | | - if (mockSubscription === null) { |
64 | | - return HttpResponse.json({ error: 'No active subscription found' }, { status: 404 }) |
65 | | - } |
66 | | - |
67 | | - mockSubscription = { |
68 | | - provider: 'paypal', |
69 | | - tier: newTier as 1 | 2 | 3 | 4, |
70 | | - } |
71 | | - |
72 | | - return HttpResponse.json({ success: true, mockSubscription }, { status: 204 }) |
73 | | - } catch (error) { |
74 | | - console.error('Error revising subscription:', error) |
75 | | - return HttpResponse.json({ error: 'Failed to revise subscription' }, { status: 500 }) |
| 38 | + // Extract product ID from URL query parameter (?product=...) |
| 39 | + const url = new URL(request.url) |
| 40 | + const productId = url.searchParams.get('product') |
| 41 | + |
| 42 | + if (!productId) { |
| 43 | + return HttpResponse.json({ error: 'Product ID is required' }, { status: 400 }) |
76 | 44 | } |
77 | | - }), |
78 | 45 |
|
79 | | - http.post('https://synergism.cc/paypal/subscriptions/cancel', async () => { |
80 | | - try { |
81 | | - if (mockSubscription === null) { |
82 | | - return HttpResponse.json({ error: 'No active subscription found' }, { status: 404 }) |
83 | | - } |
| 46 | + const newTier = getSubscriptionTier(productId) |
| 47 | + if (newTier === null) { |
| 48 | + return HttpResponse.json({ error: 'Invalid product ID or tier' }, { status: 400 }) |
| 49 | + } |
84 | 50 |
|
85 | | - mockSubscription = null |
| 51 | + const currentSub = getSubMetadata() |
86 | 52 |
|
87 | | - return HttpResponse.json({}, { status: 204 }) |
88 | | - } catch (error) { |
89 | | - console.error('Error cancelling subscription:', error) |
90 | | - return HttpResponse.json({ error: 'Failed to cancel subscription' }, { status: 500 }) |
| 53 | + if (currentSub === null) { |
| 54 | + return HttpResponse.json({ error: 'No active subscription found' }, { status: 404 }) |
91 | 55 | } |
| 56 | + |
| 57 | + setSubMetadata({ |
| 58 | + provider: 'paypal', |
| 59 | + tier: newTier |
| 60 | + }) |
| 61 | + |
| 62 | + return HttpResponse.json({ |
| 63 | + link: 'https://paypal.com/link/for/user/to/approve/change' |
| 64 | + }) |
92 | 65 | }), |
| 66 | + |
| 67 | + http.post('https://synergism.cc/paypal/subscriptions/cancel', async () => { |
| 68 | + const currentSub = getSubMetadata() |
| 69 | + |
| 70 | + if (currentSub === null) { |
| 71 | + return HttpResponse.json({ error: 'No active subscription found' }, { status: 404 }) |
| 72 | + } |
| 73 | + |
| 74 | + setSubMetadata(null) |
| 75 | + |
| 76 | + return new HttpResponse(null, { status: 204 }) |
| 77 | + }) |
93 | 78 | ] |
0 commit comments