change user membership without payment

This commit is contained in:
2025-05-23 10:35:25 +03:00
parent 2b902ef0c9
commit efccb591ff
6 changed files with 95 additions and 32 deletions

View File

@@ -18,8 +18,24 @@ const PricingCard: React.FC<PricingCardProps> = ({
const handlePlanChange = async () => {
try {
setIsLoading(true)
console.log('Changing plan to:', plan)
// const response = await changePlan(plan) -- тут обработка данных запроса на смену плана
const requestData = {
plan: plan,
}
const response = await fetch('/api/account/membership', {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
},
body: JSON.stringify(requestData),
})
if (!response.ok) {
const error = await response.json()
console.error('PricingCard - Server error:', error)
throw new Error(error.error || 'Ошибка при создании маршрута')
}
// обновляем данные в сторе
if (user) {

View File

@@ -15,7 +15,7 @@ const AdminPayments = () => {
useEffect(() => {
const fetchPlans = async () => {
try {
const response = await fetch('/api/account/get-plans')
const response = await fetch('/api/account/membership')
if (!response.ok) {
throw new Error('Failed to fetch plans')
}

View File

@@ -1,26 +0,0 @@
import { NextRequest } from 'next/server'
export async function GET(req: NextRequest) {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/plans/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
const error = await response.json()
console.error('API error:', error)
return new Response(JSON.stringify(error), { status: response.status })
}
const result = await response.json()
return new Response(JSON.stringify(result), { status: 200 })
} catch (error) {
console.error('Route handler error:', error)
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
})
}
}

View File

@@ -0,0 +1,69 @@
import { NextRequest } from 'next/server'
import { getServerSession } from 'next-auth'
import { authOptions } from '@/app/api/auth/[...nextauth]/route'
export async function GET(req: NextRequest) {
try {
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/plans/`, {
method: 'GET',
headers: {
'Content-Type': 'application/json',
},
})
if (!response.ok) {
const error = await response.json()
console.error('API error:', error)
return new Response(JSON.stringify(error), { status: response.status })
}
const result = await response.json()
return new Response(JSON.stringify(result), { status: 200 })
} catch (error) {
console.error('Route handler error:', error)
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
})
}
}
export async function PATCH(req: NextRequest) {
try {
const session = await getServerSession(authOptions)
if (!session) {
return new Response(JSON.stringify({ error: 'Unauthorized' }), {
status: 401,
})
}
const data = await req.json()
const requestBody = {
account_type: data.plan,
}
const response = await fetch(`${process.env.NEXT_PUBLIC_API_URL}/account/change_membership/`, {
method: 'PATCH',
headers: {
'Content-Type': 'application/json',
Authorization: `Bearer ${session.accessToken}`,
},
body: JSON.stringify(requestBody),
})
if (!response.ok) {
const error = await response.json()
console.error('API Route - Backend error:', error)
return new Response(JSON.stringify(error), { status: response.status })
}
const result = await response.json()
return new Response(JSON.stringify(result), { status: 200 })
} catch (error) {
console.error('API Route - Error:', error)
return new Response(JSON.stringify({ error: 'Internal Server Error' }), {
status: 500,
})
}
}