All files / src/modules/recommendations/services collaborative-filtering.service.ts

0% Statements 0/33
0% Branches 0/7
0% Functions 0/8
0% Lines 0/25

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                                                                                                                                                                                                                                 
import { Injectable, Logger } from '@nestjs/common';
import { InjectModel } from '@nestjs/mongoose';
import { Model } from 'mongoose';
import { User, UserDocument } from '../../../database/schemas/user.schema';
import { UserInteraction, UserInteractionDocument } from '../../../database/schemas/user-interaction.schema';
 
@Injectable()
export class CollaborativeFilteringService {
  private readonly logger = new Logger(CollaborativeFilteringService.name);
 
  constructor(
    @InjectModel(User.name) private userModel: Model<UserDocument>,
    @InjectModel(UserInteraction.name) private interactionModel: Model<UserInteractionDocument>,
  ) {}
 
  async getRecommendations(userId: string, options: {
    limit?: number;
    categories?: string[];
  } = {}): Promise<Array<{ productId: string; score: number }>> {
    try {
      // Find similar users based on interaction patterns
      const similarUsers = await this.findSimilarUsers(userId);
      
      // Get products liked by similar users but not by current user
      const recommendations = await this.getProductsFromSimilarUsers(userId, similarUsers, options);
      
      return recommendations.slice(0, options.limit || 20);
 
    } catch (error) {
      this.logger.error(`Error getting collaborative filtering recommendations for user ${userId}`, error);
      throw error;
    }
  }
 
  private async findSimilarUsers(userId: string): Promise<string[]> {
    // Simplified collaborative filtering - find users with similar interaction patterns
    const userInteractions = await this.interactionModel.find({ userId }).limit(100);
    
    Iif (userInteractions.length === 0) return [];
 
    const userProductIds = new Set(userInteractions.map(i => i.targetId.toString()));
    
    // Find users who interacted with similar products
    const similarUserInteractions = await this.interactionModel.aggregate([
      {
        $match: {
          targetId: { $in: Array.from(userProductIds) },
          userId: { $ne: userId },
        },
      },
      {
        $group: {
          _id: '$userId',
          commonProducts: { $addToSet: '$targetId' },
          interactionCount: { $sum: 1 },
        },
      },
      {
        $addFields: {
          similarity: {
            $divide: [
              { $size: '$commonProducts' },
              userProductIds.size,
            ],
          },
        },
      },
      { $match: { similarity: { $gte: 0.1 } } },
      { $sort: { similarity: -1 as any } },
      { $limit: 10 },
    ]);
 
    return similarUserInteractions.map(user => user._id.toString());
  }
 
  private async getProductsFromSimilarUsers(
    userId: string,
    similarUserIds: string[],
    options: any,
  ): Promise<Array<{ productId: string; score: number }>> {
    Iif (similarUserIds.length === 0) return [];
 
    // Get user's existing interactions to exclude them
    const userInteractions = await this.interactionModel.find({ userId });
    const userProductIds = new Set(userInteractions.map(i => i.targetId.toString()));
 
    // Get products liked by similar users
    const recommendations = await this.interactionModel.aggregate([
      {
        $match: {
          userId: { $in: similarUserIds },
          targetId: { $nin: Array.from(userProductIds) },
          actionType: { $in: ['purchase', 'wishlist', 'click'] },
        },
      },
      {
        $group: {
          _id: '$targetId',
          score: { $sum: 1 },
          users: { $addToSet: '$userId' },
        },
      },
      { $sort: { score: -1 as any } },
      { $limit: options.limit || 20 },
    ]);
 
    return recommendations.map(rec => ({
      productId: rec._id.toString(),
      score: rec.score / similarUserIds.length, // Normalize by number of similar users
    }));
  }
}