Skip to content

Commit

Permalink
feat: add check for last recover action in cron;
Browse files Browse the repository at this point in the history
  • Loading branch information
ilasw committed Aug 13, 2024
1 parent 0c8aae2 commit 6f248af
Showing 1 changed file with 24 additions and 3 deletions.
27 changes: 24 additions & 3 deletions src/cron/updateCurrentStreak.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@ import { Cron } from './cron';
import { User, UserStreak } from '../entity';
import { checkUserStreak, clearUserStreak } from '../common';
import { counters } from '../telemetry';
import {
ReadingStreakActions,
ReadingStreakActionType,
} from '../entity/ReadingStreakActions';

const cron: Cron = {
name: 'update-current-streak',
Expand All @@ -25,11 +29,28 @@ const cron: Cron = {
.getRawMany();

const userIdsToReset = [];
usersPastStreakTime.forEach((userStreak) => {

for (const userStreak of usersPastStreakTime) {
if (checkUserStreak(userStreak)) {
userIdsToReset.push(userStreak.userId);

// fetch user recover actions only if user has streak lose
const lastRecoverAction = await con
.getRepository(ReadingStreakActions)
.findOne({
where: {
userStreak: userStreak.userId,
type: ReadingStreakActionType.Recover,
},
order: {
timestamp: 'DESC',
},
});

if (checkUserStreak(userStreak, lastRecoverAction)) {
userIdsToReset.push(userStreak.userId);
}
}
});
}

if (!userIdsToReset.length) {
logger.info('no user streaks to reset');
Expand Down

0 comments on commit 6f248af

Please sign in to comment.