Admin Panel (Next.js): - Dashboard with stats - Users list with relationships (watches/watched_by) - User detail pages - Deployments list and detail pages - Devices, Orders, Subscriptions pages - OTP-based admin authentication Backend Optimizations: - Fixed N+1 query problem in admin APIs - Added pagination support - Added .range() and count support to Supabase wrapper - Optimized batch queries with lookup maps Database: - Added migrations for schema evolution - New tables: push_tokens, notification_settings - Updated access model iOS Build Scripts: - build-ios.sh, clear-apple-cache.sh - EAS configuration updates 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
75 lines
2.2 KiB
JavaScript
75 lines
2.2 KiB
JavaScript
const API_URL = process.env.NEXT_PUBLIC_API_URL || 'https://wellnuo.smartlaunchhub.com';
|
|
|
|
export async function apiRequest(endpoint, options = {}) {
|
|
const token = typeof window !== 'undefined' ? localStorage.getItem('adminToken') : null;
|
|
|
|
const headers = {
|
|
'Content-Type': 'application/json',
|
|
...options.headers,
|
|
};
|
|
|
|
if (token) {
|
|
headers['Authorization'] = `Bearer ${token}`;
|
|
}
|
|
|
|
const res = await fetch(`${API_URL}${endpoint}`, {
|
|
...options,
|
|
headers,
|
|
});
|
|
|
|
if (res.status === 401) {
|
|
if (typeof window !== 'undefined') {
|
|
localStorage.removeItem('adminToken');
|
|
window.location.href = '/admin/login';
|
|
}
|
|
throw new Error('Unauthorized');
|
|
}
|
|
|
|
const data = await res.json();
|
|
|
|
if (!res.ok) {
|
|
throw new Error(data.error || 'Request failed');
|
|
}
|
|
|
|
return data;
|
|
}
|
|
|
|
// Auth
|
|
export const requestOTP = (email) =>
|
|
apiRequest('/api/auth/request-otp', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ email }),
|
|
});
|
|
|
|
export const verifyOTP = (email, code) =>
|
|
apiRequest('/api/auth/verify-otp', {
|
|
method: 'POST',
|
|
body: JSON.stringify({ email, code }),
|
|
});
|
|
|
|
export const getMe = () => apiRequest('/api/auth/me');
|
|
|
|
// Admin endpoints
|
|
export const getStats = () => apiRequest('/api/admin/stats');
|
|
export const getOrders = (status) => apiRequest(`/api/admin/orders${status ? `?status=${status}` : ''}`);
|
|
export const getOrder = (id) => apiRequest(`/api/admin/orders/${id}`);
|
|
export const updateOrder = (id, data) =>
|
|
apiRequest(`/api/admin/orders/${id}`, {
|
|
method: 'PATCH',
|
|
body: JSON.stringify(data),
|
|
});
|
|
|
|
export const getUsers = () => apiRequest('/api/admin/users');
|
|
export const getUser = (id) => apiRequest(`/api/admin/users/${id}`);
|
|
|
|
export const getBeneficiaries = () => apiRequest('/api/admin/beneficiaries');
|
|
export const getSubscriptions = () => apiRequest('/api/admin/subscriptions');
|
|
|
|
// Deployments
|
|
export const getDeployments = () => apiRequest('/api/admin/deployments');
|
|
export const getDeployment = (id) => apiRequest(`/api/admin/deployments/${id}`);
|
|
|
|
// Devices
|
|
export const getDevices = (wellId) => apiRequest(`/api/admin/devices${wellId ? `?well_id=${wellId}` : ''}`);
|
|
export const getDevice = (id) => apiRequest(`/api/admin/devices/${id}`);
|