Skip to content

Commit

Permalink
Add cleanups and change comments
Browse files Browse the repository at this point in the history
  • Loading branch information
CeEv committed Nov 22, 2023
1 parent 74e4c05 commit cc275eb
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 41 deletions.
6 changes: 1 addition & 5 deletions apps/server/src/apps/h5p-library-management.app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,7 @@ import { install as sourceMapInstall } from 'source-map-support';

// application imports
import { LegacyLogger } from '@src/core/logger';
import {
H5PLibraryManagementModule,
H5PLibraryManagementService,
} from '@modules/h5p-library-management/h5p-library-management.module';
import { H5PLibraryManagementModule, H5PLibraryManagementService } from '@modules/h5p-library-management';

async function bootstrap() {
sourceMapInstall();
Expand All @@ -28,7 +25,6 @@ async function bootstrap() {

// eslint-disable-next-line @typescript-eslint/no-unsafe-member-access
await nestApp.get(H5PLibraryManagementService).run();
// await app.get(H5PLibraryManagementService).run();
// TODO: properly close app (there is some issue with the logger)
console.log('#########################################');
console.log(`##### Close H5P Library Management ######`);
Expand Down
4 changes: 3 additions & 1 deletion apps/server/src/modules/h5p-editor/index.ts
Original file line number Diff line number Diff line change
@@ -1 +1,3 @@
export * from './h5p-editor.module';
export { H5PEditorModule } from './h5p-editor.module';
export { ContentStorage, LibraryStorage } from './service';
export { s3ConfigContent, s3ConfigLibraries } from './h5p-editor.config';
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,14 @@ import { S3ClientModule } from '@infra/s3-client';
import { DB_PASSWORD, DB_URL, DB_USERNAME, createConfigModuleOptions } from '@src/config';
import { CoreModule } from '@src/core';
import { Logger } from '@src/core/logger';
import { UserModule } from '../user';
import { s3ConfigContent, s3ConfigLibraries } from '../h5p-editor/h5p-editor.config';
import { H5PContentRepo, LibraryRepo } from '../h5p-editor/repo';
import { ContentStorage, LibraryStorage } from '../h5p-editor/service';
import { H5PContent, InstalledLibrary } from '../h5p-editor/entity';
import { H5PLibraryManagementService } from './service/h5p-library-management.service';
import { h5PLibraryManagementConfig } from './service/h5p-library-management.config';
import { H5PEditorModule, s3ConfigContent, s3ConfigLibraries } from '@modules/h5p-editor';
// This export is always invalid. It is not allow to export repos or entities.
// In context of no domain object exists maybe it is ok. But i think it is not needed.
// All this database / mikroORM Stuff is done in H5PEditorModule.
// If yes remove exports from modules/h5p-editor/index.ts
// import { H5PEditorModule, s3ConfigContent, s3ConfigLibraries } from '@modules/h5p-editor';
import { H5PContent, InstalledLibrary } from '@modules/h5p-editor/entity';
import { H5PLibraryManagementService, h5PLibraryManagementConfig } from './service';

const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
findOneOrFailHandler: (entityName: string, where: Dictionary | IPrimaryKey) =>
Expand All @@ -25,7 +26,7 @@ const defaultMikroOrmOptions: MikroOrmModuleSyncOptions = {
const imports = [
ConfigModule.forRoot(createConfigModuleOptions(h5PLibraryManagementConfig)),
CoreModule,
UserModule,
H5PEditorModule,
RabbitMQWrapperModule,
MikroOrmModule.forRoot({
...defaultMikroOrmOptions,
Expand All @@ -34,21 +35,20 @@ const imports = [
clientUrl: DB_URL,
password: DB_PASSWORD,
user: DB_USERNAME,
allowGlobalContext: true,
// allowGlobalContext: true, Please not
entities: [...ALL_ENTITIES, H5PContent, InstalledLibrary],
}),
S3ClientModule.register([s3ConfigContent, s3ConfigLibraries]),
];

const controllers = [];

const providers = [Logger, H5PLibraryManagementService, ContentStorage, H5PContentRepo, LibraryRepo, LibraryStorage];
const providers = [Logger, H5PLibraryManagementService];

@Module({
imports,
controllers,
providers,
exports: [H5PLibraryManagementService],
exports: [],
})
export class H5PLibraryManagementModule {}
export { H5PLibraryManagementService };
2 changes: 2 additions & 0 deletions apps/server/src/modules/h5p-library-management/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { H5PLibraryManagementModule } from './h5p-library-management.module';
export { H5PLibraryManagementService } from './service';
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,12 @@ import {
} from '@lumieducation/h5p-server';
import ContentManager from '@lumieducation/h5p-server/build/src/ContentManager';
import ContentTypeInformationRepository from '@lumieducation/h5p-server/build/src/ContentTypeInformationRepository';
import { Injectable, NotFoundException } from '@nestjs/common';
import { ContentStorage, LibraryStorage } from '@src/modules/h5p-editor/service';
import { Injectable, InternalServerErrorException, NotFoundException } from '@nestjs/common';
import { ContentStorage, LibraryStorage } from '@src/modules/h5p-editor';
import { readFileSync } from 'fs';
import { parse } from 'yaml';
import { ConfigService } from '@nestjs/config';
import { IHubContentType } from '@lumieducation/h5p-server/build/src/types';
import { IH5PLibraryManagementConfig } from './h5p-library-management.config';

const h5pConfig = new H5PConfig(undefined, {
Expand All @@ -26,16 +27,28 @@ interface LibrariesContentType {
h5p_libraries: string[];
}

function isLibrariesContentType(object: any): object is LibrariesContentType {
return 'h5p_libraries' in object;
}
function isLibrariesContentType(object: unknown): object is LibrariesContentType {
const isType =
typeof object === 'object' &&
!Array.isArray(object) &&
object !== null &&
'h5p_libraries' in object &&
Array.isArray(object.h5p_libraries);

function isUserType(object: any): object is IUser {
return true;
return isType;
}

const castToLibrariesContentType = (object: unknown): LibrariesContentType => {
if (!isLibrariesContentType(object)) {
throw new InternalServerErrorException('Invalid input type for castToLibrariesContentType');
}

return object;
};

@Injectable()
export class H5PLibraryManagementService {
// should all this prop private?
contentTypeCache: ContentTypeCache;

contentTypeRepo: ContentTypeInformationRepository;
Expand Down Expand Up @@ -66,11 +79,10 @@ export class H5PLibraryManagementService {
const contentManager = new ContentManager(this.contentStorage);
this.libraryAdministration = new LibraryAdministration(this.libraryManager, contentManager);
const filePath = this.configService.get<string>('H5P_EDITOR__LIBRARY_LIST_PATH');

const librariesYamlContent = readFileSync(filePath, { encoding: 'utf-8' });
this.libraryWishList = [];
if (isLibrariesContentType(parse(librariesYamlContent))) {
this.libraryWishList = (parse(librariesYamlContent) as LibrariesContentType).h5p_libraries;
}
const librariesContentType = castToLibrariesContentType(parse(librariesYamlContent));
this.libraryWishList = librariesContentType.h5p_libraries;
}

public async uninstallUnwantedLibraries(
Expand All @@ -95,17 +107,14 @@ export class H5PLibraryManagementService {
);
}

public async installLibraries(librariesToInstall: string[]): Promise<void> {
if (librariesToInstall.length === 0) {
return;
}
const lastPositionLibrariesToInstallArray = librariesToInstall.length - 1;
// avoid conflicts, install one-by-one:
const contentType = await this.contentTypeCache.get(librariesToInstall[lastPositionLibrariesToInstallArray]);
private checkContentTypeExists(contentType: IHubContentType[]): void {
if (contentType === undefined) {
throw new NotFoundException('this library does not exist');
}
const userObj = {
}

private createDefaultIUser(): IUser {
const user: IUser = {
canCreateRestricted: true,
canInstallRecommended: true,
canUpdateAndInstallLibraries: true,
Expand All @@ -114,11 +123,23 @@ export class H5PLibraryManagementService {
name: 'a',
type: 'local',
};
if (isUserType(userObj)) {
const user: IUser = userObj;
await this.contentTypeRepo.installContentType(librariesToInstall[lastPositionLibrariesToInstallArray], user);
await this.installLibraries(librariesToInstall.slice(0, lastPositionLibrariesToInstallArray));

return user;
}

public async installLibraries(librariesToInstall: string[]): Promise<void> {
if (librariesToInstall.length === 0) {
return;
}
const lastPositionLibrariesToInstallArray = librariesToInstall.length - 1;
// avoid conflicts, install one-by-one:
const contentType = await this.contentTypeCache.get(librariesToInstall[lastPositionLibrariesToInstallArray]);
this.checkContentTypeExists(contentType);

const user = this.createDefaultIUser();

await this.contentTypeRepo.installContentType(librariesToInstall[lastPositionLibrariesToInstallArray], user);
await this.installLibraries(librariesToInstall.slice(0, lastPositionLibrariesToInstallArray));
}

public async run(): Promise<void> {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export * from './h5p-library-management.service';
export * from './h5p-library-management.config';

0 comments on commit cc275eb

Please sign in to comment.