Skip to content

Commit

Permalink
fix deletion rocketChatUser in KNL Deletion module
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Nov 6, 2023
2 parents ef3941a + a1ca155 commit 16a65cd
Show file tree
Hide file tree
Showing 5 changed files with 90 additions and 117 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -206,7 +206,7 @@ describe(DeletionRequestRepo.name, () => {
it('should find deletionRequests to execute with limit = 2', async () => {
const { expectedArray } = await setup();

const results = await repo.findAllItemsToExecution({ pagination: { limit: 2 } });
const results = await repo.findAllItemsToExecution(2);

expect(results.length).toEqual(2);

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { EntityManager } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { EntityId, IFindOptions, SortOrder } from '@shared/domain';
import { EntityId, SortOrder } from '@shared/domain';
import { DeletionRequest } from '../domain/deletion-request.do';
import { DeletionRequestEntity } from '../entity';
import { DeletionRequestMapper } from './mapper/deletion-request.mapper';
Expand Down Expand Up @@ -30,15 +30,13 @@ export class DeletionRequestRepo {
await this.em.flush();
}

async findAllItemsToExecution(options?: IFindOptions<DeletionRequest>): Promise<DeletionRequest[]> {
async findAllItemsToExecution(limit?: number): Promise<DeletionRequest[]> {
const currentDate = new Date();
const scope = new DeletionRequestScope().byDeleteAfter(currentDate).byStatus();
const { pagination } = options || { limit: 100 };
const order = { createdAt: SortOrder.desc };

const [deletionRequestEntities] = await this.em.findAndCount(DeletionRequestEntity, scope.query, {
offset: pagination?.skip,
limit: pagination?.limit,
limit,
orderBy: order,
});

Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { EntityId, IFindOptions } from '@shared/domain';
import { EntityId } from '@shared/domain';
import { ObjectId } from '@mikro-orm/mongodb';
import { DeletionRequestRepo } from '../repo/deletion-request.repo';
import { DeletionRequest } from '../domain/deletion-request.do';
Expand Down Expand Up @@ -39,8 +39,8 @@ export class DeletionRequestService {
return deletionRequest;
}

async findAllItemsToExecute(options?: IFindOptions<DeletionRequest>): Promise<DeletionRequest[]> {
const itemsToDelete: DeletionRequest[] = await this.deletionRequestRepo.findAllItemsToExecution(options);
async findAllItemsToExecute(limit?: number): Promise<DeletionRequest[]> {
const itemsToDelete: DeletionRequest[] = await this.deletionRequestRepo.findAllItemsToExecution(limit);

return itemsToDelete;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -347,7 +347,7 @@ describe(DeletionRequestUc.name, () => {

await uc.executeDeletionRequests();

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

Expand Down
189 changes: 82 additions & 107 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import { Injectable } from '@nestjs/common';
import { EntityId, IFindOptions } from '@shared/domain';
import { EntityId } from '@shared/domain';
import { PseudonymService } from '@src/modules/pseudonym';
import { UserService } from '@src/modules/user';
import { TeamService } from '@src/modules/teams';
Expand Down Expand Up @@ -66,9 +66,9 @@ export class DeletionRequestUc {
return result;
}

async executeDeletionRequests(options?: IFindOptions<DeletionRequest>): Promise<void> {
async executeDeletionRequests(limit?: number): Promise<void> {
const deletionRequestToExecution: DeletionRequest[] = await this.deletionRequestService.findAllItemsToExecute(
options
limit
);

for (const req of deletionRequestToExecution) {
Expand Down Expand Up @@ -134,113 +134,70 @@ export class DeletionRequestUc {
userDeleted,
rocketChatUserDeleted,
]) => {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.ACCOUNT,
await this.logDeletion(deletionRequest, DeletionDomainModel.ACCOUNT, DeletionOperationModel.DELETE, 0, 1);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.CLASS,
DeletionOperationModel.UPDATE,
classesUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.COURSEGROUP,
DeletionOperationModel.UPDATE,
courseGroupUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.COURSE,
DeletionOperationModel.UPDATE,
courseUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.FILE,
DeletionOperationModel.UPDATE,
fileDeleted + filesPermisionUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.LESSONS,
DeletionOperationModel.UPDATE,
lessonUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.PSEUDONYMS,
DeletionOperationModel.DELETE,
0,
1
pseudonymDeleted
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.TEAMS,
DeletionOperationModel.UPDATE,
teamsUpdated,
0
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.USER,
DeletionOperationModel.DELETE,
0,
userDeleted
);
await this.logDeletion(
deletionRequest,
DeletionDomainModel.ROCKETCHATUSER,
DeletionOperationModel.DELETE,
0,
rocketChatUserDeleted
);

if (classesUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.CLASS,
DeletionOperationModel.UPDATE,
classesUpdated,
0
);
}

if (classesUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.CLASS,
DeletionOperationModel.UPDATE,
classesUpdated,
0
);
}

if (courseGroupUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.COURSEGROUP,
DeletionOperationModel.UPDATE,
courseGroupUpdated,
0
);
}

if (courseUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.COURSE,
DeletionOperationModel.UPDATE,
courseUpdated,
0
);
}

if (fileDeleted > 0 || filesPermisionUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.FILE,
DeletionOperationModel.UPDATE,
fileDeleted + filesPermisionUpdated,
0
);
}

if (lessonUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.LESSONS,
DeletionOperationModel.UPDATE,
lessonUpdated,
0
);
}

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

if (teamsUpdated > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
DeletionDomainModel.TEAMS,
DeletionOperationModel.UPDATE,
teamsUpdated,
0
);
}

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

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

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

Expand All @@ -266,4 +223,22 @@ export class DeletionRequestUc {

return rocketChatUserDeleted;
}

private async logDeletion(
deletionRequest: DeletionRequest,
domainModel: DeletionDomainModel,
operationModel: DeletionOperationModel,
updatedCount: number,
deletedCount: number
): Promise<void> {
if (updatedCount > 0 || deletedCount > 0) {
await this.deletionLogService.createDeletionLog(
deletionRequest.id,
domainModel,
operationModel,
updatedCount,
deletedCount
);
}
}
}

0 comments on commit 16a65cd

Please sign in to comment.