All files / src/common/api bulkOperations.ts

0% Statements 0/136
100% Branches 1/1
100% Functions 1/1
0% Lines 0/136

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                                                                                                                                                                                                                                                                                                                                                             
import { BatchResult, BulkUpdateNewsletterParams } from '../types/api';
import { logger } from '../utils/logger';
import { requireAuth, supabase } from './supabaseClient';
 
// Batch configuration for optimal performance
const BATCH_CONFIG = {
  MAX_BATCH_SIZE: 50, // Process 50 items at a time
  BATCH_DELAY: 100,    // 100ms delay between batches
  MAX_RETRIES: 2,     // Retry failed batches
  RETRY_DELAY: 500,   // 500ms delay for retries
};
 
// Utility function to split array into batches
function createBatches<T>(items: T[], batchSize: number): T[][] {
  const batches: T[][] = [];
  for (let i = 0; i < items.length; i += batchSize) {
    batches.push(items.slice(i, i + batchSize));
  }
  return batches;
}
 
// Utility function to add delay between operations
function delay(ms: number): Promise<void> {
  return new Promise(resolve => setTimeout(resolve, ms));
}
 
// Enhanced batch update with proper error handling and progress tracking
export async function bulkUpdateBatched(
  params: BulkUpdateNewsletterParams,
  onProgress?: (completed: number, total: number, currentBatch: number) => void
): Promise<BatchResult<unknown>> {
  const startTime = Date.now();
  const user = await requireAuth();
  const { ids, updates } = params;
 
  logger.info('Starting batched bulk update', {
    component: 'NewsletterApi',
    action: 'bulk_update_batched_start',
    metadata: {
      totalIds: ids.length,
      batchSize: BATCH_CONFIG.MAX_BATCH_SIZE,
      updates: Object.keys(updates),
    },
  });
 
  // Split IDs into manageable batches
  const idBatches = createBatches(ids, BATCH_CONFIG.MAX_BATCH_SIZE);
  const allResults: any[] = [];
  const allErrors: (Error | null)[] = [];
  let completedCount = 0;
 
  // Process each batch sequentially
  for (let batchIndex = 0; batchIndex < idBatches.length; batchIndex++) {
    const batch = idBatches[batchIndex];
    let retryCount = 0;
    let batchSuccess = false;
 
    // Retry logic for each batch
    while (!batchSuccess && retryCount <= BATCH_CONFIG.MAX_RETRIES) {
      try {
        logger.debug('Processing batch', {
          component: 'NewsletterApi',
          action: 'process_batch',
          metadata: {
            batchIndex: batchIndex + 1,
            totalBatches: idBatches.length,
            batchSize: batch.length,
            retryCount,
          },
        });
 
        const { data, error } = await supabase
          .from('newsletters')
          .update({
            ...updates,
            updated_at: new Date().toISOString(),
          })
          .in('id', batch)
          .eq('user_id', user.id)
          .select();
 
        if (error) {
          throw new Error(`Batch ${batchIndex + 1} failed: ${error.message}`);
        }
 
        // Process successful batch results
        const transformedResults = (data || []).map((item: unknown) => item);
 
        batch.forEach((id) => {
          const result = transformedResults.find((r: unknown) => (r as { id: string }).id === id);
          allResults.push(result || null);
          allErrors.push(result ? null : new Error('Newsletter not found or not updated'));
        });
 
        completedCount += batch.length;
        batchSuccess = true;
 
        // Report progress
        if (onProgress) {
          onProgress(completedCount, ids.length, batchIndex + 1);
        }
 
        logger.debug('Batch completed successfully', {
          component: 'NewsletterApi',
          action: 'batch_success',
          metadata: {
            batchIndex: batchIndex + 1,
            completedCount,
            successCount: transformedResults.length,
          },
        });
 
      } catch (error) {
        retryCount++;
 
        logger.warn('Batch failed, retrying', {
          component: 'NewsletterApi',
          action: 'batch_retry',
          metadata: {
            batchIndex: batchIndex + 1,
            retryCount,
            error: error instanceof Error ? error.message : 'Unknown error',
          },
        });
 
        if (retryCount > BATCH_CONFIG.MAX_RETRIES) {
          // Mark all items in this batch as failed
          batch.forEach(() => {
            allResults.push(null);
            allErrors.push(error instanceof Error ? error : new Error('Batch operation failed'));
          });
          completedCount += batch.length;
        } else {
          // Wait before retry
          await delay(BATCH_CONFIG.RETRY_DELAY);
        }
      }
    }
 
    // Add delay between batches to prevent overwhelming the database
    if (batchIndex < idBatches.length - 1) {
      await delay(BATCH_CONFIG.BATCH_DELAY);
    }
  }
 
  const duration = Date.now() - startTime;
  const successCount = allResults.filter((r) => r !== null).length;
  const errorCount = allErrors.filter((e) => e !== null).length;
 
  logger.info('Batched bulk update completed', {
    component: 'NewsletterApi',
    action: 'bulk_update_batched_complete',
    metadata: {
      totalIds: ids.length,
      successCount,
      errorCount,
      duration,
      averageTimePerItem: duration / ids.length,
    },
  });
 
  return {
    results: allResults,
    errors: allErrors,
    successCount,
    errorCount,
  };
}
 
// Helper function to transform newsletter response (moved from original file)
// Note: Using a simple identity transformation for now - replace with actual transformation logic
function _transformNewsletterResponse(data: unknown): unknown {
  return data; // Placeholder - use actual transformation from newsletterApi.ts
}