Skip to content

Commit

Permalink
BC-6113 Add status and change find method in KNL Deletion (#4671)
Browse files Browse the repository at this point in the history
* add status and change find method in knl deletion module

* revert changes deletion usecase

---------

Co-authored-by: Bartosz Nowicki <[email protected]>
  • Loading branch information
WojciechGrancow and bn-pass authored Jan 15, 2024
1 parent 49cdb0f commit 22c0c76
Show file tree
Hide file tree
Showing 6 changed files with 89 additions and 19 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { DomainModel } from '@shared/domain/types';
import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '.';
import { DeletionStatusModel } from '../domain/types';

describe(DeletionRequestLogResponseBuilder, () => {
afterAll(() => {
Expand All @@ -12,15 +13,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);
});
});
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
import { DomainOperation } from '@shared/domain/interface';
import { DeletionRequestLogResponse } from '../controller/dto';
import { DeletionStatusModel } from '../domain/types';
import { DeletionTargetRef } from '../interface';

export class DeletionRequestLogResponseBuilder {
static build(
targetRef: DeletionTargetRef,
deletionPlannedAt: Date,
status: DeletionStatusModel,
statistics?: DomainOperation[]
): DeletionRequestLogResponse {
const deletionRequestLog = { targetRef, deletionPlannedAt, statistics };
const deletionRequestLog = { targetRef, deletionPlannedAt, status, statistics };

return deletionRequestLog;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ObjectId } from 'bson';
import { DomainModel } from '@shared/domain/types';
import { DeletionLogStatisticBuilder, DeletionTargetRefBuilder } from '../../builder';
import { DeletionRequestLogResponse } from './index';
import { DeletionStatusModel } from '../../domain/types';

describe(DeletionRequestLogResponse.name, () => {
describe('constructor', () => {
Expand All @@ -10,21 +11,23 @@ describe(DeletionRequestLogResponse.name, () => {
const targetRefDomain = DomainModel.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);
});
});
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { ApiProperty } from '@nestjs/swagger';
import { IsOptional } from 'class-validator';
import { DomainOperation } from '@shared/domain/interface';
import { DeletionTargetRef } from '../../interface';
import { DeletionStatusModel } from '../../domain/types';

export class DeletionRequestLogResponse {
@ApiProperty()
Expand All @@ -10,13 +11,17 @@ export class DeletionRequestLogResponse {
@ApiProperty()
deletionPlannedAt: Date;

@ApiProperty()
status: DeletionStatusModel;

@ApiProperty()
@IsOptional()
statistics?: DomainOperation[];

constructor(response: DeletionRequestLogResponse) {
this.targetRef = response.targetRef;
this.deletionPlannedAt = response.deletionPlannedAt;
this.status = response.status;
this.statistics = response.statistics;
}
}
68 changes: 63 additions & 5 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ import { DeletionRequestUc } from './deletion-request.uc';
import { deletionRequestFactory } from '../domain/testing/factory/deletion-request.factory';
import { deletionLogFactory } from '../domain/testing';
import { DeletionRequestBodyProps } from '../controller/dto';
import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '../builder';
import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '../builder';

describe(DeletionRequestUc.name, () => {
let module: TestingModule;
Expand Down Expand Up @@ -508,7 +508,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 });
Expand All @@ -526,6 +526,7 @@ describe(DeletionRequestUc.name, () => {
const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build(
targetRef,
deletionRequestExecuted.deleteAfter,
DeletionStatusModel.SUCCESS,
[statistics]
);

Expand Down Expand Up @@ -556,6 +557,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);
});
});

Expand All @@ -565,7 +620,9 @@ describe(DeletionRequestUc.name, () => {
const targetRef = DeletionTargetRefBuilder.build(deletionRequest.targetRefDomain, deletionRequest.targetRefId);
const notExecutedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build(
targetRef,
deletionRequest.deleteAfter
deletionRequest.deleteAfter,
DeletionStatusModel.REGISTERED,
[]
);

return {
Expand All @@ -574,15 +631,15 @@ describe(DeletionRequestUc.name, () => {
};
};

it('should call to deletionRequestService', async () => {
it('should call to deletionRequestService and deletionLogService', async () => {
const { deletionRequest } = setup();

deletionRequestService.findById.mockResolvedValueOnce(deletionRequest);

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 () => {
Expand All @@ -593,6 +650,7 @@ describe(DeletionRequestUc.name, () => {
const result = await uc.findById(deletionRequest.id);

expect(result).toEqual(notExecutedDeletionRequestSummary);
expect(result.status).toEqual(DeletionStatusModel.REGISTERED);
});
});
});
Expand Down
17 changes: 8 additions & 9 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ import { DomainOperationBuilder } from '@shared/domain/builder/domain-operation.
import { DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '../builder';
import { DeletionRequestBodyProps, DeletionRequestLogResponse, DeletionRequestResponse } from '../controller/dto';
import { DeletionRequest, DeletionLog } from '../domain';
import { DeletionOperationModel, DeletionStatusModel } from '../domain/types';
import { DeletionOperationModel } from '../domain/types';
import { DeletionRequestService, DeletionLogService } from '../services';

@Injectable()
Expand Down Expand Up @@ -77,16 +77,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 domainOperation: DomainOperation[] = deletionLog.map((log) =>
DomainOperationBuilder.build(log.domain, log.modifiedCount, log.deletedCount)
);
response = { ...response, statistics: domainOperation };
}
const deletionLog: DeletionLog[] = await this.deletionLogService.findByDeletionRequestId(deletionRequestId);
const domainOperation: DomainOperation[] = deletionLog.map((log) =>
DomainOperationBuilder.build(log.domain, log.modifiedCount, log.deletedCount)
);
response = { ...response, statistics: domainOperation };

return response;
}
Expand Down

0 comments on commit 22c0c76

Please sign in to comment.