From 0cf051c65801a4480e12e56af885781106489f67 Mon Sep 17 00:00:00 2001 From: blazejpass Date: Mon, 6 May 2024 13:18:40 +0200 Subject: [PATCH 1/4] initial testing of recursive deriving of statuses --- .../board-do-copy-service/recursive-copy.visitor.ts | 8 +++++++- .../modules/copy-helper/service/copy-helper.service.ts | 8 ++++++++ .../src/modules/learnroom/service/course-copy.service.ts | 2 -- 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts b/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts index 95cd09ce1e2..7667a01b74f 100644 --- a/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts +++ b/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts @@ -148,7 +148,13 @@ export class RecursiveCopyVisitor implements BoardCompositeVisitorAsync { this.resultMap.set(original.id, { copyEntity: copy, type: CopyElementType.DRAWING_ELEMENT, - status: CopyStatusEnum.SUCCESS, + status: CopyStatusEnum.PARTIAL, + elements: [ + { + type: CopyElementType.CONTENT, + status: CopyStatusEnum.FAIL, + }, + ], }); this.copyMap.set(original.id, copy); diff --git a/apps/server/src/modules/copy-helper/service/copy-helper.service.ts b/apps/server/src/modules/copy-helper/service/copy-helper.service.ts index b3a42dac7df..b9008b97c58 100644 --- a/apps/server/src/modules/copy-helper/service/copy-helper.service.ts +++ b/apps/server/src/modules/copy-helper/service/copy-helper.service.ts @@ -10,6 +10,14 @@ export class CopyHelperService { deriveStatusFromElements(elements: CopyStatus[]): CopyStatusEnum { const elementsStatuses = elements.map((el) => el.status); + for (const element of elements) { + if (element.elements?.length) { + element.status = this.deriveStatusFromElements(element.elements); + } else { + return element.status; + } + } + const filtered = elementsStatuses.filter((status) => status !== CopyStatusEnum.NOT_DOING); if (filtered.length > 0) { diff --git a/apps/server/src/modules/learnroom/service/course-copy.service.ts b/apps/server/src/modules/learnroom/service/course-copy.service.ts index e176ef3ea37..f5e6a2cb38e 100644 --- a/apps/server/src/modules/learnroom/service/course-copy.service.ts +++ b/apps/server/src/modules/learnroom/service/course-copy.service.ts @@ -71,9 +71,7 @@ export class CourseCopyService { const boardStatus = await this.boardCopyService.copyBoard({ originalBoard, destinationCourse: courseCopy, user }); const finishedCourseCopy = await this.finishCourseCopying(courseCopy); - const courseStatus = this.deriveCourseStatus(originalCourse, finishedCourseCopy, boardStatus); - return courseStatus; } From d52215cb9f946843fe0405ad5e0a9a6730456cf5 Mon Sep 17 00:00:00 2001 From: blazejpass Date: Mon, 6 May 2024 14:44:44 +0200 Subject: [PATCH 2/4] Returning all updated status structure --- .../service/board-do-copy-service/recursive-copy.visitor.ts | 6 ------ .../src/modules/copy-helper/service/copy-helper.service.ts | 5 +---- .../src/modules/learnroom/service/course-copy.service.ts | 2 ++ 3 files changed, 3 insertions(+), 10 deletions(-) diff --git a/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts b/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts index 7667a01b74f..833792c3fb8 100644 --- a/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts +++ b/apps/server/src/modules/board/service/board-do-copy-service/recursive-copy.visitor.ts @@ -149,12 +149,6 @@ export class RecursiveCopyVisitor implements BoardCompositeVisitorAsync { copyEntity: copy, type: CopyElementType.DRAWING_ELEMENT, status: CopyStatusEnum.PARTIAL, - elements: [ - { - type: CopyElementType.CONTENT, - status: CopyStatusEnum.FAIL, - }, - ], }); this.copyMap.set(original.id, copy); diff --git a/apps/server/src/modules/copy-helper/service/copy-helper.service.ts b/apps/server/src/modules/copy-helper/service/copy-helper.service.ts index b9008b97c58..697a22e7a0e 100644 --- a/apps/server/src/modules/copy-helper/service/copy-helper.service.ts +++ b/apps/server/src/modules/copy-helper/service/copy-helper.service.ts @@ -8,16 +8,13 @@ const isAtLeastPartialSuccessfull = (status) => status === CopyStatusEnum.PARTIA @Injectable() export class CopyHelperService { deriveStatusFromElements(elements: CopyStatus[]): CopyStatusEnum { - const elementsStatuses = elements.map((el) => el.status); - for (const element of elements) { if (element.elements?.length) { element.status = this.deriveStatusFromElements(element.elements); - } else { - return element.status; } } + const elementsStatuses = elements.map((el) => el.status); const filtered = elementsStatuses.filter((status) => status !== CopyStatusEnum.NOT_DOING); if (filtered.length > 0) { diff --git a/apps/server/src/modules/learnroom/service/course-copy.service.ts b/apps/server/src/modules/learnroom/service/course-copy.service.ts index f5e6a2cb38e..e176ef3ea37 100644 --- a/apps/server/src/modules/learnroom/service/course-copy.service.ts +++ b/apps/server/src/modules/learnroom/service/course-copy.service.ts @@ -71,7 +71,9 @@ export class CourseCopyService { const boardStatus = await this.boardCopyService.copyBoard({ originalBoard, destinationCourse: courseCopy, user }); const finishedCourseCopy = await this.finishCourseCopying(courseCopy); + const courseStatus = this.deriveCourseStatus(originalCourse, finishedCourseCopy, boardStatus); + return courseStatus; } From d08b72cd500327bd0ec628c7c7b0b5b8a4a4135f Mon Sep 17 00:00:00 2001 From: blazejpass Date: Wed, 8 May 2024 12:33:46 +0200 Subject: [PATCH 3/4] Add and adjust tests --- .../board-do-copy.service.spec.ts | 4 +- .../service/copy-helper.service.spec.ts | 44 +++++++++++++++++++ 2 files changed, 46 insertions(+), 2 deletions(-) diff --git a/apps/server/src/modules/board/service/board-do-copy-service/board-do-copy.service.spec.ts b/apps/server/src/modules/board/service/board-do-copy-service/board-do-copy.service.spec.ts index 0023ece7ad2..d3417a587cc 100644 --- a/apps/server/src/modules/board/service/board-do-copy-service/board-do-copy.service.spec.ts +++ b/apps/server/src/modules/board/service/board-do-copy-service/board-do-copy.service.spec.ts @@ -511,12 +511,12 @@ describe('recursive board copy visitor', () => { expect(copy.id).not.toEqual(original.id); }); - it('should show status successful', async () => { + it('should show status partial', async () => { const { original, fileCopyService } = setup(); const result = await service.copy({ original, fileCopyService }); - expect(result.status).toEqual(CopyStatusEnum.SUCCESS); + expect(result.status).toEqual(CopyStatusEnum.PARTIAL); }); it('should show type RichTextElement', async () => { diff --git a/apps/server/src/modules/copy-helper/service/copy-helper.service.spec.ts b/apps/server/src/modules/copy-helper/service/copy-helper.service.spec.ts index 337178ecbb8..27276237119 100644 --- a/apps/server/src/modules/copy-helper/service/copy-helper.service.spec.ts +++ b/apps/server/src/modules/copy-helper/service/copy-helper.service.spec.ts @@ -13,6 +13,21 @@ function createStates(elementStates: CopyStatusEnum[]): CopyStatus[] { return elementState; }); } +function createNestedStates(elementStates: CopyStatusEnum[]): CopyStatus { + const elementState = elementStates.shift(); + const element: CopyStatus = { + title: `title-${Math.floor(Math.random() * 1000)}-${elementStates.length}`, + type: CopyElementType.LEAF, + status: CopyStatusEnum.SUCCESS, + }; + + if (elementState) { + element.status = elementState; + element.elements = elementStates.length ? [createNestedStates(elementStates)] : []; + } + + return element; +} describe('copy helper service', () => { let module: TestingModule; @@ -35,6 +50,23 @@ describe('copy helper service', () => { }); describe('deriveStatusFromElements', () => { + describe('setup cases', () => { + it('should run method multiple times for nested structure', () => { + const derivedStatusSpy = jest.spyOn(copyHelperService, 'deriveStatusFromElements'); + const element = createNestedStates([ + CopyStatusEnum.SUCCESS, + CopyStatusEnum.SUCCESS, + CopyStatusEnum.SUCCESS, + CopyStatusEnum.SUCCESS, + ]); + + copyHelperService.deriveStatusFromElements([element]); + + expect(derivedStatusSpy).toHaveBeenCalledTimes(4); + derivedStatusSpy.mockRestore(); + }); + }); + describe('successful cases', () => { it('should return success, if no elements were given', () => { const derivedStatus = copyHelperService.deriveStatusFromElements([]); @@ -92,6 +124,18 @@ describe('copy helper service', () => { expect(derivedStatus).toEqual(CopyStatusEnum.FAIL); }); + + it('should return fail if the last and only nested child is FAIL ', () => { + const element = createNestedStates([ + CopyStatusEnum.SUCCESS, + CopyStatusEnum.SUCCESS, + CopyStatusEnum.SUCCESS, + CopyStatusEnum.FAIL, + ]); + const derivedStatus = copyHelperService.deriveStatusFromElements([element]); + + expect(derivedStatus).toEqual(CopyStatusEnum.FAIL); + }); }); describe('partial cases', () => { From d89ab1b4c5cb3818ee2c967648c8bdae75c6158e Mon Sep 17 00:00:00 2001 From: blazejpass Date: Thu, 9 May 2024 09:12:12 +0200 Subject: [PATCH 4/4] Remove unnecessary parameter for flattened array of types --- .../modules/copy-helper/dto/copy.response.ts | 7 ------- .../modules/copy-helper/mapper/copy.mapper.ts | 17 +---------------- .../learnroom/controller/rooms.controller.ts | 1 - 3 files changed, 1 insertion(+), 24 deletions(-) diff --git a/apps/server/src/modules/copy-helper/dto/copy.response.ts b/apps/server/src/modules/copy-helper/dto/copy.response.ts index d3fbb33c8da..549dcac7014 100644 --- a/apps/server/src/modules/copy-helper/dto/copy.response.ts +++ b/apps/server/src/modules/copy-helper/dto/copy.response.ts @@ -45,11 +45,4 @@ export class CopyApiResponse { description: 'List of included sub elements with recursive type structure', }) elements?: CopyApiResponse[]; - - @ApiPropertyOptional({ - isArray: true, - enum: CopyElementType, - description: 'Array with listed types of all sub elements', - }) - elementsTypes?: CopyElementType[]; } diff --git a/apps/server/src/modules/copy-helper/mapper/copy.mapper.ts b/apps/server/src/modules/copy-helper/mapper/copy.mapper.ts index 0e65613fa8f..581e33161bd 100644 --- a/apps/server/src/modules/copy-helper/mapper/copy.mapper.ts +++ b/apps/server/src/modules/copy-helper/mapper/copy.mapper.ts @@ -5,7 +5,7 @@ import { TaskCopyParentParams } from '@modules/task/types'; import { LessonEntity, Task } from '@shared/domain/entity'; import { EntityId } from '@shared/domain/types'; import { CopyApiResponse } from '../dto/copy.response'; -import { CopyElementType, CopyStatus, CopyStatusEnum } from '../types/copy.types'; +import { CopyStatus, CopyStatusEnum } from '../types/copy.types'; export class CopyMapper { static mapToResponse(copyStatus: CopyStatus): CopyApiResponse { @@ -46,19 +46,4 @@ export class CopyMapper { return dto; } - - static mapElementsToTypes(element: CopyStatus, types: CopyElementType[] = []): CopyElementType[] { - const indexOfFound = types.indexOf(element.type); - if (indexOfFound === -1) { - types.push(element.type); - } - - if (element.elements) { - element.elements.forEach((child) => { - this.mapElementsToTypes(child, types); - }); - } - - return types; - } } diff --git a/apps/server/src/modules/learnroom/controller/rooms.controller.ts b/apps/server/src/modules/learnroom/controller/rooms.controller.ts index 7caa9149d87..7e73104e0d1 100644 --- a/apps/server/src/modules/learnroom/controller/rooms.controller.ts +++ b/apps/server/src/modules/learnroom/controller/rooms.controller.ts @@ -69,7 +69,6 @@ export class RoomsController { ): Promise { const copyStatus = await this.courseCopyUc.copyCourse(currentUser.userId, urlParams.roomId); const dto = CopyMapper.mapToResponse(copyStatus); - dto.elementsTypes = CopyMapper.mapElementsToTypes(copyStatus); return dto; }