Files
brief_ai/frontend-BriefAI/src/services/apiService.ts
T
Nicolo-Salvafiorita 54ae86a88a feat: Implement subscription management and article saving features
- Added subscriptionPlan and subscriptionExpiresAt fields to UserProfile model.
- Enhanced profile routes to handle subscription state updates and expiration logic.
- Introduced email and password validation during user registration.
- Implemented link stripping for processed articles in the articles route.
- Added save article functionality with appropriate UI feedback in the frontend.
- Updated AccountSettings and FeedContent components to reflect subscription state and saved articles.
- Improved error handling and local storage management for user preferences.
2026-05-07 12:06:26 +02:00

82 lines
2.2 KiB
TypeScript

const BASE = import.meta.env.VITE_API_URL as string
import { getAuthHeader } from './authService'
export type ProfileResponse = {
success?: boolean
profile?: {
userId?: string
username?: string
email?: string
macroTopics?: string[]
keywords?: string[]
subscriptionPlan?: 'free' | 'pro'
subscriptionExpiresAt?: string | null
}
}
const authFetch = (path: string, options: RequestInit = {}) =>
fetch(`${BASE}${path}`, {
...options,
headers: { ...getAuthHeader(), ...options.headers },
})
// GET /api/stats/sentiment
export const fetchSentimentStats = () =>
authFetch('/api/stats/sentiment').then((r) => r.json())
// GET /api/stats/trending
export const fetchTrendingTopics = () =>
authFetch('/api/stats/trending').then((r) => r.json())
// GET /api/stats/categories
export const fetchCategoryStats = () =>
authFetch('/api/stats/categories').then((r) => r.json())
// GET /api/stats/sources
export const fetchSourceStats = () =>
authFetch('/api/stats/sources').then((r) => r.json())
// GET /api/stats/overview
export const fetchOverview = () =>
authFetch('/api/stats/overview').then((r) => r.json())
// GET /api/profile
export const fetchProfile = () =>
authFetch('/api/profile').then((r) => r.json() as Promise<ProfileResponse>)
// PUT /api/profile
export const updateProfile = async (data: {
userId?: string
macroTopics?: string[]
keywords?: string[]
subscriptionState?: 'free' | 'pro'
}) => {
// Sync to backend
const backendRes = await authFetch('/api/profile', {
method: 'PUT',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify(data),
}).then((r) => r.json());
// Opt-in sync to n8n if url is present and userId is known
const n8nUrl = import.meta.env.VITE_N8N_URL;
if (n8nUrl && data.userId) {
try {
await fetch(`${n8nUrl}/briefai/profile/update`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
userId: data.userId,
macroTopics: data.macroTopics,
keywords: data.keywords,
subscriptionState: data.subscriptionState,
})
});
} catch (e) {
console.warn("Failed to sync profile to n8n webhook", e);
}
}
return backendRes;
}