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.
This commit is contained in:
Nicolo-Salvafiorita
2026-05-07 11:56:14 +02:00
parent 249db76341
commit 54ae86a88a
18 changed files with 548 additions and 63 deletions
+5 -1
View File
@@ -9,6 +9,8 @@ export type ProfileResponse = {
email?: string
macroTopics?: string[]
keywords?: string[]
subscriptionPlan?: 'free' | 'pro'
subscriptionExpiresAt?: string | null
}
}
@@ -47,6 +49,7 @@ export const updateProfile = async (data: {
userId?: string
macroTopics?: string[]
keywords?: string[]
subscriptionState?: 'free' | 'pro'
}) => {
// Sync to backend
const backendRes = await authFetch('/api/profile', {
@@ -57,7 +60,7 @@ export const updateProfile = async (data: {
// 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 && data.macroTopics) {
if (n8nUrl && data.userId) {
try {
await fetch(`${n8nUrl}/briefai/profile/update`, {
method: 'POST',
@@ -66,6 +69,7 @@ export const updateProfile = async (data: {
userId: data.userId,
macroTopics: data.macroTopics,
keywords: data.keywords,
subscriptionState: data.subscriptionState,
})
});
} catch (e) {
+14 -3
View File
@@ -1,5 +1,5 @@
import type { Article } from '../types/article'
import { getAuthHeader, decodeToken } from './authService'
import { getAuthHeader, decodeToken, getMe } from './authService'
const N8N = import.meta.env.VITE_N8N_URL
@@ -9,8 +9,19 @@ export const fetchPersonalizedFeed = async (): Promise<Article[]> => {
if (!token) throw new Error('Non autenticato')
const payload = decodeToken(token)
const userId: string = payload?.userId
if (!userId) throw new Error('Token non valido')
let userId: string = payload?.userId
// Fallback: if token decoding failed, ask backend who we are
if (!userId) {
try {
const me = await getMe()
userId = me?.user?.userId
} catch (e) {
// ignore and let later check throw
}
}
if (!userId) throw new Error('Token non valido o utente non riconosciuto')
const res = await fetch(`${N8N}/briefai/feed`, {
method: 'POST',
@@ -46,3 +46,61 @@ export const sendFeedback = async (
clearTimeout(timeoutId)
}
}
export const saveArticle = async (articleId: string): Promise<void> => {
const token = localStorage.getItem('briefai_token')
if (!token) throw new Error('Token mancante')
if (!N8N) {
// Fallback: salva solo localmente se n8n non è configurato
console.warn('N8N_URL non configurato, salvataggio locale')
const saved = JSON.parse(localStorage.getItem('briefai_saved_articles') || '{}')
saved[articleId] = true
localStorage.setItem('briefai_saved_articles', JSON.stringify(saved))
return
}
let payload: { userId?: string } | null = null
try {
payload = JSON.parse(atob(token.split('.')[1]))
} catch {
throw new Error('Token non valido')
}
if (!payload?.userId) throw new Error('userId non presente nel token')
const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), FEEDBACK_TIMEOUT_MS)
try {
const response = await fetch(`${N8N}/briefai/save-article`, {
method: 'POST',
headers: { 'Content-Type': 'application/json' },
body: JSON.stringify({
userId: payload.userId,
articleId,
}),
signal: controller.signal,
})
if (!response.ok) {
console.error(`Save article HTTP ${response.status}`, response)
// Fallback locale se n8n fallisce
const saved = JSON.parse(localStorage.getItem('briefai_saved_articles') || '{}')
saved[articleId] = true
localStorage.setItem('briefai_saved_articles', JSON.stringify(saved))
return
}
} catch (error) {
console.error('Save article error:', error)
if (error instanceof Error && error.name === 'AbortError') {
throw new Error('Timeout save article')
}
// Fallback locale se errore di rete
const saved = JSON.parse(localStorage.getItem('briefai_saved_articles') || '{}')
saved[articleId] = true
localStorage.setItem('briefai_saved_articles', JSON.stringify(saved))
} finally {
clearTimeout(timeoutId)
}
}