Files
picpeak/frontend/src/services/admin.service.ts
T
Paul Nothaft a5b20ca3fe fix(events): server-side search/pagination to remove first-100 cap (#346)
Counters and search on Admin → Events were bounded to the first 100 rows
returned from /admin/events?page=1&limit=100, so on instances with more
events the totals were wrong and search couldn't find anything outside
that window. The dashboard's expiring list had the same first-100 issue.

Backend
- adminEvents.js: extend search to include customer_email so the column
  shown in the table is actually queryable.
- adminDashboard.js: add totalEvents to /dashboard/stats so the events
  page can render an accurate "All (N)" / Total Events counter without
  walking the full table on the client.

Frontend
- events.service.ts: getEvents() now accepts search + the full status
  enum (active|inactive|archived|draft|expiring); response type matches
  the actual {events, pagination} shape.
- admin.service.ts: DashboardStats gains totalEvents.
- EventsListPage.tsx: rewired around server-side pagination, status
  filter, and 300ms-debounced search; Prev/Next + range/page indicator
  below the table; placeholderData keeps the previous page visible
  during fetches; stat cards and "All (N)" pull from /dashboard/stats so
  totals stay accurate regardless of the visible page; archive/delete
  invalidates dashboard-stats so cards refresh.
- AdminDashboard.tsx: expiring list now fetches getEvents(1, 5,
  'expiring') directly instead of slicing the first 100 client-side. As
  a side effect the dashboard's "expiring" definition now matches the
  backend (was excluding events expiring within the next 24h).
2026-05-01 20:55:13 +02:00

157 lines
4.6 KiB
TypeScript

import { api } from '../config/api';
export interface DashboardStats {
activeEvents: number;
expiringEvents: number;
totalPhotos: number;
storageUsed: number;
totalViews: number;
totalDownloads: number;
viewsTrend: number;
downloadsTrend: number;
archivedEvents: number;
totalEvents: number;
}
export interface SystemHealth {
overall: 'healthy' | 'warning' | 'error';
services: {
database: 'healthy' | 'warning' | 'error';
email: 'healthy' | 'warning' | 'error';
storage: 'healthy' | 'warning' | 'error';
memory: 'healthy' | 'warning' | 'error';
};
details: {
emailQueue: {
pending: number;
processable: number;
stuck: number;
sent: number;
failed: number;
};
memory: {
total: number;
free: number;
used: number;
percentage: number;
};
};
}
export interface Activity {
id: number;
type: string;
actorType: string;
actorName: string;
eventName?: string;
metadata: Record<string, any>;
createdAt: string;
}
export interface AdminProfile {
id: number;
username: string;
email: string;
mustChangePassword?: boolean;
last_login?: string | null;
last_login_ip?: string | null;
created_at?: string;
updated_at?: string;
}
export interface AnalyticsData {
chartData: Array<{
date: string;
views: number;
downloads: number;
uniqueVisitors: number;
}>;
topGalleries: Array<{
event_name: string;
slug: string;
views: number;
downloads?: number;
uniqueVisitors?: number;
}>;
devices: {
desktop: number;
mobile: number;
tablet: number;
};
}
export const adminService = {
// Dashboard statistics
async getDashboardStats(): Promise<DashboardStats> {
const response = await api.get<DashboardStats>('/admin/dashboard/stats');
return response.data;
},
// Recent activity
async getRecentActivity(limit: number = 10): Promise<Activity[]> {
const response = await api.get<Activity[]>('/admin/dashboard/activity', {
params: { limit }
});
return response.data;
},
// Analytics data
async getAnalytics(days: number = 7): Promise<AnalyticsData> {
const response = await api.get<AnalyticsData>('/admin/dashboard/analytics', {
params: { days }
});
return response.data;
},
// System health check
async getSystemHealth(): Promise<SystemHealth> {
const response = await api.get<SystemHealth>('/admin/dashboard/health');
return response.data;
},
// Format activity message
formatActivityMessage(activity: Activity): string {
const messages: Record<string, string> = {
'event_created': `New event created: ${activity.eventName || 'Unknown'}`,
'photos_uploaded': `${activity.metadata.count || 0} photos uploaded to ${activity.eventName || 'Unknown'}`,
'event_archived': `Event archived: ${activity.eventName || 'Unknown'}`,
'archive_restored': `Archive restored: ${activity.eventName || 'Unknown'}`,
'archive_deleted': `Archive deleted: ${activity.metadata.event_name || 'Unknown'}`,
'archive_downloaded': `Archive downloaded: ${activity.eventName || 'Unknown'}`,
'email_config_updated': 'Email configuration updated',
'email_template_updated': `Email template updated: ${activity.metadata.template_key || ''}`,
'branding_updated': 'Branding settings updated',
'theme_updated': 'Theme settings updated',
'bulk_download': `${activity.metadata.photo_count || 0} photos downloaded from ${activity.eventName || 'Unknown'}`,
'gallery_password_entry': `Password entered for ${activity.eventName || 'Unknown'}`,
'expiration_warning_viewed': `Expiration warning viewed for ${activity.eventName || 'Unknown'}`
};
return messages[activity.type] || activity.type;
},
// Format bytes to human readable
formatBytes(bytes: number): string {
if (bytes === 0) return '0 Bytes';
const k = 1024;
const sizes = ['Bytes', 'KB', 'MB', 'GB', 'TB'];
const i = Math.floor(Math.log(bytes) / Math.log(k));
return parseFloat((bytes / Math.pow(k, i)).toFixed(2)) + ' ' + sizes[i];
},
// Change password
async changePassword(data: { currentPassword: string; newPassword: string }): Promise<void> {
await api.post('/admin/auth/change-password', data);
},
async getAdminProfile(): Promise<AdminProfile> {
const response = await api.get<AdminProfile>('/admin/auth/profile');
return response.data;
},
async updateAdminProfile(data: { username: string; email: string }): Promise<AdminProfile> {
const response = await api.put<{ user: AdminProfile }>('/admin/auth/profile', data);
return response.data.user;
}
};