-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathcommon-cartridge-import.service.ts
198 lines (171 loc) · 7.3 KB
/
common-cartridge-import.service.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
import {
BoardExternalReferenceType,
BoardLayout,
BoardNodeFactory,
BoardNodeService,
Card,
Column,
ColumnBoard,
ContentElementType,
} from '@modules/board';
import {
CommonCartridgeFileParser,
CommonCartridgeImportOrganizationProps,
DEFAULT_FILE_PARSER_OPTIONS,
} from '@modules/common-cartridge';
import { Injectable } from '@nestjs/common';
import { Course, User } from '@shared/domain/entity';
import { CommonCartridgeImportMapper } from '../mapper/common-cartridge-import.mapper';
import { CourseService } from './course.service';
@Injectable()
export class CommonCartridgeImportService {
constructor(
private readonly courseService: CourseService,
private readonly boardNodeFactory: BoardNodeFactory,
private readonly boardNodeService: BoardNodeService,
private readonly mapper: CommonCartridgeImportMapper
) {}
public async importFile(user: User, file: Buffer): Promise<void> {
const parser = new CommonCartridgeFileParser(file, DEFAULT_FILE_PARSER_OPTIONS);
const course = new Course({ teachers: [user], school: user.school, name: parser.getTitle() });
await this.courseService.create(course);
await this.createColumnBoards(parser, course);
}
private async createColumnBoards(parser: CommonCartridgeFileParser, course: Course): Promise<void> {
const organizations = parser.getOrganizations();
const boards = organizations.filter((organization) => organization.pathDepth === 0);
for await (const board of boards) {
await this.createColumnBoard(parser, course, board, organizations);
}
}
private async createColumnBoard(
parser: CommonCartridgeFileParser,
course: Course,
boardProps: CommonCartridgeImportOrganizationProps,
organizations: CommonCartridgeImportOrganizationProps[]
): Promise<void> {
const columnBoard = this.boardNodeFactory.buildColumnBoard({
context: {
type: BoardExternalReferenceType.Course,
id: course.id,
},
layout: BoardLayout.COLUMNS,
title: boardProps.title === '' ? 'Importiertes Spaltenboard' : boardProps.title,
});
await this.boardNodeService.addRoot(columnBoard);
await this.createColumns(parser, columnBoard, boardProps, organizations);
}
private async createColumns(
parser: CommonCartridgeFileParser,
columnBoard: ColumnBoard,
boardProps: CommonCartridgeImportOrganizationProps,
organizations: CommonCartridgeImportOrganizationProps[]
): Promise<void> {
const columnsWithResource = organizations.filter(
(organization) =>
organization.pathDepth === 1 && organization.path.startsWith(boardProps.identifier) && organization.isResource
);
for await (const columnWithResource of columnsWithResource) {
await this.createColumnWithResource(parser, columnBoard, columnWithResource);
}
const columnsWithoutResource = organizations.filter(
(organization) =>
organization.pathDepth === 1 && organization.path.startsWith(boardProps.identifier) && !organization.isResource
);
for await (const columnWithoutResource of columnsWithoutResource) {
await this.createColumn(parser, columnBoard, columnWithoutResource, organizations);
}
}
private async createColumnWithResource(
parser: CommonCartridgeFileParser,
columnBoard: ColumnBoard,
columnProps: CommonCartridgeImportOrganizationProps
): Promise<void> {
const column = this.boardNodeFactory.buildColumn();
const { title } = this.mapper.mapOrganizationToColumn(columnProps);
column.title = title;
await this.boardNodeService.addToParent(columnBoard, column);
await this.createCardWithElement(parser, column, columnProps, false);
}
private async createColumn(
parser: CommonCartridgeFileParser,
columnBoard: ColumnBoard,
columnProps: CommonCartridgeImportOrganizationProps,
organizations: CommonCartridgeImportOrganizationProps[]
): Promise<void> {
const column = this.boardNodeFactory.buildColumn();
const { title } = this.mapper.mapOrganizationToColumn(columnProps);
column.title = title;
await this.boardNodeService.addToParent(columnBoard, column);
const cards = organizations.filter(
(organization) => organization.pathDepth === 2 && organization.path.startsWith(columnProps.path)
);
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 createCardWithElement(
parser: CommonCartridgeFileParser,
column: Column,
cardProps: CommonCartridgeImportOrganizationProps,
withTitle = true
): Promise<void> {
const card = this.boardNodeFactory.buildCard();
const { title, height } = this.mapper.mapOrganizationToCard(cardProps, withTitle);
card.title = title;
card.height = height;
await this.boardNodeService.addToParent(column, card);
const resource = parser.getResource(cardProps);
const contentElementType = this.mapper.mapResourceTypeToContentElementType(resource?.type);
if (resource && contentElementType) {
const contentElement = this.boardNodeFactory.buildContentElement(contentElementType);
await this.boardNodeService.addToParent(card, contentElement);
const contentElementBody = this.mapper.mapResourceToContentElementBody(resource);
await this.boardNodeService.updateContent(contentElement, contentElementBody);
}
}
private async createCard(
parser: CommonCartridgeFileParser,
column: Column,
cardProps: CommonCartridgeImportOrganizationProps,
organizations: CommonCartridgeImportOrganizationProps[]
): Promise<void> {
const card = this.boardNodeFactory.buildCard();
const { title, height } = this.mapper.mapOrganizationToCard(cardProps, true);
card.title = title;
card.height = height;
await this.boardNodeService.addToParent(column, card);
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: CommonCartridgeImportOrganizationProps
): Promise<void> {
if (cardElementProps.isResource) {
const resource = parser.getResource(cardElementProps);
const contentElementType = this.mapper.mapResourceTypeToContentElementType(resource?.type);
if (resource && contentElementType) {
const contentElement = this.boardNodeFactory.buildContentElement(contentElementType);
await this.boardNodeService.addToParent(card, contentElement);
const contentElementBody = this.mapper.mapResourceToContentElementBody(resource);
await this.boardNodeService.updateContent(contentElement, contentElementBody);
}
} else {
const contentElement = this.boardNodeFactory.buildContentElement(ContentElementType.RICH_TEXT);
await this.boardNodeService.addToParent(card, contentElement);
const contentElementBody = this.mapper.mapOrganizationToTextElement(cardElementProps);
await this.boardNodeService.updateContent(contentElement, contentElementBody);
}
}
}