From 43e97b77248f947c3361d90c7e58fc210c6c2bbe Mon Sep 17 00:00:00 2001 From: virgilchiriac Date: Fri, 13 Oct 2023 13:39:56 +0200 Subject: [PATCH] some cleanup --- .../board/controller/element.controller.ts | 2 +- .../mapper/submission-item-response.mapper.ts | 8 +++--- .../board/service/content-element.service.ts | 11 -------- .../modules/board/uc/submission-item.uc.ts | 10 ++++++-- .../domainobject/board/submission-item.do.ts | 25 ++++--------------- .../domainobject/board/types/any-board-do.ts | 2 -- .../board/types/any-content-element-do.ts | 3 --- .../entity/boardnode/types/any-board-node.ts | 10 +++++++- 8 files changed, 27 insertions(+), 44 deletions(-) diff --git a/apps/server/src/modules/board/controller/element.controller.ts b/apps/server/src/modules/board/controller/element.controller.ts index 2dacd2cf539..ef5aab6d3d6 100644 --- a/apps/server/src/modules/board/controller/element.controller.ts +++ b/apps/server/src/modules/board/controller/element.controller.ts @@ -134,7 +134,7 @@ export class ElementController { bodyParams.completed ); const mapper = SubmissionItemResponseMapper.getInstance(); - const response = mapper.mapSubmissionsToResponse(submissionItem); + const response = mapper.mapSubmissionItemToResponse(submissionItem); return response; } diff --git a/apps/server/src/modules/board/controller/mapper/submission-item-response.mapper.ts b/apps/server/src/modules/board/controller/mapper/submission-item-response.mapper.ts index b9ef34b5338..a556d1b32f2 100644 --- a/apps/server/src/modules/board/controller/mapper/submission-item-response.mapper.ts +++ b/apps/server/src/modules/board/controller/mapper/submission-item-response.mapper.ts @@ -1,8 +1,8 @@ import { FileElement, - isContent, isFileElement, isRichTextElement, + isSubmissionItemContent, RichTextElement, SubmissionItem, UserBoardRoles, @@ -26,7 +26,7 @@ export class SubmissionItemResponseMapper { public mapToResponse(submissionItems: SubmissionItem[], users: UserBoardRoles[]): SubmissionsResponse { const submissionItemsResponse: SubmissionItemResponse[] = submissionItems.map((item) => - this.mapSubmissionsToResponse(item) + this.mapSubmissionItemToResponse(item) ); const usersResponse: UserDataResponse[] = users.map((user) => this.mapUsersToResponse(user)); @@ -35,8 +35,8 @@ export class SubmissionItemResponseMapper { return response; } - public mapSubmissionsToResponse(submissionItem: SubmissionItem): SubmissionItemResponse { - const children: (FileElement | RichTextElement)[] = submissionItem.children.filter(isContent); + public mapSubmissionItemToResponse(submissionItem: SubmissionItem): SubmissionItemResponse { + const children: (FileElement | RichTextElement)[] = submissionItem.children.filter(isSubmissionItemContent); const result = new SubmissionItemResponse({ completed: submissionItem.completed, id: submissionItem.id, diff --git a/apps/server/src/modules/board/service/content-element.service.ts b/apps/server/src/modules/board/service/content-element.service.ts index 10679e416a1..4404f51fc3e 100644 --- a/apps/server/src/modules/board/service/content-element.service.ts +++ b/apps/server/src/modules/board/service/content-element.service.ts @@ -7,7 +7,6 @@ import { ContentElementType, EntityId, isAnyContentElement, - SubmissionContainerElement, SubmissionItem, } from '@shared/domain'; import { AnyElementContentBody } from '../controller/dto'; @@ -43,16 +42,6 @@ export class ContentElementService { return parent; } - async findSubmissionContainerElement(elementId: EntityId): Promise { - const element = await this.boardDoRepo.findById(elementId); - - if (!(element instanceof SubmissionContainerElement)) { - throw new NotFoundException(`There is no '${element.constructor.name}' with this id`); - } - - return element; - } - async create(parent: Card | SubmissionItem, type: ContentElementType): Promise { const element = this.contentElementFactory.build(type); parent.addChild(element); diff --git a/apps/server/src/modules/board/uc/submission-item.uc.ts b/apps/server/src/modules/board/uc/submission-item.uc.ts index d384da71465..b3690c16cf0 100644 --- a/apps/server/src/modules/board/uc/submission-item.uc.ts +++ b/apps/server/src/modules/board/uc/submission-item.uc.ts @@ -13,6 +13,8 @@ import { ContentElementType, EntityId, FileElement, + isFileElement, + isRichTextElement, isSubmissionContainerElement, isSubmissionItem, RichTextElement, @@ -91,8 +93,12 @@ export class SubmissionItemUc { // await this.checkPermission(userId, submissionItem, Action.write); const element = await this.elementService.create(submissionItem, type); - // TODO - return element as FileElement | RichTextElement; + + if (!isFileElement(element) && !isRichTextElement(element)) { + throw new UnprocessableEntityException(); + } + + return element; } private async isAuthorizedStudent(userId: EntityId, boardDo: AnyBoardDo): Promise { diff --git a/apps/server/src/shared/domain/domainobject/board/submission-item.do.ts b/apps/server/src/shared/domain/domainobject/board/submission-item.do.ts index ce5f9dcbdcb..4a96e562bb4 100644 --- a/apps/server/src/shared/domain/domainobject/board/submission-item.do.ts +++ b/apps/server/src/shared/domain/domainobject/board/submission-item.do.ts @@ -1,24 +1,8 @@ -import { - EntityId, - FileElement, - isFileElement, - isRichTextElement, - isSumbmissionItem, - RichTextElement, -} from '@shared/domain'; +import { EntityId, FileElement, isFileElement, isRichTextElement, RichTextElement } from '@shared/domain'; import { BoardComposite, BoardCompositeProps } from './board-composite.do'; import type { AnyBoardDo, BoardCompositeVisitor, BoardCompositeVisitorAsync } from './types'; export class SubmissionItem extends BoardComposite { - /* get children(): (RichTextElement | FileElement)[] { - const { children } = this.props; - if (!children) return []; - - const filteredChildren = children.filter(isFileElement || isRichTextElement); - - return filteredChildren; - } */ - get completed(): boolean { return this.props.completed; } @@ -35,10 +19,8 @@ export class SubmissionItem extends BoardComposite { this.props.userId = value; } - // eslint-disable-next-line @typescript-eslint/no-unused-vars isAllowedAsChild(child: AnyBoardDo): boolean { - // Currently submission-item rejects any children, will open in the future - const allowed = child instanceof FileElement || child instanceof RichTextElement; + const allowed = isFileElement(child) || isRichTextElement(child); return allowed; } @@ -60,3 +42,6 @@ export interface SubmissionItemProps extends BoardCompositeProps { export function isSubmissionItem(reference: unknown): reference is SubmissionItem { return reference instanceof SubmissionItem; } + +export const isSubmissionItemContent = (element: AnyBoardDo): element is RichTextElement | FileElement => + isRichTextElement(element) || isFileElement(element); diff --git a/apps/server/src/shared/domain/domainobject/board/types/any-board-do.ts b/apps/server/src/shared/domain/domainobject/board/types/any-board-do.ts index c6634e9aa0f..c3504070f36 100644 --- a/apps/server/src/shared/domain/domainobject/board/types/any-board-do.ts +++ b/apps/server/src/shared/domain/domainobject/board/types/any-board-do.ts @@ -5,5 +5,3 @@ import { SubmissionItem } from '../submission-item.do'; import { AnyContentElementDo } from './any-content-element-do'; export type AnyBoardDo = ColumnBoard | Column | Card | AnyContentElementDo | SubmissionItem; - -export const isSumbmissionItem = (element: AnyBoardDo): element is SubmissionItem => element instanceof SubmissionItem; diff --git a/apps/server/src/shared/domain/domainobject/board/types/any-content-element-do.ts b/apps/server/src/shared/domain/domainobject/board/types/any-content-element-do.ts index f47b46b40f6..614071e658c 100644 --- a/apps/server/src/shared/domain/domainobject/board/types/any-content-element-do.ts +++ b/apps/server/src/shared/domain/domainobject/board/types/any-content-element-do.ts @@ -22,6 +22,3 @@ export const isAnyContentElement = (element: AnyBoardDo): element is AnyContentE return result; }; - -export const isContent = (element: AnyBoardDo): element is RichTextElement | FileElement => - element instanceof RichTextElement || element instanceof FileElement; diff --git a/apps/server/src/shared/domain/entity/boardnode/types/any-board-node.ts b/apps/server/src/shared/domain/entity/boardnode/types/any-board-node.ts index 1b6a3991475..68110bb0fa7 100644 --- a/apps/server/src/shared/domain/entity/boardnode/types/any-board-node.ts +++ b/apps/server/src/shared/domain/entity/boardnode/types/any-board-node.ts @@ -2,11 +2,19 @@ import { CardNode, ColumnBoardNode, ColumnNode, + ExternalToolElement, FileElementNode, + LinkElementNode, RichTextElementNode, SubmissionContainerElementNode, SubmissionItemNode, } from '@shared/domain'; -export type AnyElementNode = FileElementNode | RichTextElementNode | SubmissionContainerElementNode; +export type AnyElementNode = + | FileElementNode + | RichTextElementNode + | SubmissionContainerElementNode + | ExternalToolElement + | LinkElementNode; + export type AnyBoardNode = ColumnBoardNode | ColumnNode | CardNode | AnyElementNode | SubmissionItemNode;