Skip to content

Commit

Permalink
implement deletion rocketChat user in KNL deletion module
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Nov 6, 2023
1 parent 9965b2a commit 550f16f
Show file tree
Hide file tree
Showing 3 changed files with 87 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ export const enum DeletionDomainModel {
FILE = 'file',
LESSONS = 'lessons',
PSEUDONYMS = 'pseudonyms',
ROCKETCHATUSER = 'rocketChatUser',
TEAMS = 'teams',
USER = 'user',
}
55 changes: 54 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,17 @@ import { LessonService } from '@src/modules/lesson/service';
import { PseudonymService } from '@src/modules/pseudonym';
import { TeamService } from '@src/modules/teams';
import { UserService } from '@src/modules/user';
import { RocketChatUserService } from '@src/modules/rocketchat-user/service/rocket-chat-user.service';
import { RocketChatService } from '@src/modules/rocketchat';
import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum';
import { DeletionLogService } from '../services/deletion-log.service';
import { DeletionRequestService } from '../services';
import { DeletionRequestLog, DeletionRequestProps, DeletionRequestUc } from './deletion-request.uc';
import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory';
import { DeletionStatusModel } from '../domain/types/deletion-status-model.enum';
import { deletionLogFactory } from '../domain/testing/factory/deletion-log.factory';
import { rocketChatUserFactory } from '@src/modules/rocketchat-user/domain/testing/rocket-chat-user.factory';

Check failure on line 22 in apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

`@src/modules/rocketchat-user/domain/testing/rocket-chat-user.factory` import should occur before import of `../domain/types/deletion-domain-model.enum`
import { RocketChatUser } from '@src/modules/rocketchat-user/domain';

Check failure on line 23 in apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts

View workflow job for this annotation

GitHub Actions / nest_lint

`@src/modules/rocketchat-user/domain` import should occur before import of `../domain/types/deletion-domain-model.enum`

describe(DeletionRequestUc.name, () => {
let module: TestingModule;
Expand All @@ -32,6 +36,8 @@ describe(DeletionRequestUc.name, () => {
let pseudonymService: DeepMocked<PseudonymService>;
let teamService: DeepMocked<TeamService>;
let userService: DeepMocked<UserService>;
let rocketChatUserService: DeepMocked<RocketChatUserService>;
let rocketChatService: DeepMocked<RocketChatService>;

beforeAll(async () => {
module = await Test.createTestingModule({
Expand Down Expand Up @@ -81,6 +87,14 @@ describe(DeletionRequestUc.name, () => {
provide: UserService,
useValue: createMock<UserService>(),
},
{
provide: RocketChatUserService,
useValue: createMock<RocketChatUserService>(),
},
{
provide: RocketChatService,
useValue: createMock<RocketChatService>(),
},
],
}).compile();

Expand All @@ -96,6 +110,8 @@ describe(DeletionRequestUc.name, () => {
pseudonymService = module.get(PseudonymService);
teamService = module.get(TeamService);
userService = module.get(UserService);
rocketChatUserService = module.get(RocketChatUserService);
rocketChatService = module.get(RocketChatService);
await setupEntities();
});

Expand Down Expand Up @@ -153,6 +169,9 @@ describe(DeletionRequestUc.name, () => {
const setup = () => {
jest.clearAllMocks();
const deletionRequestToExecute = deletionRequestFactory.build({ deleteAfter: new Date('2023-01-01') });
const rocketChatUser: RocketChatUser = rocketChatUserFactory.build({
userId: deletionRequestToExecute.targetRefId,
});

classService.deleteUserDataFromClasses.mockResolvedValueOnce(1);
courseGroupService.deleteUserDataFromCourseGroup.mockResolvedValueOnce(2);
Expand All @@ -163,9 +182,11 @@ describe(DeletionRequestUc.name, () => {
pseudonymService.deleteByUserId.mockResolvedValueOnce(2);
teamService.deleteUserDataFromTeams.mockResolvedValueOnce(2);
userService.deleteUser.mockResolvedValueOnce(1);
rocketChatUserService.deleteByUserId.mockResolvedValueOnce(1);

return {
deletionRequestToExecute,
rocketChatUser,
};
};

Expand Down Expand Up @@ -287,14 +308,46 @@ describe(DeletionRequestUc.name, () => {
expect(userService.deleteUser).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId);
});

it('should call rocketChatUserService.findByUserId to find rocketChatUser in rocketChatUser module', async () => {
const { deletionRequestToExecute } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);

await uc.executeDeletionRequests();

expect(rocketChatUserService.findByUserId).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId);
});

it('should call rocketChatUserService.deleteByUserId to delete rocketChatUser in rocketChatUser module', async () => {
const { deletionRequestToExecute, rocketChatUser } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);
rocketChatUserService.findByUserId.mockResolvedValueOnce(rocketChatUser);

await uc.executeDeletionRequests();

expect(rocketChatUserService.deleteByUserId).toHaveBeenCalledWith(deletionRequestToExecute.targetRefId);
});

it('should call rocketChatService.deleteUser to delete rocketChatUser in rocketChat external module', async () => {
const { deletionRequestToExecute, rocketChatUser } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);
rocketChatUserService.findByUserId.mockResolvedValueOnce(rocketChatUser);

await uc.executeDeletionRequests();

expect(rocketChatService.deleteUser).toHaveBeenCalledWith(rocketChatUser.username);
});

it('should call deletionLogService.createDeletionLog to create logs for deletionRequest', async () => {
const { deletionRequestToExecute } = setup();

deletionRequestService.findAllItemsToExecute.mockResolvedValueOnce([deletionRequestToExecute]);

await uc.executeDeletionRequests();

expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10);
expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(11);
});
});

Expand Down
33 changes: 32 additions & 1 deletion apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ import { CourseService } from '@src/modules/learnroom/service';
import { CourseGroupService } from '@src/modules/learnroom/service/coursegroup.service';
import { FilesService } from '@src/modules/files/service';
import { AccountService } from '@src/modules/account/services/account.service';
import { RocketChatService } from '@src/modules/rocketchat';
import { RocketChatUserService } from '@src/modules/rocketchat-user/service/rocket-chat-user.service';
import { DeletionRequestService } from '../services/deletion-request.service';
import { DeletionDomainModel } from '../domain/types/deletion-domain-model.enum';
import { DeletionLogService } from '../services/deletion-log.service';
Expand Down Expand Up @@ -47,7 +49,9 @@ export class DeletionRequestUc {
private readonly lessonService: LessonService,
private readonly pseudonymService: PseudonymService,
private readonly teamService: TeamService,
private readonly userService: UserService
private readonly userService: UserService,
private readonly rocketChatUserService: RocketChatUserService,
private readonly rocketChatService: RocketChatService
) {}

async createDeletionRequest(
Expand Down Expand Up @@ -114,6 +118,7 @@ export class DeletionRequestUc {
this.pseudonymService.deleteByUserId(deletionRequest.targetRefId),
this.teamService.deleteUserDataFromTeams(deletionRequest.targetRefId),
this.userService.deleteUser(deletionRequest.targetRefId),
this.removeUserFromRocketChatByUserId(deletionRequest.targetRefId),
])
.then(
async ([
Expand All @@ -127,6 +132,7 @@ export class DeletionRequestUc {
pseudonymDeleted,
teamsUpdated,
userDeleted,
rocketChatUserDeleted,
]) => {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
Expand Down Expand Up @@ -226,6 +232,16 @@ export class DeletionRequestUc {
);
}

if (rocketChatUserDeleted > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.ROCKETCHATUSER,
DeletionOperationModel.DELETE,
0,
rocketChatUserDeleted
);
}

await this.deletionRequestService.markDeletionRequestAsExecuted(deletionRequest.id);

return true;
Expand All @@ -235,4 +251,19 @@ export class DeletionRequestUc {
await this.deletionRequestService.markDeletionRequestAsFailed(deletionRequest.id);
});
}

private async removeUserFromRocketChatByUserId(userId: EntityId): Promise<number> {
const rocketChatUser = await this.rocketChatUserService.findByUserId(userId);

if (!rocketChatUser) {
return 0;
}

const [, rocketChatUserDeleted] = await Promise.all([
this.rocketChatService.deleteUser(rocketChatUser.username),
this.rocketChatUserService.deleteByUserId(rocketChatUser.userId),
]);

return rocketChatUserDeleted;
}
}

0 comments on commit 550f16f

Please sign in to comment.