|
| 1 | +import { schedule } from 'node-cron'; |
| 2 | +import config from '../../config'; |
| 3 | +import { logger } from '../../utils/logger'; |
| 4 | +import { User } from '../../entities/user'; |
| 5 | +import { updateUserGitcoinScores } from '../userService'; |
| 6 | +import { |
| 7 | + GITCOIN_PASSPORT_MIN_VALID_ANALYSIS_SCORE, |
| 8 | + GITCOIN_PASSPORT_MIN_VALID_SCORER_SCORE, |
| 9 | +} from '../../constants/gitcoin'; |
| 10 | + |
| 11 | +const cronJobTime = |
| 12 | + (config.get('UPDATE_GITCOIN_SCORES_CRONJOB_EXPRESSION') as string) || |
| 13 | + '0 */6 * * *'; // Run every 6 hours by default |
| 14 | + |
| 15 | +export const runUpdateGitcoinScoresJob = () => { |
| 16 | + logger.debug( |
| 17 | + 'runUpdateGitcoinScoresJob() has been called, cronJobTime', |
| 18 | + cronJobTime, |
| 19 | + ); |
| 20 | + |
| 21 | + schedule(cronJobTime, async () => { |
| 22 | + try { |
| 23 | + // Find users who skipped verification and have insufficient scores |
| 24 | + const users = await User.createQueryBuilder('user') |
| 25 | + .where('user.skipVerification = :skipVerification', { |
| 26 | + skipVerification: true, |
| 27 | + }) |
| 28 | + .andWhere( |
| 29 | + '(user.analysisScore < :minAnalysisScore OR user.passportScore < :minPassportScore)', |
| 30 | + { |
| 31 | + minAnalysisScore: GITCOIN_PASSPORT_MIN_VALID_ANALYSIS_SCORE, |
| 32 | + minPassportScore: GITCOIN_PASSPORT_MIN_VALID_SCORER_SCORE, |
| 33 | + }, |
| 34 | + ) |
| 35 | + .getMany(); |
| 36 | + |
| 37 | + logger.debug(`Found ${users.length} users to update Gitcoin scores`); |
| 38 | + |
| 39 | + // Update scores for each user |
| 40 | + for (const user of users) { |
| 41 | + try { |
| 42 | + await updateUserGitcoinScores(user); |
| 43 | + logger.debug( |
| 44 | + `Successfully updated Gitcoin scores for user ${user.id}`, |
| 45 | + ); |
| 46 | + } catch (error) { |
| 47 | + logger.error( |
| 48 | + `Failed to update Gitcoin scores for user ${user.id}:`, |
| 49 | + error, |
| 50 | + ); |
| 51 | + } |
| 52 | + } |
| 53 | + } catch (error) { |
| 54 | + logger.error('Error in updateGitcoinScoresJob:', error); |
| 55 | + } |
| 56 | + }); |
| 57 | +}; |
0 commit comments