Skip to content

Commit

Permalink
BC-6082 Add Logger to KNL-Module (#4668)
Browse files Browse the repository at this point in the history
* add logger to registrationPin deleteService

* add logger to dashboardModule  deleteService

* add logger to classModule  deleteService

* add logger to courseGroupService for delete method

* add logger to courseService for delete method

* add logger to FilesModule for removePermision and markFordeletion

* add logger to LessonModule for removeUserData from lessons

* add logger to pseudonymService and add logger import in course test

* add logger to teamService in team module

* add logger to userService for deleteUsermethod in user  module

* add logger to RocketChatUser and to RocketChat Services for delete methods

* Update apps/server/src/modules/learnroom/service/course.service.spec.ts

Co-authored-by: Bartosz Nowicki <[email protected]>

* add additional metadata to logs during deletion user data for services

* fix test

* some fixes

* fix after review

* change logger in teams module

* add import in pseudonym module

---------

Co-authored-by: Bartosz Nowicki <[email protected]>
Co-authored-by: sszafGCA <[email protected]>
  • Loading branch information
3 people authored Jan 14, 2024
1 parent dbd2e3f commit 3bf7056
Show file tree
Hide file tree
Showing 34 changed files with 525 additions and 52 deletions.
2 changes: 2 additions & 0 deletions apps/server/src/modules/class/class.module.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
import { Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { ClassService } from './service';
import { ClassesRepo } from './repo';

@Module({
imports: [LoggerModule],
providers: [ClassService, ClassesRepo],
exports: [ClassService],
})
Expand Down
5 changes: 5 additions & 0 deletions apps/server/src/modules/class/service/class.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { InternalServerErrorException } from '@nestjs/common';
import { Test, TestingModule } from '@nestjs/testing';
import { EntityId } from '@shared/domain/types';
import { setupEntities } from '@shared/testing';
import { Logger } from '@src/core/logger';
import { Class } from '../domain';
import { classFactory } from '../domain/testing';
import { classEntityFactory } from '../entity/testing';
Expand All @@ -24,6 +25,10 @@ describe(ClassService.name, () => {
provide: ClassesRepo,
useValue: createMock<ClassesRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
],
}).compile();

Expand Down
32 changes: 28 additions & 4 deletions apps/server/src/modules/class/service/class.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Injectable, InternalServerErrorException } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { DomainModel, EntityId, StatusModel } from '@shared/domain/types';
import { Logger } from '@src/core/logger';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { Class } from '../domain';
import { ClassesRepo } from '../repo';

@Injectable()
export class ClassService {
constructor(private readonly classesRepo: ClassesRepo) {}
constructor(private readonly classesRepo: ClassesRepo, private readonly logger: Logger) {
this.logger.setContext(ClassService.name);
}

public async findClassesForSchool(schoolId: EntityId): Promise<Class[]> {
const classes: Class[] = await this.classesRepo.findAllBySchoolId(schoolId);
Expand All @@ -19,8 +23,16 @@ export class ClassService {
return classes;
}

// FIXME There is no usage of this method
public async deleteUserDataFromClasses(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting data from Classes',
DomainModel.CLASS,
userId,
StatusModel.PENDING
)
);

if (!userId) {
throw new InternalServerErrorException('User id is missing');
}
Expand All @@ -34,8 +46,20 @@ export class ClassService {
return domainObject;
});

const numberOfUpdatedClasses = updatedClasses.length;

await this.classesRepo.updateMany(updatedClasses);
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully removed user data from Classes',
DomainModel.CLASS,
userId,
StatusModel.FINISHED,
numberOfUpdatedClasses,
0
)
);

return updatedClasses.length;
return numberOfUpdatedClasses;
}
}
5 changes: 5 additions & 0 deletions apps/server/src/modules/files/service/files.service.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ObjectId } from '@mikro-orm/mongodb';
import { Test, TestingModule } from '@nestjs/testing';
import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { setupEntities } from '@shared/testing';
import { Logger } from '@src/core/logger';
import { FilesService } from './files.service';
import { FilesRepo } from '../repo';
import { fileEntityFactory, filePermissionEntityFactory } from '../entity/testing';
Expand All @@ -20,6 +21,10 @@ describe(FilesService.name, () => {
provide: FilesRepo,
useValue: createMock<FilesRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
],
}).compile();

Expand Down
54 changes: 50 additions & 4 deletions apps/server/src/modules/files/service/files.service.ts
Original file line number Diff line number Diff line change
@@ -1,17 +1,29 @@
import { Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { DomainModel, EntityId, StatusModel } from '@shared/domain/types';
import { Logger } from '@src/core/logger';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { FileEntity } from '../entity';
import { FilesRepo } from '../repo';

@Injectable()
export class FilesService {
constructor(private readonly repo: FilesRepo) {}
constructor(private readonly repo: FilesRepo, private readonly logger: Logger) {
this.logger.setContext(FilesService.name);
}

async findFilesAccessibleOrCreatedByUser(userId: EntityId): Promise<FileEntity[]> {
return this.repo.findByPermissionRefIdOrCreatorId(userId);
}

async removeUserPermissionsOrCreatorReferenceToAnyFiles(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting user data from Files',
DomainModel.FILE,
userId,
StatusModel.PENDING
)
);
const entities = await this.repo.findByPermissionRefIdOrCreatorId(userId);

if (entities.length === 0) {
Expand All @@ -25,14 +37,35 @@ export class FilesService {

await this.repo.save(entities);

return entities.length;
const numberOfUpdatedFiles = entities.length;

this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully removed user data from Files',
DomainModel.FILE,
userId,
StatusModel.FINISHED,
numberOfUpdatedFiles,
0
)
);

return numberOfUpdatedFiles;
}

async findFilesOwnedByUser(userId: EntityId): Promise<FileEntity[]> {
return this.repo.findByOwnerUserId(userId);
}

async markFilesOwnedByUserForDeletion(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Marking user files to deletion',
DomainModel.FILE,
userId,
StatusModel.PENDING
)
);
const entities = await this.repo.findByOwnerUserId(userId);

if (entities.length === 0) {
Expand All @@ -43,6 +76,19 @@ export class FilesService {

await this.repo.save(entities);

return entities.length;
const numberOfMarkedForDeletionFiles = entities.length;

this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully marked user files for deletion',
DomainModel.FILE,
userId,
StatusModel.FINISHED,
numberOfMarkedForDeletionFiles,
0
)
);

return numberOfMarkedForDeletionFiles;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import { Test, TestingModule } from '@nestjs/testing';
import { Course } from '@shared/domain/entity';
import { CourseRepo, UserRepo } from '@shared/repo';
import { courseFactory, setupEntities, userFactory } from '@shared/testing';
import { Logger } from '@src/core/logger';
import { CourseService } from './course.service';

describe('CourseService', () => {
Expand All @@ -24,6 +25,10 @@ describe('CourseService', () => {
provide: CourseRepo,
useValue: createMock<CourseRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
],
}).compile();
courseRepo = module.get(CourseRepo);
Expand Down
26 changes: 24 additions & 2 deletions apps/server/src/modules/learnroom/service/course.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Injectable } from '@nestjs/common';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { Course } from '@shared/domain/entity';
import { Counted, EntityId } from '@shared/domain/types';
import { Counted, DomainModel, EntityId, StatusModel } from '@shared/domain/types';
import { CourseRepo } from '@shared/repo';
import { Logger } from '@src/core/logger';

@Injectable()
export class CourseService {
constructor(private readonly repo: CourseRepo) {}
constructor(private readonly repo: CourseRepo, private readonly logger: Logger) {
this.logger.setContext(CourseService.name);
}

async findById(courseId: EntityId): Promise<Course> {
return this.repo.findById(courseId);
Expand All @@ -18,11 +22,29 @@ export class CourseService {
}

public async deleteUserDataFromCourse(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting data from Courses',
DomainModel.COURSE,
userId,
StatusModel.PENDING
)
);
const [courses, count] = await this.repo.findAllByUserId(userId);

courses.forEach((course: Course) => course.removeUser(userId));

await this.repo.save(courses);
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully removed data from Courses',
DomainModel.COURSE,
userId,
StatusModel.FINISHED,
0,
count
)
);

return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { createMock, DeepMocked } from '@golevelup/ts-jest';
import { Test, TestingModule } from '@nestjs/testing';
import { CourseGroupRepo, UserRepo } from '@shared/repo';
import { courseGroupFactory, setupEntities, userFactory } from '@shared/testing';
import { Logger } from '@src/core/logger';
import { CourseGroupService } from './coursegroup.service';

describe('CourseGroupService', () => {
Expand All @@ -23,6 +24,10 @@ describe('CourseGroupService', () => {
provide: CourseGroupRepo,
useValue: createMock<CourseGroupRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
],
}).compile();
courseGroupRepo = module.get(CourseGroupRepo);
Expand Down
26 changes: 24 additions & 2 deletions apps/server/src/modules/learnroom/service/coursegroup.service.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { Injectable } from '@nestjs/common';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { CourseGroup } from '@shared/domain/entity';
import { Counted, EntityId } from '@shared/domain/types';
import { Counted, DomainModel, EntityId, StatusModel } from '@shared/domain/types';
import { CourseGroupRepo } from '@shared/repo';
import { Logger } from '@src/core/logger';

@Injectable()
export class CourseGroupService {
constructor(private readonly repo: CourseGroupRepo) {}
constructor(private readonly repo: CourseGroupRepo, private readonly logger: Logger) {
this.logger.setContext(CourseGroupService.name);
}

public async findAllCourseGroupsByUserId(userId: EntityId): Promise<Counted<CourseGroup[]>> {
const [courseGroups, count] = await this.repo.findByUserId(userId);
Expand All @@ -14,11 +18,29 @@ export class CourseGroupService {
}

public async deleteUserDataFromCourseGroup(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting user data from CourseGroup',
DomainModel.COURSEGROUP,
userId,
StatusModel.PENDING
)
);
const [courseGroups, count] = await this.repo.findByUserId(userId);

courseGroups.forEach((courseGroup) => courseGroup.removeStudent(userId));

await this.repo.save(courseGroups);
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully deleted user data from CourseGroup',
DomainModel.COURSEGROUP,
userId,
StatusModel.FINISHED,
count,
0
)
);

return count;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import { DashboardElementRepo, IDashboardRepo, UserRepo } from '@shared/repo';
import { setupEntities, userFactory } from '@shared/testing';
import { LearnroomMetadata, LearnroomTypes } from '@shared/domain/types';
import { DashboardEntity, GridElement } from '@shared/domain/entity';
import { Logger } from '@src/core/logger';
import { DashboardService } from '.';

const learnroomMock = (id: string, name: string) => {
Expand Down Expand Up @@ -44,6 +45,10 @@ describe(DashboardService.name, () => {
provide: DashboardElementRepo,
useValue: createMock<DashboardElementRepo>(),
},
{
provide: Logger,
useValue: createMock<Logger>(),
},
],
}).compile();
dashboardService = module.get(DashboardService);
Expand Down
29 changes: 26 additions & 3 deletions apps/server/src/modules/learnroom/service/dashboard.service.ts
Original file line number Diff line number Diff line change
@@ -1,18 +1,41 @@
import { Inject, Injectable } from '@nestjs/common';
import { EntityId } from '@shared/domain/types';
import { DataDeletionDomainOperationLoggable } from '@shared/common/loggable';
import { DomainModel, EntityId, StatusModel } from '@shared/domain/types';
import { IDashboardRepo, DashboardElementRepo } from '@shared/repo';
import { Logger } from '@src/core/logger';

@Injectable()
export class DashboardService {
constructor(
@Inject('DASHBOARD_REPO') private readonly dashboardRepo: IDashboardRepo,
private readonly dashboardElementRepo: DashboardElementRepo
) {}
private readonly dashboardElementRepo: DashboardElementRepo,
private readonly logger: Logger
) {
this.logger.setContext(DashboardService.name);
}

async deleteDashboardByUserId(userId: EntityId): Promise<number> {
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Deleting user data from Dashboard',
DomainModel.DASHBOARD,
userId,
StatusModel.PENDING
)
);
const usersDashboard = await this.dashboardRepo.getUsersDashboard(userId);
await this.dashboardElementRepo.deleteByDashboardId(usersDashboard.id);
const result = await this.dashboardRepo.deleteDashboardByUserId(userId);
this.logger.info(
new DataDeletionDomainOperationLoggable(
'Successfully deleted user data from Dashboard',
DomainModel.DASHBOARD,
userId,
StatusModel.FINISHED,
0,
result
)
);

return result;
}
Expand Down
Loading

0 comments on commit 3bf7056

Please sign in to comment.