diff --git a/src/words/words.service.ts b/src/words/words.service.ts index b215b7b..90fc1ca 100644 --- a/src/words/words.service.ts +++ b/src/words/words.service.ts @@ -25,31 +25,33 @@ export class WordsService { async getDailyWords(difficulty?: string): Promise { const today = new Date().toISOString().split('T')[0]; - const dailyKey = difficulty ? `${today}_${difficulty}` : today; - const existingQuery = this.repo + let existingQuery = this.repo .createQueryBuilder('w') - .where('w.daily_date = :dailyKey AND w.is_daily = true', { dailyKey }); + .where('w.daily_date = :today AND w.is_daily = true', { today }); + if (difficulty) existingQuery = existingQuery.andWhere('w.difficulty = :difficulty', { difficulty }); + const existing = await existingQuery.getMany(); if (existing.length >= 10) return existing.slice(0, 10); - await this.repo + let resetQuery = this.repo .createQueryBuilder() .update(Word) .set({ is_daily: false, daily_date: null }) - .where('daily_date = :dailyKey AND is_daily = true', { dailyKey }) - .execute(); + .where('daily_date = :today AND is_daily = true', { today }); + if (difficulty) resetQuery = resetQuery.andWhere('difficulty = :difficulty', { difficulty }); + await resetQuery.execute(); - const newWordsQuery = this.repo + let selectQuery = this.repo .createQueryBuilder('w') .orderBy('RAND()') .limit(10); - if (difficulty) newWordsQuery.where('w.difficulty = :difficulty', { difficulty }); + if (difficulty) selectQuery = selectQuery.where('w.difficulty = :difficulty', { difficulty }); - const shuffled = await newWordsQuery.getMany(); + const shuffled = await selectQuery.getMany(); for (const w of shuffled) { w.is_daily = true; - w.daily_date = dailyKey; + w.daily_date = today; } return this.repo.save(shuffled); }