Skip to content

Commit

Permalink
EW-876: Fix import of card titles.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Jun 5, 2024
1 parent 3fe4a32 commit 5b65f03
Show file tree
Hide file tree
Showing 5 changed files with 89 additions and 8 deletions.
Binary file not shown.
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,25 @@ describe('CommonCartridgeImportMapper', () => {
});
});

// AI next 17 lines
describe('mapOrganizationToTextElement', () => {
describe('when organization is provided', () => {
const setup = () => setupOrganization();

it('should map organization to text element', () => {
const { organization } = setup();

const result = sut.mapOrganizationToTextElement(organization);

expect(result).toBeInstanceOf(RichTextContentBody);
expect(result).toEqual<RichTextContentBody>({
text: `<b>${organization.title}</b>`,
inputFormat: InputFormat.RICH_TEXT_CK5_SIMPLE,
});
});
});
});

describe('mapResourceTypeToContentElementType', () => {
describe('when resourceType is provided', () => {
it('should return undefined', () => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,14 @@ export class CommonCartridgeImportMapper {
};
}

public mapOrganizationToTextElement(organization: CommonCartridgeOrganizationProps): AnyElementContentBody {
const body = new RichTextContentBody();
body.text = `<b>${organization.title}</b>`;
body.inputFormat = InputFormat.RICH_TEXT_CK5_SIMPLE;

return body;
}

public mapResourceTypeToContentElementType(
resourceType: CommonCartridgeResourceTypeV1P1 | undefined
): ContentElementType | undefined {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -146,7 +146,7 @@ describe('CommonCartridgeImportService', () => {

await sut.importFile(user, buffer);

expect(columnServiceMock.create).toHaveBeenCalledTimes(4);
expect(columnServiceMock.create).toHaveBeenCalledTimes(6);
});

it('should create cards', async () => {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
import { Injectable } from '@nestjs/common';
import { BoardExternalReferenceType, BoardLayout, Column, ColumnBoard } from '@shared/domain/domainobject';
import {
BoardExternalReferenceType,
BoardLayout,
Card,
Column,
ColumnBoard,
ContentElementType,
} from '@shared/domain/domainobject';
import { Course, User } from '@shared/domain/entity';
import { CardService, ColumnBoardService, ColumnService, ContentElementService } from '@src/modules/board';
import {
Expand Down Expand Up @@ -87,7 +94,7 @@ export class CommonCartridgeImportService {
columnProps: CommonCartridgeOrganizationProps
): Promise<void> {
const column = await this.columnService.create(columnBoard, this.mapper.mapOrganizationToColumn(columnProps));
await this.createCard(parser, column, columnProps, false);
await this.createCardWithElement(parser, column, columnProps, false);
}

private async createColumn(
Expand All @@ -98,16 +105,23 @@ export class CommonCartridgeImportService {
): Promise<void> {
const column = await this.columnService.create(columnBoard, this.mapper.mapOrganizationToColumn(columnProps));
const cards = organizations.filter(
(organization) =>
organization.pathDepth >= 2 && organization.path.startsWith(columnProps.path) && organization.isResource
(organization) => organization.pathDepth === 2 && organization.path.startsWith(columnProps.path)
);

for await (const card of cards) {
await this.createCard(parser, column, card, true);
const cardsWithResource = cards.filter((card) => card.isResource);

for await (const card of cardsWithResource) {
await this.createCardWithElement(parser, column, card, true);
}

const cardsWithoutResource = cards.filter((card) => !card.isResource);

for await (const card of cardsWithoutResource) {
await this.createCard(parser, column, card, organizations);
}
}

private async createCard(
private async createCardWithElement(
parser: CommonCartridgeFileParser,
column: Column,
cardProps: CommonCartridgeOrganizationProps,
Expand All @@ -128,4 +142,44 @@ export class CommonCartridgeImportService {
await this.contentElementService.update(contentElement, contentElementBody);
}
}

private async createCard(
parser: CommonCartridgeFileParser,
column: Column,
cardProps: CommonCartridgeOrganizationProps,
organizations: CommonCartridgeOrganizationProps[]
) {
const card = await this.cardService.create(column, undefined, this.mapper.mapOrganizationToCard(cardProps, true));

const cardElements = organizations.filter(
(organization) => organization.pathDepth >= 3 && organization.path.startsWith(cardProps.path)
);

for await (const cardElement of cardElements) {
await this.createCardElement(parser, card, cardElement);
}
}

private async createCardElement(
parser: CommonCartridgeFileParser,
card: Card,
cardElementProps: CommonCartridgeOrganizationProps
) {
if (cardElementProps.isResource) {
const resource = parser.getResource(cardElementProps);
const contentElementType = this.mapper.mapResourceTypeToContentElementType(resource?.type);

if (resource && contentElementType) {
const contentElement = await this.contentElementService.create(card, contentElementType);
const contentElementBody = this.mapper.mapResourceToContentElementBody(resource);

await this.contentElementService.update(contentElement, contentElementBody);
}
} else {
const contentElement = await this.contentElementService.create(card, ContentElementType.RICH_TEXT);
const contentElementBody = this.mapper.mapOrganizationToTextElement(cardElementProps);

await this.contentElementService.update(contentElement, contentElementBody);
}
}
}

0 comments on commit 5b65f03

Please sign in to comment.