|
| 1 | +import { Injectable } from '@nestjs/common'; |
| 2 | +import { Cron } from '@nestjs/schedule'; |
| 3 | +import { InjectRepository } from '@nestjs/typeorm'; |
| 4 | +import { Cohort } from 'src/cohort/entities/cohort.entity'; |
| 5 | +import { User, UserStatus } from 'src/user/entities/user-entity'; |
| 6 | +import { CohortMembers, MemberStatus } from 'src/cohortMembers/entities/cohort-member.entity'; |
| 7 | +import { PostgresUserService } from 'src/adapters/postgres/user-adapter'; |
| 8 | +import { Repository } from 'typeorm'; |
| 9 | +import { LessThan } from 'typeorm'; |
| 10 | + |
| 11 | +@Injectable() |
| 12 | +export class CronService { |
| 13 | + constructor( |
| 14 | + @InjectRepository(Cohort) |
| 15 | + private cohortRepository: Repository<Cohort>, |
| 16 | + @InjectRepository(User) |
| 17 | + private userRepository: Repository<User>, |
| 18 | + @InjectRepository(CohortMembers) |
| 19 | + private cohortMembersRepository: Repository<CohortMembers>, |
| 20 | + private postgresUserService: PostgresUserService, |
| 21 | + ) {} |
| 22 | + |
| 23 | + // Get all active cohorts older than 30 days |
| 24 | + async getOldActiveCohorts() { |
| 25 | + try { |
| 26 | + const dateThreshold = new Date(); |
| 27 | + dateThreshold.setDate(dateThreshold.getDate() - 30); |
| 28 | + console.log(dateThreshold) |
| 29 | + |
| 30 | + const cohorts = await this.cohortRepository.find({ |
| 31 | + where: { |
| 32 | + status: 'active', |
| 33 | + createdAt: LessThan(dateThreshold), // Only get those older than 30 days |
| 34 | + }, |
| 35 | + }); |
| 36 | + |
| 37 | + return cohorts; |
| 38 | + } catch (error) { |
| 39 | + console.error('Error fetching cohorts:', error); |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + // Update the status to inactive |
| 44 | + async setInactive(cohortId: string) { |
| 45 | + try { |
| 46 | + await this.cohortRepository.update(cohortId, { |
| 47 | + status: 'inactive', |
| 48 | + }); |
| 49 | + } catch (error) { |
| 50 | + console.error('Error updating cohort status:', error); |
| 51 | + } |
| 52 | + } |
| 53 | + async setUserInactive(cohortIds) { |
| 54 | + let filter = { |
| 55 | + "filters": { |
| 56 | + "role" : "learner", |
| 57 | + "status" : ["active"] |
| 58 | + }, |
| 59 | + "tenantCohortRoleMapping": { |
| 60 | + "tenantId": cohortIds.tenantId, |
| 61 | + "cohortId": [cohortIds.cohortId] |
| 62 | + }, |
| 63 | + } |
| 64 | + let userId = await this.postgresUserService.findAllUserDetails(filter) |
| 65 | + |
| 66 | + if(userId && userId.getUserDetails.length > 0) { |
| 67 | + let results = userId.getUserDetails; |
| 68 | + //get only the userids from userId |
| 69 | + let userIds = results.map(item => item.userId) |
| 70 | + //update the user status to inactive in user table |
| 71 | + await this.userRepository.update(userIds,{ |
| 72 | + status : UserStatus.INACTIVE |
| 73 | + }) |
| 74 | + await this.cohortMembersRepository.update(userIds,{ |
| 75 | + status : MemberStatus.INACTIVE |
| 76 | + }) |
| 77 | + |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + |
| 82 | + @Cron(process.env.COHORT_CRON_EXPRESSION) |
| 83 | + async handleCron() { |
| 84 | + console.log('Running Cohort Inactive Status Check...'); |
| 85 | + |
| 86 | + const oldCohorts = await this.getOldActiveCohorts(); |
| 87 | + |
| 88 | + if (oldCohorts && oldCohorts.length > 0) { |
| 89 | + for (const cohort of oldCohorts) { |
| 90 | + await this.setInactive(cohort.cohortId); |
| 91 | + console.log(`Cohort with ID ${cohort.cohortId} set to inactive.`); |
| 92 | + await this.setUserInactive(cohort); |
| 93 | + console.log(`users mapped to cohortID ${cohort.cohortId} set to inactive.`); |
| 94 | + } |
| 95 | + } else { |
| 96 | + console.log('No cohorts to update.'); |
| 97 | + } |
| 98 | + } |
| 99 | +} |
| 100 | + |
0 commit comments