From 85c8db5cba08178af87da1955f5ddc96b4f9ea89 Mon Sep 17 00:00:00 2001 From: WojciechGrancow Date: Wed, 3 Jan 2024 07:41:01 +0100 Subject: [PATCH] add status and change find method in knl deletion module --- ...etion-request-log-response.builder.spec.ts | 6 +- .../deletion-request-log-response.builder.ts | 4 +- .../dto/deletion-request-log.response.spec.ts | 10 +-- .../dto/deletion-request-log.response.ts | 5 ++ .../deletion/uc/deletion-request.uc.spec.ts | 66 +++++++++++++++++-- .../deletion/uc/deletion-request.uc.ts | 17 +++-- 6 files changed, 88 insertions(+), 20 deletions(-) diff --git a/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts index 6d02894b43f..15a8c4e0f6e 100644 --- a/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.spec.ts @@ -1,4 +1,4 @@ -import { DeletionDomainModel } from '../domain/types'; +import { DeletionDomainModel, DeletionStatusModel } from '../domain/types'; import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '.'; describe(DeletionRequestLogResponseBuilder, () => { @@ -12,15 +12,17 @@ describe(DeletionRequestLogResponseBuilder, () => { const targetRefId = '653e4833cc39e5907a1e18d2'; const targetRef = DeletionTargetRefBuilder.build(targetRefDomain, targetRefId); const deletionPlannedAt = new Date(); + const status = DeletionStatusModel.SUCCESS; const modifiedCount = 0; const deletedCount = 2; const statistics = [DeletionLogStatisticBuilder.build(targetRefDomain, modifiedCount, deletedCount)]; - const result = DeletionRequestLogResponseBuilder.build(targetRef, deletionPlannedAt, statistics); + const result = DeletionRequestLogResponseBuilder.build(targetRef, deletionPlannedAt, status, statistics); // Assert expect(result.targetRef).toEqual(targetRef); expect(result.deletionPlannedAt).toEqual(deletionPlannedAt); + expect(result.status).toEqual(status); expect(result.statistics).toEqual(statistics); }); }); diff --git a/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts index be4b0ba5a96..8b4c78ec117 100644 --- a/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts +++ b/apps/server/src/modules/deletion/builder/deletion-request-log-response.builder.ts @@ -1,13 +1,15 @@ import { DeletionRequestLogResponse } from '../controller/dto'; +import { DeletionStatusModel } from '../domain/types'; import { DeletionLogStatistic, DeletionTargetRef } from '../interface'; export class DeletionRequestLogResponseBuilder { static build( targetRef: DeletionTargetRef, deletionPlannedAt: Date, + status: DeletionStatusModel, statistics?: DeletionLogStatistic[] ): DeletionRequestLogResponse { - const deletionRequestLog = { targetRef, deletionPlannedAt, statistics }; + const deletionRequestLog = { targetRef, deletionPlannedAt, status, statistics }; return deletionRequestLog; } diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts index 5036a0d39e0..7e9624652ec 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.spec.ts @@ -1,5 +1,5 @@ import { ObjectId } from 'bson'; -import { DeletionDomainModel } from '../../domain/types'; +import { DeletionDomainModel, DeletionStatusModel } from '../../domain/types'; import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../../builder'; import { DeletionRequestLogResponse } from './index'; @@ -10,21 +10,23 @@ describe(DeletionRequestLogResponse.name, () => { const targetRefDomain = DeletionDomainModel.PSEUDONYMS; const targetRefId = new ObjectId().toHexString(); const targetRef = DeletionTargetRefBuilder.build(targetRefDomain, targetRefId); + const status = DeletionStatusModel.SUCCESS; const deletionPlannedAt = new Date(); const modifiedCount = 0; const deletedCount = 2; const statistics = [DeletionLogStatisticBuilder.build(targetRefDomain, modifiedCount, deletedCount)]; - return { targetRef, deletionPlannedAt, statistics }; + return { targetRef, deletionPlannedAt, status, statistics }; }; it('should set the id', () => { - const { targetRef, deletionPlannedAt, statistics } = setup(); + const { targetRef, deletionPlannedAt, status, statistics } = setup(); - const deletionRequestLog = new DeletionRequestLogResponse({ targetRef, deletionPlannedAt, statistics }); + const deletionRequestLog = new DeletionRequestLogResponse({ targetRef, deletionPlannedAt, status, statistics }); expect(deletionRequestLog.targetRef).toEqual(targetRef); expect(deletionRequestLog.deletionPlannedAt).toEqual(deletionPlannedAt); + expect(deletionRequestLog.status).toEqual(status); expect(deletionRequestLog.statistics).toEqual(statistics); }); }); diff --git a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts index 3619bebace8..f12d6046b73 100644 --- a/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts +++ b/apps/server/src/modules/deletion/controller/dto/deletion-request-log.response.ts @@ -1,6 +1,7 @@ import { ApiProperty } from '@nestjs/swagger'; import { IsOptional } from 'class-validator'; import { DeletionLogStatistic, DeletionTargetRef } from '../../interface'; +import { DeletionStatusModel } from '../../domain/types'; export class DeletionRequestLogResponse { @ApiProperty() @@ -9,6 +10,9 @@ export class DeletionRequestLogResponse { @ApiProperty() deletionPlannedAt: Date; + @ApiProperty() + status: DeletionStatusModel; + @ApiProperty() @IsOptional() statistics?: DeletionLogStatistic[]; @@ -16,6 +20,7 @@ export class DeletionRequestLogResponse { constructor(response: DeletionRequestLogResponse) { this.targetRef = response.targetRef; this.deletionPlannedAt = response.deletionPlannedAt; + this.status = response.status; this.statistics = response.statistics; } } 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 bcdba26070a..e7dfffe154f 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 @@ -461,7 +461,7 @@ describe(DeletionRequestUc.name, () => { }); describe('findById', () => { - describe('when searching for logs for deletionRequest which was executed', () => { + describe('when searching for logs for deletionRequest which was executed with success status', () => { const setup = () => { const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.SUCCESS }); const deletionLogExecuted = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); @@ -479,6 +479,7 @@ describe(DeletionRequestUc.name, () => { const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( targetRef, deletionRequestExecuted.deleteAfter, + DeletionStatusModel.SUCCESS, [statistics] ); @@ -509,6 +510,60 @@ describe(DeletionRequestUc.name, () => { const result = await uc.findById(deletionRequestExecuted.id); expect(result).toEqual(executedDeletionRequestSummary); + expect(result.status).toEqual(DeletionStatusModel.SUCCESS); + }); + }); + + describe('when searching for logs for deletionRequest which was executed with failed status', () => { + const setup = () => { + const deletionRequestExecuted = deletionRequestFactory.build({ status: DeletionStatusModel.FAILED }); + const deletionLogExecuted = deletionLogFactory.build({ deletionRequestId: deletionRequestExecuted.id }); + + const targetRef = DeletionTargetRefBuilder.build( + deletionRequestExecuted.targetRefDomain, + deletionRequestExecuted.targetRefId + ); + const statistics = DeletionLogStatisticBuilder.build( + deletionLogExecuted.domain, + deletionLogExecuted.modifiedCount, + deletionLogExecuted.deletedCount + ); + + const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( + targetRef, + deletionRequestExecuted.deleteAfter, + DeletionStatusModel.FAILED, + [statistics] + ); + + return { + deletionRequestExecuted, + executedDeletionRequestSummary, + deletionLogExecuted, + }; + }; + + it('should call to deletionRequestService and deletionLogService', async () => { + const { deletionRequestExecuted } = setup(); + + deletionRequestService.findById.mockResolvedValueOnce(deletionRequestExecuted); + + await uc.findById(deletionRequestExecuted.id); + + expect(deletionRequestService.findById).toHaveBeenCalledWith(deletionRequestExecuted.id); + expect(deletionLogService.findByDeletionRequestId).toHaveBeenCalledWith(deletionRequestExecuted.id); + }); + + it('should return object with summary of deletionRequest', async () => { + const { deletionRequestExecuted, deletionLogExecuted, executedDeletionRequestSummary } = setup(); + + deletionRequestService.findById.mockResolvedValueOnce(deletionRequestExecuted); + deletionLogService.findByDeletionRequestId.mockResolvedValueOnce([deletionLogExecuted]); + + const result = await uc.findById(deletionRequestExecuted.id); + + expect(result).toEqual(executedDeletionRequestSummary); + expect(result.status).toEqual(DeletionStatusModel.FAILED); }); }); @@ -518,7 +573,9 @@ describe(DeletionRequestUc.name, () => { const targetRef = DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId); const notExecutedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build( targetRef, - deletionRequest.deleteAfter + deletionRequest.deleteAfter, + DeletionStatusModel.REGISTERED, + [] ); return { @@ -527,7 +584,7 @@ describe(DeletionRequestUc.name, () => { }; }; - it('should call to deletionRequestService', async () => { + it('should call to deletionRequestService and deletionLogService', async () => { const { deletionRequest } = setup(); deletionRequestService.findById.mockResolvedValueOnce(deletionRequest); @@ -535,7 +592,7 @@ describe(DeletionRequestUc.name, () => { await uc.findById(deletionRequest.id); expect(deletionRequestService.findById).toHaveBeenCalledWith(deletionRequest.id); - expect(deletionLogService.findByDeletionRequestId).not.toHaveBeenCalled(); + expect(deletionLogService.findByDeletionRequestId).toHaveBeenCalledWith(deletionRequest.id); }); it('should return object with summary of deletionRequest', async () => { @@ -546,6 +603,7 @@ describe(DeletionRequestUc.name, () => { const result = await uc.findById(deletionRequest.id); expect(result).toEqual(notExecutedDeletionRequestSummary); + expect(result.status).toEqual(DeletionStatusModel.REGISTERED); }); }); }); 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 7d705b23b09..ca579bb6e46 100644 --- a/apps/server/src/modules/deletion/uc/deletion-request.uc.ts +++ b/apps/server/src/modules/deletion/uc/deletion-request.uc.ts @@ -17,7 +17,7 @@ import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, Deletio import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto'; import { DeletionLogStatistic } from './interface/interfaces'; import { DeletionRequest, DeletionLog } from '../domain'; -import { DeletionDomainModel, DeletionOperationModel, DeletionStatusModel } from '../domain/types'; +import { DeletionDomainModel, DeletionOperationModel } from '../domain/types'; import { DeletionRequestService, DeletionLogService } from '../services'; @Injectable() @@ -74,16 +74,15 @@ export class DeletionRequestUc { const deletionRequest: DeletionRequest = await this.deletionRequestService.findById(deletionRequestId); let response: DeletionRequestLogResponse = DeletionRequestLogResponseBuilder.build( DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId), - deletionRequest.deleteAfter + deletionRequest.deleteAfter, + deletionRequest.status ); - if (deletionRequest.status === DeletionStatusModel.SUCCESS) { - const deletionLog: DeletionLog[] = await this.deletionLogService.findByDeletionRequestId(deletionRequestId); - const deletionLogStatistic: DeletionLogStatistic[] = deletionLog.map((log) => - DeletionLogStatisticBuilder.build(log.domain, log.modifiedCount, log.deletedCount) - ); - response = { ...response, statistics: deletionLogStatistic }; - } + const deletionLog: DeletionLog[] = await this.deletionLogService.findByDeletionRequestId(deletionRequestId); + const deletionLogStatistic: DeletionLogStatistic[] = deletionLog.map((log) => + DeletionLogStatisticBuilder.build(log.domain, log.modifiedCount, log.deletedCount) + ); + response = { ...response, statistics: deletionLogStatistic }; return response; }