Skip to content

Commit

Permalink
add status and change find method in knl deletion module
Browse files Browse the repository at this point in the history
  • Loading branch information
WojciechGrancow committed Jan 3, 2024
1 parent 80d8bd5 commit 85c8db5
Show file tree
Hide file tree
Showing 6 changed files with 88 additions and 20 deletions.
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { DeletionDomainModel } from '../domain/types';
import { DeletionDomainModel, DeletionStatusModel } from '../domain/types';
import { DeletionLogStatisticBuilder, DeletionRequestLogResponseBuilder, DeletionTargetRefBuilder } from '.';

describe(DeletionRequestLogResponseBuilder, () => {
Expand All @@ -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);
});
});
Original file line number Diff line number Diff line change
@@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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);
});
});
Expand Down
Original file line number Diff line number Diff line change
@@ -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()
Expand All @@ -9,13 +10,17 @@ export class DeletionRequestLogResponse {
@ApiProperty()
deletionPlannedAt: Date;

@ApiProperty()
status: DeletionStatusModel;

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

constructor(response: DeletionRequestLogResponse) {
this.targetRef = response.targetRef;
this.deletionPlannedAt = response.deletionPlannedAt;
this.status = response.status;
this.statistics = response.statistics;
}
}
66 changes: 62 additions & 4 deletions apps/server/src/modules/deletion/uc/deletion-request.uc.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 });
Expand All @@ -479,6 +479,7 @@ describe(DeletionRequestUc.name, () => {
const executedDeletionRequestSummary = DeletionRequestLogResponseBuilder.build(
targetRef,
deletionRequestExecuted.deleteAfter,
DeletionStatusModel.SUCCESS,
[statistics]
);

Expand Down Expand Up @@ -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);
});
});

Expand All @@ -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 {
Expand All @@ -527,15 +584,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 @@ -546,6 +603,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 @@ -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()
Expand Down Expand Up @@ -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;
}
Expand Down

0 comments on commit 85c8db5

Please sign in to comment.