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 | import { Injectable, Logger } from '@nestjs/common'; import { InjectModel } from '@nestjs/mongoose'; import { Model } from 'mongoose'; import { CommunityChallenge, CommunityChallengeDocument } from '../../../database/schemas/community-challenge.schema'; @Injectable() export class ChallengeService { private readonly logger = new Logger(ChallengeService.name); constructor( @InjectModel(CommunityChallenge.name) private challengeModel: Model<CommunityChallengeDocument>, ) {} async getTrendingChallenges(limit: number = 10): Promise<Array<{ challengeId: string; title: string; participants: number; timeLeft: string; difficulty: string; }>> { try { const challenges = await this.challengeModel .find({ status: 'active', isPublic: true, endDate: { $gt: new Date() }, }) .sort({ 'analytics.totalParticipants': -1, createdAt: -1 }) .limit(limit); return challenges.map(challenge => ({ challengeId: challenge._id.toString(), title: challenge.title, participants: challenge.analytics.totalParticipants, timeLeft: this.calculateTimeLeft(challenge.endDate), difficulty: challenge.rules.difficulty, })); } catch (error) { this.logger.error('Error getting trending challenges', error); return []; } } private calculateTimeLeft(endDate: Date): string { const now = new Date(); const timeDiff = endDate.getTime() - now.getTime(); Iif (timeDiff <= 0) { return 'Ended'; } const days = Math.floor(timeDiff / (1000 * 60 * 60 * 24)); const hours = Math.floor((timeDiff % (1000 * 60 * 60 * 24)) / (1000 * 60 * 60)); if (days > 0) { return `${days} days left`; } else if (hours > 0) { return `${hours} hours left`; } else { return 'Less than 1 hour left'; } } } |