Skip to content

Commit

Permalink
Merge pull request #35 from vishnuvinay89/all-saas-0.2-dev
Browse files Browse the repository at this point in the history
Task #234670 task : Cohort and cohort membvers and user  Inactive  on 30 days of creation
  • Loading branch information
gouravmore authored Mar 5, 2025
2 parents 3d080a7 + 15d6172 commit ccbe446
Show file tree
Hide file tree
Showing 3 changed files with 103 additions and 2 deletions.
2 changes: 1 addition & 1 deletion src/adapters/postgres/user-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -719,7 +719,7 @@ export class PostgresUserService implements IServicelocator {
}
let userDetails = await this.usersRepository.findOne({
where: whereClause,
select: ["userId", "username", "name", "mobile", "email", "temporaryPassword", "createdBy"]
select: ["userId", "username", "name", "mobile", "email", "temporaryPassword", "createdBy", "status"]
})
if (!userDetails) {
return false;
Expand Down
3 changes: 2 additions & 1 deletion src/cohort/cohort.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { User } from "src/user/entities/user-entity";
import { UserRoleMapping } from "src/rbac/assign-role/entities/assign-role.entity";
import { PostgresRoleService } from "src/adapters/postgres/rbac/role-adapter";
import { RolePrivilegeMapping } from "src/rbac/assign-privilege/entities/assign-privilege.entity";
import { CronService } from './cron/cron.service';

@Module({
imports: [
Expand All @@ -32,6 +33,6 @@ import { RolePrivilegeMapping } from "src/rbac/assign-privilege/entities/assign-
PostgresModule
],
controllers: [CohortController],
providers: [CohortAdapter, FieldsService, PostgresCohortService, PostgresFieldsService, CohortAcademicYearService, PostgresAcademicYearService, PostgresCohortMembersService,PostgresRoleService],
providers: [CohortAdapter, FieldsService, PostgresCohortService, PostgresFieldsService, CohortAcademicYearService, PostgresAcademicYearService, PostgresCohortMembersService,PostgresRoleService,CronService],
})
export class CohortModule { }
100 changes: 100 additions & 0 deletions src/cohort/cron/cron.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import { Injectable } from '@nestjs/common';
import { Cron } from '@nestjs/schedule';
import { InjectRepository } from '@nestjs/typeorm';
import { Cohort } from 'src/cohort/entities/cohort.entity';
import { User, UserStatus } from 'src/user/entities/user-entity';
import { CohortMembers, MemberStatus } from 'src/cohortMembers/entities/cohort-member.entity';
import { PostgresUserService } from 'src/adapters/postgres/user-adapter';
import { Repository } from 'typeorm';
import { LessThan } from 'typeorm';

@Injectable()
export class CronService {
constructor(
@InjectRepository(Cohort)
private cohortRepository: Repository<Cohort>,
@InjectRepository(User)
private userRepository: Repository<User>,
@InjectRepository(CohortMembers)
private cohortMembersRepository: Repository<CohortMembers>,
private postgresUserService: PostgresUserService,
) {}

// Get all active cohorts older than 30 days
async getOldActiveCohorts() {
try {
const dateThreshold = new Date();
dateThreshold.setDate(dateThreshold.getDate() - 30);
console.log(dateThreshold)

const cohorts = await this.cohortRepository.find({
where: {
status: 'active',
createdAt: LessThan(dateThreshold), // Only get those older than 30 days
},
});

return cohorts;
} catch (error) {
console.error('Error fetching cohorts:', error);
}
}

// Update the status to inactive
async setInactive(cohortId: string) {
try {
await this.cohortRepository.update(cohortId, {
status: 'inactive',
});
} catch (error) {
console.error('Error updating cohort status:', error);
}
}
async setUserInactive(cohortIds) {
let filter = {
"filters": {
"role" : "learner",
"status" : ["active"]
},
"tenantCohortRoleMapping": {
"tenantId": cohortIds.tenantId,
"cohortId": [cohortIds.cohortId]
},
}
let userId = await this.postgresUserService.findAllUserDetails(filter)

if(userId && userId.getUserDetails.length > 0) {
let results = userId.getUserDetails;
//get only the userids from userId
let userIds = results.map(item => item.userId)
//update the user status to inactive in user table
await this.userRepository.update(userIds,{
status : UserStatus.INACTIVE
})
await this.cohortMembersRepository.update(userIds,{
status : MemberStatus.INACTIVE
})

}
}


@Cron(process.env.COHORT_CRON_EXPRESSION)
async handleCron() {
console.log('Running Cohort Inactive Status Check...');

const oldCohorts = await this.getOldActiveCohorts();

if (oldCohorts && oldCohorts.length > 0) {
for (const cohort of oldCohorts) {
await this.setInactive(cohort.cohortId);
console.log(`Cohort with ID ${cohort.cohortId} set to inactive.`);
await this.setUserInactive(cohort);
console.log(`users mapped to cohortID ${cohort.cohortId} set to inactive.`);
}
} else {
console.log('No cohorts to update.');
}
}
}

0 comments on commit ccbe446

Please sign in to comment.