fix: getDailyWords use date column properly, filter by difficulty separately
All checks were successful
Build and Deploy / build-and-deploy (push) Successful in 32s

This commit is contained in:
deploy 2026-05-18 01:13:24 +00:00
parent 16c47f8692
commit 908d6ef8a9

View File

@ -25,31 +25,33 @@ export class WordsService {
async getDailyWords(difficulty?: string): Promise<Word[]> { async getDailyWords(difficulty?: string): Promise<Word[]> {
const today = new Date().toISOString().split('T')[0]; const today = new Date().toISOString().split('T')[0];
const dailyKey = difficulty ? `${today}_${difficulty}` : today;
const existingQuery = this.repo let existingQuery = this.repo
.createQueryBuilder('w') .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(); const existing = await existingQuery.getMany();
if (existing.length >= 10) return existing.slice(0, 10); if (existing.length >= 10) return existing.slice(0, 10);
await this.repo let resetQuery = this.repo
.createQueryBuilder() .createQueryBuilder()
.update(Word) .update(Word)
.set({ is_daily: false, daily_date: null }) .set({ is_daily: false, daily_date: null })
.where('daily_date = :dailyKey AND is_daily = true', { dailyKey }) .where('daily_date = :today AND is_daily = true', { today });
.execute(); if (difficulty) resetQuery = resetQuery.andWhere('difficulty = :difficulty', { difficulty });
await resetQuery.execute();
const newWordsQuery = this.repo let selectQuery = this.repo
.createQueryBuilder('w') .createQueryBuilder('w')
.orderBy('RAND()') .orderBy('RAND()')
.limit(10); .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) { for (const w of shuffled) {
w.is_daily = true; w.is_daily = true;
w.daily_date = dailyKey; w.daily_date = today;
} }
return this.repo.save(shuffled); return this.repo.save(shuffled);
} }