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 | import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { UserConnection, UserConnectionDocument } from '../../../database/schemas/user-connection.schema'; import { SocialActivity, SocialActivityDocument } from '../../../database/schemas/social-activity.schema'; @Injectable() export class SocialAnalyticsService { private readonly logger = new Logger(SocialAnalyticsService.name); constructor( @InjectModel(UserConnection.name) private connectionModel: Model<UserConnectionDocument>, @InjectModel(SocialActivity.name) private activityModel: Model<SocialActivityDocument>, ) {} async getUserConnectionStats(userId: string): Promise<{ friends: number; followers: number; following: number; }> { try { const [friends, followers, following] = await Promise.all([ this.connectionModel.countDocuments({ $or: [ { userId, status: 'accepted' }, { friendId: userId, status: 'accepted' }, ], }), this.connectionModel.countDocuments({ userId, connectionType: 'following', status: 'accepted', }), this.connectionModel.countDocuments({ friendId: userId, connectionType: 'following', status: 'accepted', }), ]); return { friends, followers, following }; } catch (error) { this.logger.error(`Error getting connection stats for user ${userId}`, error); return { friends: 0, followers: 0, following: 0 }; } } async getUserActivityStats(userId: string): Promise<{ totalActivities: number; thisWeek: number; engagementRate: number; }> { try { const oneWeekAgo = new Date(Date.now() - 7 * 24 * 60 * 60 * 1000); const [totalActivities, thisWeekActivities, engagementData] = await Promise.all([ this.activityModel.countDocuments({ userId, isActive: true }), this.activityModel.countDocuments({ userId, isActive: true, createdAt: { $gte: oneWeekAgo }, }), this.activityModel.aggregate([ { $match: { userId, isActive: true } }, { $group: { _id: null, avgEngagement: { $avg: '$engagement.engagementRate' }, }, }, ]), ]); const engagementRate = engagementData.length > 0 ? engagementData[0].avgEngagement : 0; return { totalActivities, thisWeek: thisWeekActivities, engagementRate: Math.round(engagementRate * 100) / 100, }; } catch (error) { this.logger.error(`Error getting activity stats for user ${userId}`, error); return { totalActivities: 0, thisWeek: 0, engagementRate: 0 }; } } async getUserAchievements(userId: string): Promise<{ totalBadges: number; recentAchievements: Array<{ type: string; title: string; earnedAt: Date; }>; }> { try { // This would typically come from a user achievements collection // For now, return sample data return { totalBadges: 5, recentAchievements: [ { type: 'social_butterfly', title: 'Social Butterfly', earnedAt: new Date(Date.now() - 2 * 24 * 60 * 60 * 1000), }, { type: 'deal_hunter', title: 'Deal Hunter', earnedAt: new Date(Date.now() - 5 * 24 * 60 * 60 * 1000), }, ], }; } catch (error) { this.logger.error(`Error getting achievements for user ${userId}`, error); return { totalBadges: 0, recentAchievements: [] }; } } } |