From a1ca1556ba0bf9911d737a1342f57d3214a375e6 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Mon, 6 Nov 2023 16:32:53 +0100 Subject: [PATCH] changing limit parameter and changing in useCases --- .../repo/deletion-request.repo.spec.ts | 2 +- .../deletion/repo/deletion-request.repo.ts | 8 +- .../services/deletion-request.service.ts | 6 +- .../deletion/uc/deletion-request.uc.spec.ts | 2 +- .../deletion/uc/deletion-request.uc.ts | 172 ++++++++---------- 5 files changed, 83 insertions(+), 107 deletions(-) diff --git a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts index 0e296a3406d..1883a581b2e 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request.repo.spec.ts @@ -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); diff --git a/apps/server/src/modules/deletion/repo/deletion-request.repo.ts b/apps/server/src/modules/deletion/repo/deletion-request.repo.ts index d762d19f67f..5964115c852 100644 --- a/apps/server/src/modules/deletion/repo/deletion-request.repo.ts +++ b/apps/server/src/modules/deletion/repo/deletion-request.repo.ts @@ -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'; @@ -30,15 +30,13 @@ export class DeletionRequestRepo { await this.em.flush(); } - async findAllItemsToExecution(options?: IFindOptions): Promise { + async findAllItemsToExecution(limit?: number): Promise { 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, }); diff --git a/apps/server/src/modules/deletion/services/deletion-request.service.ts b/apps/server/src/modules/deletion/services/deletion-request.service.ts index b04a29f68e4..d3f8da4c708 100644 --- a/apps/server/src/modules/deletion/services/deletion-request.service.ts +++ b/apps/server/src/modules/deletion/services/deletion-request.service.ts @@ -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'; @@ -39,8 +39,8 @@ export class DeletionRequestService { return deletionRequest; } - async findAllItemsToExecute(options?: IFindOptions): Promise { - const itemsToDelete: DeletionRequest[] = await this.deletionRequestRepo.findAllItemsToExecution(options); + async findAllItemsToExecute(limit?: number): Promise { + const itemsToDelete: DeletionRequest[] = await this.deletionRequestRepo.findAllItemsToExecution(limit); return itemsToDelete; } diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts index 340df09200f..e328c831ac1 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts @@ -294,7 +294,7 @@ describe(DeletionRequestUc.name, () => { await uc.executeDeletionRequests(); - expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(10); + expect(deletionLogService.createDeletionLog).toHaveBeenCalledTimes(9); }); }); diff --git a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts index 0b3ed0bd2e1..3a1b5d4b07b 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -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'; @@ -62,9 +62,9 @@ export class DeletionRequestUc { return result; } - async executeDeletionRequests(options?: IFindOptions): Promise { + async executeDeletionRequests(limit?: number): Promise { const deletionRequestToExecution: DeletionRequest[] = await this.deletionRequestService.findAllItemsToExecute( - options + limit ); for (const req of deletionRequestToExecution) { @@ -128,103 +128,63 @@ export class DeletionRequestUc { teamsUpdated, userDeleted, ]) => { - 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 ); - - 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 - ); - } await this.deletionRequestService.markDeletionRequestAsExecuted(deletionRequest.id); @@ -235,4 +195,22 @@ export class DeletionRequestUc { await this.deletionRequestService.markDeletionRequestAsFailed(deletionRequest.id); }); } + + private async logDeletion( + deletionRequest: DeletionRequest, + domainModel: DeletionDomainModel, + operationModel: DeletionOperationModel, + updatedCount: number, + deletedCount: number + ): Promise { + if (updatedCount > 0 || deletedCount > 0) { + await this.deletionLogService.createDeletionLog( + deletionRequest.id, + domainModel, + operationModel, + updatedCount, + deletedCount + ); + } + } }