From 5b88b9dc9e3655ceefd6a2a972cc38013d5da6b2 Mon Sep 17 00:00:00 2001 From: Hussam Kayed Date: Fri, 18 Oct 2024 13:09:05 +0200 Subject: [PATCH] ew-1009: proper mapping for all DTOs implemented --- .../card-client/dto/card-response.dto.ts | 2 +- ...rative-text-editor-element-response.dto.ts | 2 +- .../dto/drawing-element-response.dto.ts | 4 +- .../mapper/card-response.mapper.ts | 200 +++++++++++++++++- .../types/card-content-elements-inner.type.ts | 17 ++ 5 files changed, 215 insertions(+), 10 deletions(-) create mode 100644 apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/types/card-content-elements-inner.type.ts diff --git a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/card-response.dto.ts b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/card-response.dto.ts index 5220b4ff7f1..43e20b43b9e 100644 --- a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/card-response.dto.ts +++ b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/card-response.dto.ts @@ -19,7 +19,7 @@ export class CardResponseDto { id: string, title: string, height: number, - elements: Array, + elements: CardResponseElementsInnerDto[], visibilitySettings: VisibilitySettingsResponseDto, timeStamps: TimestampResponseDto ) { diff --git a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/collaborative-text-editor-element-response.dto.ts b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/collaborative-text-editor-element-response.dto.ts index fdb80af6a70..7d864480759 100644 --- a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/collaborative-text-editor-element-response.dto.ts +++ b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/collaborative-text-editor-element-response.dto.ts @@ -10,7 +10,7 @@ export class CollaborativeTextEditorElementResponseDto { content: object; - constructor(id: string, type: ContentElementType, timestamps: TimestampResponseDto, content: object) { + constructor(id: string, type: ContentElementType, content: object, timestamps: TimestampResponseDto) { this.id = id; this.type = type; this.timestamps = timestamps; diff --git a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/drawing-element-response.dto.ts b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/drawing-element-response.dto.ts index 7faeab24465..a658f9de525 100644 --- a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/drawing-element-response.dto.ts +++ b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/dto/drawing-element-response.dto.ts @@ -14,8 +14,8 @@ export class DrawingElementResponseDto { constructor( id: string, type: ContentElementType, - timestamps: TimestampResponseDto, - content: DrawingElementContentDto + content: DrawingElementContentDto, + timestamps: TimestampResponseDto ) { this.id = id; this.type = type; diff --git a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/mapper/card-response.mapper.ts b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/mapper/card-response.mapper.ts index b8b02d08621..0e9edde6f31 100644 --- a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/mapper/card-response.mapper.ts +++ b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/mapper/card-response.mapper.ts @@ -1,17 +1,205 @@ -import { CardResponse } from '../cards-api-client'; +import { + CardResponse, + CardResponseElementsInner, + VisibilitySettingsResponse, + TimestampsResponse, + DeletedElementResponse, + SubmissionContainerElementResponse, + DrawingElementResponse, + ExternalToolElementResponse, + FileElementResponse, + LinkElementResponse, + RichTextElementResponse, +} from '../cards-api-client'; +import { CardResponseElementsInnerDto } from '../dto/card-response-elements-inner.dto'; import { CardResponseDto } from '../dto/card-response.dto'; +import { CollaborativeTextEditorElementResponseDto } from '../dto/collaborative-text-editor-element-response.dto'; +import { DeletedElementResponseDto } from '../dto/deleted-element-response.dto'; +import { DrawingElementContentDto } from '../dto/drawing-element-content.dto'; +import { DrawingElementResponseDto } from '../dto/drawing-element-response.dto'; +import { ExternalToolElementContentDto } from '../dto/external-tool-element-content.dto'; +import { ExternalToolElementResponseDto } from '../dto/external-tool-element-response.dto'; +import { FileElementContentDto } from '../dto/file-element-content.dto'; +import { FileElementResponseDto } from '../dto/file-element-response.dto'; +import { LinkElementContentDto } from '../dto/link-element-content.dto'; +import { LinkElementResponseDto } from '../dto/link-element-response.dto'; +import { RichTextElementContentDto } from '../dto/rich-text-element-content.dto'; +import { RichTextElementResponseDto } from '../dto/rich-text-element-response.dto'; +import { SubmissionContainerElementResponseDto } from '../dto/submission-container-element-response.dto'; +import { ContentElementType } from '../enums/content-element-type.enum'; +import { SubmissionContainerElementContentDto } from '../dto/submission-container-element-content.dto'; +import { DeletedElementContentDto } from '../dto/deleted-element-content.dto'; +import { VisibilitySettingsResponseDto } from '../dto/visibility-settings-response.dto'; +import { TimestampResponseDto } from '../dto/timestamp-response.dto'; +import { CardContentElementInner } from '../types/card-content-elements-inner.type'; export class CardResponseMapper { - public static mapToCardResponseDto(cardResponse: CardResponse) { + public static mapToCardResponseDto(cardResponse: CardResponse): CardResponseDto { return new CardResponseDto( cardResponse.id, cardResponse.title!, cardResponse.height, - cardResponse.elements, - cardResponse.visibilitySettings, - cardResponse.timestamps + this.mapToCardResponseElementsInnerDto(cardResponse.elements), + this.mapToVisibilitySettingsDto(cardResponse.visibilitySettings), + this.mapToTimestampDto(cardResponse.timestamps) ); } - private static mapTo + private static mapToCardResponseElementsInnerDto( + cardResponseElementsInner: CardResponseElementsInner[] + ): CardResponseElementsInnerDto[] { + const elements: CardResponseElementsInnerDto[] = []; + + cardResponseElementsInner.forEach((element) => { + switch (element.type) { + case ContentElementType.COLLABORATIVE_TEXT_EDITOR: + elements.push( + new CollaborativeTextEditorElementResponseDto( + element.id, + ContentElementType.COLLABORATIVE_TEXT_EDITOR, + this.mapToElementsContent(element, ContentElementType.COLLABORATIVE_TEXT_EDITOR), + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.DELETED: + elements.push( + new DeletedElementResponseDto( + element.id, + ContentElementType.DELETED, + this.mapToElementsContent(element, ContentElementType.DELETED) as DeletedElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.SUBMISSION_CONTAINER: + elements.push( + new SubmissionContainerElementResponseDto( + element.id, + ContentElementType.SUBMISSION_CONTAINER, + this.mapToElementsContent( + element, + ContentElementType.SUBMISSION_CONTAINER + ) as SubmissionContainerElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.DRAWING: + elements.push( + new DrawingElementResponseDto( + element.id, + ContentElementType.DRAWING, + this.mapToElementsContent(element, ContentElementType.DRAWING) as DrawingElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.EXTERNAL_TOOL: + elements.push( + new ExternalToolElementResponseDto( + element.id, + ContentElementType.EXTERNAL_TOOL, + this.mapToElementsContent(element, ContentElementType.EXTERNAL_TOOL) as ExternalToolElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.FILE: + elements.push( + new FileElementResponseDto( + element.id, + ContentElementType.FILE, + this.mapToElementsContent(element, ContentElementType.FILE) as FileElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.LINK: + elements.push( + new LinkElementResponseDto( + element.id, + ContentElementType.LINK, + this.mapToElementsContent(element, ContentElementType.LINK) as LinkElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + case ContentElementType.RICH_TEXT: + elements.push( + new RichTextElementResponseDto( + element.id, + ContentElementType.RICH_TEXT, + this.mapToElementsContent(element, ContentElementType.RICH_TEXT) as RichTextElementContentDto, + this.mapToTimestampDto(element.timestamps) + ) + ); + break; + default: + break; + } + }); + return elements; + } + + private static mapToElementsContent( + response: CardResponseElementsInner, + elementType: ContentElementType + ): CardContentElementInner { + switch (elementType) { + case ContentElementType.COLLABORATIVE_TEXT_EDITOR: + return {}; + case ContentElementType.DELETED: { + const deletedElementContent: DeletedElementResponse = response as DeletedElementResponse; + return new DeletedElementContentDto( + deletedElementContent.content.title, + ContentElementType.DELETED, + deletedElementContent.content.description + ); + } + case ContentElementType.SUBMISSION_CONTAINER: { + const submissionContainerResponse: SubmissionContainerElementResponse = + response as SubmissionContainerElementResponse; + return new SubmissionContainerElementContentDto(submissionContainerResponse.content.dueDate); + } + case ContentElementType.DRAWING: { + const drawingResponse: DrawingElementResponse = response as DrawingElementResponse; + return new DrawingElementContentDto(drawingResponse.content.description); + } + case ContentElementType.EXTERNAL_TOOL: { + const externalToolResponse: ExternalToolElementResponse = response as ExternalToolElementResponse; + return new ExternalToolElementContentDto(externalToolResponse.content.contextExternalToolId); + } + case ContentElementType.FILE: { + const fileResponse: FileElementResponse = response as FileElementResponse; + return new FileElementContentDto(fileResponse.content.caption, fileResponse.content.alternativeText); + } + case ContentElementType.LINK: { + const linkElementResponse: LinkElementResponse = response as LinkElementResponse; + return new LinkElementContentDto( + linkElementResponse.content.url, + linkElementResponse.content.title, + linkElementResponse.content.description!, + linkElementResponse.content.imageUrl as string + ); + } + case ContentElementType.RICH_TEXT: { + const richTextResponse: RichTextElementResponse = response as RichTextElementResponse; + return new RichTextElementContentDto(richTextResponse.content.text, richTextResponse.content.inputFormat); + } + default: + break; + } + return {}; + } + + private static mapToVisibilitySettingsDto( + visibilitySettings: VisibilitySettingsResponse + ): VisibilitySettingsResponseDto { + return new VisibilitySettingsResponseDto(visibilitySettings.publishedAt as string); + } + + private static mapToTimestampDto(timestamp: TimestampsResponse): TimestampResponseDto { + return new TimestampResponseDto(timestamp.lastUpdatedAt, timestamp.createdAt, timestamp.deletedAt!); + } } diff --git a/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/types/card-content-elements-inner.type.ts b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/types/card-content-elements-inner.type.ts new file mode 100644 index 00000000000..b8a32bc9479 --- /dev/null +++ b/apps/server/src/modules/common-cartridge/common-cartridge-client/card-client/types/card-content-elements-inner.type.ts @@ -0,0 +1,17 @@ +import { DeletedElementContentDto } from '../dto/deleted-element-content.dto'; +import { DrawingElementContentDto } from '../dto/drawing-element-content.dto'; +import { ExternalToolElementContentDto } from '../dto/external-tool-element-content.dto'; +import { FileElementContentDto } from '../dto/file-element-content.dto'; +import { LinkElementContentDto } from '../dto/link-element-content.dto'; +import { RichTextElementContentDto } from '../dto/rich-text-element-content.dto'; +import { SubmissionContainerElementContentDto } from '../dto/submission-container-element-content.dto'; + +export type CardContentElementInner = + | LinkElementContentDto + | DeletedElementContentDto + | DrawingElementContentDto + | ExternalToolElementContentDto + | FileElementContentDto + | RichTextElementContentDto + | SubmissionContainerElementContentDto + | object;