Skip to content

Commit ccbe446

Browse files
authored
Merge pull request #35 from vishnuvinay89/all-saas-0.2-dev
Task #234670 task : Cohort and cohort membvers and user Inactive on 30 days of creation
2 parents 3d080a7 + 15d6172 commit ccbe446

File tree

3 files changed

+103
-2
lines changed

3 files changed

+103
-2
lines changed

src/adapters/postgres/user-adapter.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -719,7 +719,7 @@ export class PostgresUserService implements IServicelocator {
719719
}
720720
let userDetails = await this.usersRepository.findOne({
721721
where: whereClause,
722-
select: ["userId", "username", "name", "mobile", "email", "temporaryPassword", "createdBy"]
722+
select: ["userId", "username", "name", "mobile", "email", "temporaryPassword", "createdBy", "status"]
723723
})
724724
if (!userDetails) {
725725
return false;

src/cohort/cohort.module.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ import { User } from "src/user/entities/user-entity";
2323
import { UserRoleMapping } from "src/rbac/assign-role/entities/assign-role.entity";
2424
import { PostgresRoleService } from "src/adapters/postgres/rbac/role-adapter";
2525
import { RolePrivilegeMapping } from "src/rbac/assign-privilege/entities/assign-privilege.entity";
26+
import { CronService } from './cron/cron.service';
2627

2728
@Module({
2829
imports: [
@@ -32,6 +33,6 @@ import { RolePrivilegeMapping } from "src/rbac/assign-privilege/entities/assign-
3233
PostgresModule
3334
],
3435
controllers: [CohortController],
35-
providers: [CohortAdapter, FieldsService, PostgresCohortService, PostgresFieldsService, CohortAcademicYearService, PostgresAcademicYearService, PostgresCohortMembersService,PostgresRoleService],
36+
providers: [CohortAdapter, FieldsService, PostgresCohortService, PostgresFieldsService, CohortAcademicYearService, PostgresAcademicYearService, PostgresCohortMembersService,PostgresRoleService,CronService],
3637
})
3738
export class CohortModule { }

src/cohort/cron/cron.service.ts

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
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

Comments
 (0)