226 lines
5.1 KiB
TypeScript
226 lines
5.1 KiB
TypeScript
|
|
'use server';
|
||
|
|
|
||
|
|
import { cookies } from 'next/headers';
|
||
|
|
import type { SubscriptionApiData, UsageApiData, SubscriptionInfo } from './types';
|
||
|
|
import { transformApiToFrontend } from './utils';
|
||
|
|
|
||
|
|
// ===== API 헤더 생성 =====
|
||
|
|
async function getApiHeaders(): Promise<HeadersInit> {
|
||
|
|
const cookieStore = await cookies();
|
||
|
|
const token = cookieStore.get('access_token')?.value;
|
||
|
|
|
||
|
|
return {
|
||
|
|
'Accept': 'application/json',
|
||
|
|
'Content-Type': 'application/json',
|
||
|
|
'Authorization': token ? `Bearer ${token}` : '',
|
||
|
|
'X-API-KEY': process.env.API_KEY || '',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===== 현재 활성 구독 조회 =====
|
||
|
|
export async function getCurrentSubscription(): Promise<{
|
||
|
|
success: boolean;
|
||
|
|
data: SubscriptionApiData | null;
|
||
|
|
error?: string;
|
||
|
|
}> {
|
||
|
|
try {
|
||
|
|
const headers = await getApiHeaders();
|
||
|
|
|
||
|
|
const response = await fetch(
|
||
|
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/current`,
|
||
|
|
{
|
||
|
|
method: 'GET',
|
||
|
|
headers,
|
||
|
|
cache: 'no-store',
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok || !result.success) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: result.message || '구독 정보를 불러오는데 실패했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: result.data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[SubscriptionActions] getCurrentSubscription error:', error);
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: '서버 오류가 발생했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===== 사용량 조회 =====
|
||
|
|
export async function getUsage(): Promise<{
|
||
|
|
success: boolean;
|
||
|
|
data: UsageApiData | null;
|
||
|
|
error?: string;
|
||
|
|
}> {
|
||
|
|
try {
|
||
|
|
const headers = await getApiHeaders();
|
||
|
|
|
||
|
|
const response = await fetch(
|
||
|
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/usage`,
|
||
|
|
{
|
||
|
|
method: 'GET',
|
||
|
|
headers,
|
||
|
|
cache: 'no-store',
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok || !result.success) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: result.message || '사용량 정보를 불러오는데 실패했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: result.data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[SubscriptionActions] getUsage error:', error);
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: '서버 오류가 발생했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===== 구독 취소 =====
|
||
|
|
export async function cancelSubscription(
|
||
|
|
id: number,
|
||
|
|
reason?: string
|
||
|
|
): Promise<{
|
||
|
|
success: boolean;
|
||
|
|
error?: string;
|
||
|
|
}> {
|
||
|
|
try {
|
||
|
|
const headers = await getApiHeaders();
|
||
|
|
|
||
|
|
const response = await fetch(
|
||
|
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/${id}/cancel`,
|
||
|
|
{
|
||
|
|
method: 'POST',
|
||
|
|
headers,
|
||
|
|
body: JSON.stringify({ reason }),
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok || !result.success) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: result.message || '구독 취소에 실패했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return { success: true };
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[SubscriptionActions] cancelSubscription error:', error);
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: '서버 오류가 발생했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===== 데이터 내보내기 요청 =====
|
||
|
|
export async function requestDataExport(
|
||
|
|
exportType: string = 'all'
|
||
|
|
): Promise<{
|
||
|
|
success: boolean;
|
||
|
|
data?: { id: number; status: string };
|
||
|
|
error?: string;
|
||
|
|
}> {
|
||
|
|
try {
|
||
|
|
const headers = await getApiHeaders();
|
||
|
|
|
||
|
|
const response = await fetch(
|
||
|
|
`${process.env.NEXT_PUBLIC_API_URL}/api/v1/subscriptions/export`,
|
||
|
|
{
|
||
|
|
method: 'POST',
|
||
|
|
headers,
|
||
|
|
body: JSON.stringify({ export_type: exportType }),
|
||
|
|
}
|
||
|
|
);
|
||
|
|
|
||
|
|
const result = await response.json();
|
||
|
|
|
||
|
|
if (!response.ok || !result.success) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: result.message || '내보내기 요청에 실패했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data: {
|
||
|
|
id: result.data.id,
|
||
|
|
status: result.data.status,
|
||
|
|
},
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[SubscriptionActions] requestDataExport error:', error);
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
error: '서버 오류가 발생했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
// ===== 통합 데이터 조회 (현재 구독 + 사용량) =====
|
||
|
|
export async function getSubscriptionData(): Promise<{
|
||
|
|
success: boolean;
|
||
|
|
data: SubscriptionInfo | null;
|
||
|
|
error?: string;
|
||
|
|
}> {
|
||
|
|
try {
|
||
|
|
const [subscriptionResult, usageResult] = await Promise.all([
|
||
|
|
getCurrentSubscription(),
|
||
|
|
getUsage(),
|
||
|
|
]);
|
||
|
|
|
||
|
|
if (!subscriptionResult.success && !usageResult.success) {
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: subscriptionResult.error || usageResult.error,
|
||
|
|
};
|
||
|
|
}
|
||
|
|
|
||
|
|
const data = transformApiToFrontend(
|
||
|
|
subscriptionResult.data,
|
||
|
|
usageResult.data
|
||
|
|
);
|
||
|
|
|
||
|
|
return {
|
||
|
|
success: true,
|
||
|
|
data,
|
||
|
|
};
|
||
|
|
} catch (error) {
|
||
|
|
console.error('[SubscriptionActions] getSubscriptionData error:', error);
|
||
|
|
return {
|
||
|
|
success: false,
|
||
|
|
data: null,
|
||
|
|
error: '서버 오류가 발생했습니다.',
|
||
|
|
};
|
||
|
|
}
|
||
|
|
}
|