Skip to content

Commit

Permalink
Fix Delete School, Update Student, and Create Scheduler to Recount Ad…
Browse files Browse the repository at this point in the history
…min & Student #build
  • Loading branch information
iwansuryaningrat committed Aug 20, 2024
1 parent 73de3c2 commit b9d7b18
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
21 changes: 18 additions & 3 deletions src/admin/services/scheduler.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ import { Level } from "@app/common/model/schema/levels.schema";
import { Score } from "@app/common/model/schema/scores.schema";
import { Record } from "@app/common/model/schema/records.schema";
import { TimeHelper } from "@app/common/helpers/time.helper";
import { UserRole } from "@app/common/enums/role.enum";

@Injectable()
export class SchedulerService {
Expand All @@ -20,9 +21,6 @@ export class SchedulerService {
@InjectModel(User.name) private userModel: Model<User>,
@InjectModel(School.name) private schoolModel: Model<School>,
@InjectModel(Game.name) private gameModel: Model<Game>,
@InjectModel(Level.name) private levelModel: Model<Level>,
@InjectModel(Score.name) private scoreModel: Model<Score>,
@InjectModel(Record.name) private recordModel: Model<Record>,
) { }

private readonly logger = new Logger(SchedulerService.name);
Expand Down Expand Up @@ -90,4 +88,21 @@ export class SchedulerService {
console.log(error?.message);
}
}

@Cron(CronExpression.EVERY_HOUR)
async recountAdminAndStudent() {
try {
let schools = await this.schoolModel.find({ deletedAt: null });

if (schools?.length) await Promise.all(schools.map(async (school) => {
let users = await this.userModel.countDocuments({ school: school._id, deletedAt: null, role: UserRole.USER });
let admins = await this.userModel.countDocuments({ school: school._id, deletedAt: null, role: UserRole.ADMIN });

await this.schoolModel.updateOne({ _id: school._id }, { $set: { studentsCount: users, adminsCount: admins } });
}));
} catch (error) {
this.logger.error(this.recountAdminAndStudent.name);
console.log(error?.message);
}
}
}
4 changes: 2 additions & 2 deletions src/admin/services/schools.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -206,10 +206,10 @@ export class SchoolAdminService {

let users = await this.userModel.find({ school: school._id });

if (users.length > 0) users.forEach(async (item) => {
if (users?.length > 0) await Promise.all(users.map(async (item) => {
item.school = null;
await item.save();
});
}))

await this.logService.logging({
target: TargetLogEnum.SCHOOL,
Expand Down
8 changes: 6 additions & 2 deletions src/admin/services/students.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -88,17 +88,21 @@ export class StudentsService {
}
if (users.role === UserRole.ADMIN && users?.school) query.school = users?.school;

let user = await this.userModel.findOne().populate('image')
let user = await this.userModel.findOne(query).populate('image')
if (!user) throw new NotFoundException("User Not Found");

let school = user?.school ?? users?.school
if (!school) school = await this.schoolModel.findOne({ _id: new Types.ObjectId(body?.schoolId) });

const check = await this.userModel.findOne({
role: UserRole.USER,
name: body.name,
school: user.school,
school: user?.school ?? school._id,
_id: { $ne: user._id },
});
if (check) throw new BadRequestException("Student Name Already Exist");

user.school = school;
user.$set(body)
user.lastUpdatedBy = users
if (media.length) {
Expand Down

0 comments on commit b9d7b18

Please sign in to comment.