All files / src/common/services WordCountService.ts

85.36% Statements 70/82
72.72% Branches 16/22
100% Functions 5/5
85.36% Lines 70/82

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 1521x                                                             1x                   1x 14x 2x 2x   12x 12x 12x 12x   14x 1x 1x 1x   14x 14x 1x 1x 1x 14x         1x 2x 1x 1x   1x 1x 1x 1x   2x         2x 2x       2x         1x 2x 2x 2x 2x   2x 1x 1x 1x   1x 2x       2x         1x 2x 2x 2x 2x   2x 1x 1x 1x   1x 2x       2x           1x   2x 2x 2x 2x 2x   2x 2x   2x 2x 2x 2x 2x 1x  
import { supabase } from '../api/supabaseClient';
 
/**
 * WordCountService - CURRENTLY UNUSED
 * 
 * This service provides client-side word counting functionality for newsletters.
 * 
 * NOTE: This service is currently not used in the application. The application
 * uses database-level word counting through PostgreSQL functions in the
 * 20250130_consolidated_word_count_enhancements.sql migration.
 * 
 * The service could be used for:
 * - Client-side validation of word counts
 * - Debugging word count calculations
 * - Batch operations on existing newsletters
 * - Statistics and analytics
 * 
 * Database functions used instead:
 * - calculate_word_count(content)
 * - get_word_count_stats(user_id)
 * - update_newsletter_word_count(newsletter_id)
 */
 
export interface WordCountStats {
  total_newsletters: number;
  avg_word_count: number;
  median_word_count: number;
  min_word_count: number;
  max_word_count: number;
}
 
export class WordCountService {
  /**
   * WordCountService - CURRENTLY UNUSED
   * 
   * This class provides client-side word counting functionality but is not
   * currently used in the application. See file header comment for details.
   */
  /**
   * Calculate word count for newsletter content using the advanced database function
   */
  static async calculateWordCount(content: string | null): Promise<number> {
    if (!content) {
      return 0;
    }
 
    try {
      const { data, error } = await supabase.rpc('calculate_word_count', {
        content: content
      });
 
      if (error) {
        console.error('Error calculating word count:', error);
        return 0;
      }
 
      return data || 0;
    } catch (error) {
      console.error('Error in calculateWordCount:', error);
      return 0;
    }
  }
 
  /**
   * Update word counts for multiple newsletters in a batch
   */
  static async batchUpdateWordCounts(newsletterIds: string[]): Promise<number> {
    if (!newsletterIds.length) {
      return 0;
    }
 
    try {
      const { data, error } = await supabase.rpc('batch_update_newsletter_word_counts', {
        newsletter_ids: newsletterIds
      });
 
      if (error) {
        console.error('Error batch updating word counts:', error);
        return 0;
      }
 
      return data?.updated_count || 0;
    } catch (error) {
      console.error('Error in batchUpdateWordCounts:', error);
      return 0;
    }
  }
 
  /**
   * Get word count statistics for a user's newsletters
   */
  static async getWordCountStats(userId: string): Promise<WordCountStats | null> {
    try {
      const { data, error } = await supabase.rpc('get_word_count_stats', {
        p_user_id: userId
      });
 
      if (error) {
        console.error('Error getting word count stats:', error);
        return null;
      }
 
      return data;
    } catch (error) {
      console.error('Error in getWordCountStats:', error);
      return null;
    }
  }
 
  /**
   * Update word count for a single newsletter
   */
  static async updateWordCount(newsletterId: string): Promise<boolean> {
    try {
      const { error } = await supabase.rpc('update_newsletter_word_count', {
        p_newsletter_id: newsletterId
      });
 
      if (error) {
        console.error('Error updating word count:', error);
        return false;
      }
 
      return true;
    } catch (error) {
      console.error('Error in updateWordCount:', error);
      return false;
    }
  }
 
  /**
   * Validate word count calculation by comparing with a simple client-side calculation
   * (useful for debugging and testing)
   */
  static validateWordCount(content: string): { clientCount: number; serverCount: number } {
    // Simple client-side calculation for comparison
    const cleanContent = content
      .replace(/<[^>]*>/g, ' ') // Remove HTML tags
      .replace(/&[#a-zA-Z0-9]+;/g, ' ') // Remove HTML entities
      .replace(/\s+/g, ' ') // Normalize whitespace
      .trim();
 
    const words = cleanContent.split(/\s+/).filter(word => word.length > 0);
    const clientCount = words.length;
 
    return {
      clientCount,
      serverCount: -1 // Will be populated by the caller
    };
  }
}