All files / src/lib/api-docs/endpoints/admin analytics.ts

100% Statements 358/358
100% Branches 0/0
100% Functions 0/0
100% Lines 358/358

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 3591x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x 1x  
/**
 * Admin Analytics Endpoints
 * Business analytics, reports, and insights
 */
 
import type { ApiEndpoint } from '@/types/api-docs';
 
export const adminAnalyticsEndpoints: ApiEndpoint[] = [
  {
    id: 'admin-analytics-overview',
    method: 'GET',
    path: '/api/admin/analytics/overview',
    summary: 'Get analytics overview',
    description: 'Returns high-level business metrics and KPIs',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date (ISO 8601)' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date (ISO 8601)' },
      { name: 'compareWith', type: 'string', required: false, location: 'query', description: 'Comparison period', enum: ['previous_period', 'previous_year'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Analytics overview data',
        example: {
          success: true,
          data: {
            revenue: { current: 150000, previous: 120000, change: 25 },
            orders: { current: 1500, previous: 1200, change: 25 },
            customers: { current: 500, new: 150 },
            averageOrderValue: 100,
            conversionRate: 3.5,
          },
        },
      },
      { status: 403, description: 'Admin access required' },
    ],
  },
  {
    id: 'admin-analytics-revenue',
    method: 'GET',
    path: '/api/admin/analytics/revenue',
    summary: 'Get revenue analytics',
    description: 'Returns detailed revenue breakdown and trends',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
      { name: 'granularity', type: 'string', required: false, location: 'query', description: 'Data granularity', enum: ['daily', 'weekly', 'monthly'] },
      { name: 'groupBy', type: 'string', required: false, location: 'query', description: 'Group by dimension', enum: ['category', 'product', 'channel'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Revenue analytics',
        example: {
          success: true,
          data: {
            total: 150000,
            trend: [{ date: '2024-01-01', revenue: 5000, orders: 50 }],
            byCategory: [{ category: 'Electronics', revenue: 50000, percentage: 33 }],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-customers',
    method: 'GET',
    path: '/api/admin/analytics/customers',
    summary: 'Get customer analytics',
    description: 'Returns customer behavior and cohort analysis',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
    ],
    responses: [
      {
        status: 200,
        description: 'Customer analytics',
        example: {
          success: true,
          data: {
            totalCustomers: 5000,
            newCustomers: 500,
            returningCustomers: 1500,
            churnRate: 5,
            lifetimeValue: 250,
            cohortRetention: [],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-products',
    method: 'GET',
    path: '/api/admin/analytics/products',
    summary: 'Get product analytics',
    description: 'Returns product performance and inventory insights',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
      { name: 'limit', type: 'number', required: false, location: 'query', description: 'Number of top products' },
    ],
    responses: [
      {
        status: 200,
        description: 'Product analytics',
        example: {
          success: true,
          data: {
            topSelling: [{ id: 1, name: 'Product A', units: 500, revenue: 10000 }],
            lowStock: [{ id: 5, name: 'Product E', stock: 3 }],
            noSales: [{ id: 10, name: 'Product J', daysSinceLastSale: 30 }],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-traffic',
    method: 'GET',
    path: '/api/admin/analytics/traffic',
    summary: 'Get traffic analytics',
    description: 'Returns website traffic and visitor analytics',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
    ],
    responses: [
      {
        status: 200,
        description: 'Traffic analytics',
        example: {
          success: true,
          data: {
            pageViews: 100000,
            uniqueVisitors: 25000,
            sessions: 40000,
            bounceRate: 45,
            avgSessionDuration: 180,
            topPages: [{ path: '/products', views: 15000 }],
            trafficSources: [{ source: 'google', sessions: 15000, percentage: 37.5 }],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-export',
    method: 'POST',
    path: '/api/admin/analytics/export',
    summary: 'Export analytics data',
    description: 'Exports analytics data in various formats',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    requestBody: {
      contentType: 'application/json',
      fields: [
        { name: 'report', type: 'string', required: true, description: 'Report type', enum: ['revenue', 'customers', 'products', 'traffic'] },
        { name: 'format', type: 'string', required: true, description: 'Export format', enum: ['csv', 'xlsx', 'pdf'] },
        { name: 'startDate', type: 'string', required: true, description: 'Start date' },
        { name: 'endDate', type: 'string', required: true, description: 'End date' },
      ],
    },
    responses: [
      { status: 200, description: 'Export file or download URL' },
      { status: 400, description: 'Invalid export parameters' },
    ],
  },
  {
    id: 'admin-analytics-cohorts',
    method: 'GET',
    path: '/api/admin/analytics/cohorts',
    summary: 'Get cohort analysis',
    description: 'Returns customer cohort retention and behavior analysis',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date (ISO 8601)' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date (ISO 8601)' },
      { name: 'granularity', type: 'string', required: false, location: 'query', description: 'Cohort granularity', enum: ['daily', 'weekly', 'monthly'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Cohort analysis data',
        example: {
          success: true,
          data: {
            cohorts: [{ period: '2024-01', users: 500, retention: [100, 45, 30, 25] }],
          },
        },
      },
      { status: 403, description: 'Admin access required' },
    ],
  },
  {
    id: 'admin-analytics-funnels',
    method: 'GET',
    path: '/api/admin/analytics/funnels',
    summary: 'Get conversion funnel analytics',
    description: 'Returns conversion funnel data for user journey analysis',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
      { name: 'funnelType', type: 'string', required: false, location: 'query', description: 'Funnel type', enum: ['checkout', 'registration', 'product'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Funnel analytics data',
        example: {
          success: true,
          data: {
            steps: [
              { name: 'View Product', count: 10000, rate: 100 },
              { name: 'Add to Cart', count: 3000, rate: 30 },
              { name: 'Checkout', count: 1500, rate: 15 },
              { name: 'Purchase', count: 1000, rate: 10 },
            ],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-products-top',
    method: 'GET',
    path: '/api/admin/analytics/products/top',
    summary: 'Get top performing products',
    description: 'Returns ranked list of top performing products by revenue or units',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
      { name: 'metric', type: 'string', required: false, location: 'query', description: 'Ranking metric', enum: ['revenue', 'units', 'views'] },
      { name: 'limit', type: 'number', required: false, location: 'query', description: 'Number of products to return' },
    ],
    responses: [
      {
        status: 200,
        description: 'Top products data',
        example: {
          success: true,
          data: {
            products: [{ id: 1, name: 'Product A', revenue: 50000, units: 500, views: 10000 }],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-revenue-forecast',
    method: 'GET',
    path: '/api/admin/analytics/revenue/forecast',
    summary: 'Get revenue forecast',
    description: 'Returns projected revenue based on historical trends',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'periods', type: 'number', required: false, location: 'query', description: 'Number of periods to forecast' },
      { name: 'granularity', type: 'string', required: false, location: 'query', description: 'Forecast granularity', enum: ['daily', 'weekly', 'monthly'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Revenue forecast data',
        example: {
          success: true,
          data: {
            forecast: [{ period: '2024-02', projected: 160000, confidence: { low: 140000, high: 180000 } }],
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-revenue-trend',
    method: 'GET',
    path: '/api/admin/analytics/revenue/trend',
    summary: 'Get revenue trend analysis',
    description: 'Returns revenue trend data with growth rates and patterns',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
      { name: 'granularity', type: 'string', required: false, location: 'query', description: 'Data granularity', enum: ['daily', 'weekly', 'monthly'] },
    ],
    responses: [
      {
        status: 200,
        description: 'Revenue trend data',
        example: {
          success: true,
          data: {
            trend: [{ period: '2024-01', revenue: 150000, growth: 25 }],
            movingAverage: 145000,
            seasonality: { detected: true, pattern: 'weekly' },
          },
        },
      },
    ],
  },
  {
    id: 'admin-analytics-segments',
    method: 'GET',
    path: '/api/admin/analytics/segments',
    summary: 'Get segment analytics',
    description: 'Returns analytics broken down by customer segments',
    category: 'admin-analytics',
    requiresAuth: true,
    adminOnly: true,
    parameters: [
      { name: 'startDate', type: 'string', required: false, location: 'query', description: 'Start date' },
      { name: 'endDate', type: 'string', required: false, location: 'query', description: 'End date' },
    ],
    responses: [
      {
        status: 200,
        description: 'Segment analytics data',
        example: {
          success: true,
          data: {
            segments: [
              { id: 1, name: 'VIP Customers', revenue: 75000, orders: 300, avgOrderValue: 250 },
              { id: 2, name: 'New Customers', revenue: 25000, orders: 500, avgOrderValue: 50 },
            ],
          },
        },
      },
    ],
  },
];