|
| 1 | +import { Injectable, Logger } from '@nestjs/common'; |
| 2 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 3 | +import { Repository } from 'typeorm'; |
| 4 | +import { DailyQuest } from '../entities/daily-quest.entity'; |
| 5 | +import { DailyQuestStatusDto } from '../dtos/daily-quest-status.dto'; |
| 6 | +import { GetTodaysDailyQuestProvider } from './getTodaysDailyQuest.provider'; |
| 7 | + |
| 8 | +/** |
| 9 | + * Provider for fetching the status of today's Daily Quest. |
| 10 | + * Returns minimal data (totalQuestions, completedQuestions, isCompleted) for fast, cache-friendly lookups. |
| 11 | + * |
| 12 | + * This is read-only and does not mutate state. |
| 13 | + * If no quest exists, it auto-generates one using the existing generation logic. |
| 14 | + */ |
| 15 | +@Injectable() |
| 16 | +export class GetTodaysDailyQuestStatusProvider { |
| 17 | + private readonly logger = new Logger(GetTodaysDailyQuestStatusProvider.name); |
| 18 | + |
| 19 | + constructor( |
| 20 | + @InjectRepository(DailyQuest) |
| 21 | + private readonly dailyQuestRepository: Repository<DailyQuest>, |
| 22 | + private readonly getTodaysDailyQuestProvider: GetTodaysDailyQuestProvider, |
| 23 | + ) {} |
| 24 | + |
| 25 | + /** |
| 26 | + * Fetches the status of today's Daily Quest. |
| 27 | + * Auto-generates a quest if one doesn't exist. |
| 28 | + * |
| 29 | + * @param userId - The user's ID |
| 30 | + * @returns DailyQuestStatusDto with totalQuestions, completedQuestions, isCompleted |
| 31 | + */ |
| 32 | + async execute(userId: string): Promise<DailyQuestStatusDto> { |
| 33 | + const todayDate = this.getTodayDateString(); |
| 34 | + this.logger.log( |
| 35 | + `Fetching daily quest status for user ${userId} on ${todayDate}`, |
| 36 | + ); |
| 37 | + |
| 38 | + // Try to find existing quest for today |
| 39 | + let dailyQuest = await this.dailyQuestRepository.findOne({ |
| 40 | + where: { userId, questDate: todayDate }, |
| 41 | + select: ['id', 'totalQuestions', 'completedQuestions', 'isCompleted'], |
| 42 | + }); |
| 43 | + |
| 44 | + // If no quest exists, auto-generate one |
| 45 | + if (!dailyQuest) { |
| 46 | + this.logger.log( |
| 47 | + `No quest found for user ${userId}, auto-generating quest`, |
| 48 | + ); |
| 49 | + // Use the existing provider to generate the full quest |
| 50 | + // This ensures consistency with the main getTodaysDailyQuest endpoint |
| 51 | + // const fullQuest = await this.getTodaysDailyQuestProvider.execute(userId); |
| 52 | + |
| 53 | + // Fetch the newly created quest with status fields |
| 54 | + dailyQuest = await this.dailyQuestRepository.findOne({ |
| 55 | + where: { userId, questDate: todayDate }, |
| 56 | + select: ['id', 'totalQuestions', 'completedQuestions', 'isCompleted'], |
| 57 | + }); |
| 58 | + |
| 59 | + if (!dailyQuest) { |
| 60 | + throw new Error( |
| 61 | + `Failed to retrieve created daily quest for user ${userId}`, |
| 62 | + ); |
| 63 | + } |
| 64 | + } |
| 65 | + |
| 66 | + return this.buildStatusResponse(dailyQuest); |
| 67 | + } |
| 68 | + |
| 69 | + /** |
| 70 | + * Returns today's date as YYYY-MM-DD string (timezone-safe) |
| 71 | + */ |
| 72 | + private getTodayDateString(): string { |
| 73 | + const now = new Date(); |
| 74 | + return now.toISOString().split('T')[0]; |
| 75 | + } |
| 76 | + |
| 77 | + /** |
| 78 | + * Converts DailyQuest entity to DailyQuestStatusDto |
| 79 | + */ |
| 80 | + private buildStatusResponse(dailyQuest: DailyQuest): DailyQuestStatusDto { |
| 81 | + return { |
| 82 | + totalQuestions: dailyQuest.totalQuestions, |
| 83 | + completedQuestions: dailyQuest.completedQuestions, |
| 84 | + isCompleted: dailyQuest.isCompleted, |
| 85 | + }; |
| 86 | + } |
| 87 | +} |
0 commit comments