diff --git a/apps/server/src/infra/vidis-client/index.ts b/apps/server/src/infra/vidis-client/index.ts new file mode 100644 index 0000000000..aa5b7d82e3 --- /dev/null +++ b/apps/server/src/infra/vidis-client/index.ts @@ -0,0 +1,5 @@ +export * from './generated/api'; +export * from './generated/models'; +export * from './vidis-client-config'; +export * from './vidis-client-factory'; +export * from './vidis-client.module'; diff --git a/apps/server/src/infra/vidis-client/vidis-client-config.ts b/apps/server/src/infra/vidis-client/vidis-client-config.ts new file mode 100644 index 0000000000..9b6970d637 --- /dev/null +++ b/apps/server/src/infra/vidis-client/vidis-client-config.ts @@ -0,0 +1,3 @@ +export interface VidisClientConfig { + VIDIS_API_CLIENT_BASE_URL: string; +} diff --git a/apps/server/src/infra/vidis-client/vidis-client-factory.integration.spec.ts b/apps/server/src/infra/vidis-client/vidis-client-factory.integration.spec.ts new file mode 100644 index 0000000000..ddf7e960e0 --- /dev/null +++ b/apps/server/src/infra/vidis-client/vidis-client-factory.integration.spec.ts @@ -0,0 +1,42 @@ +import { createMock } from '@golevelup/ts-jest'; +import { ServerTestModule } from '@modules/server'; +import { ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { VidisClientFactory } from './vidis-client-factory'; +import { VidisClientModule } from './vidis-client.module'; + +describe.skip('VidisClientFactory Integration', () => { + let module: TestingModule; + let sut: VidisClientFactory; + + beforeAll(async () => { + module = await Test.createTestingModule({ + imports: [ServerTestModule, VidisClientModule], + }) + .overrideProvider(ConfigService) + .useValue( + createMock({ + getOrThrow: (key: string) => { + switch (key) { + case 'VIDIS_API_CLIENT_BASE_URL': + return 'https://test2.schulportal-thueringen.de/tip-ms/api'; + + default: + throw new Error(`Unknown key: ${key}`); + } + }, + }) + ) + .compile(); + + sut = module.get(VidisClientFactory); + }); + + afterAll(async () => { + await module.close(); + }); + + it('should be defined', () => { + expect(sut).toBeDefined(); + }); +}); diff --git a/apps/server/src/infra/vidis-client/vidis-client-factory.spec.ts b/apps/server/src/infra/vidis-client/vidis-client-factory.spec.ts new file mode 100644 index 0000000000..2524c063d4 --- /dev/null +++ b/apps/server/src/infra/vidis-client/vidis-client-factory.spec.ts @@ -0,0 +1,64 @@ +import { faker } from '@faker-js/faker'; +import { DeepMocked, createMock } from '@golevelup/ts-jest'; +import { ServerConfig } from '@modules/server'; +import { ConfigService } from '@nestjs/config'; +import { Test, TestingModule } from '@nestjs/testing'; +import { VidisClientFactory } from './vidis-client-factory'; + +describe('TspClientFactory', () => { + let module: TestingModule; + let sut: VidisClientFactory; + let configServiceMock: DeepMocked>; + + beforeAll(async () => { + module = await Test.createTestingModule({ + providers: [ + VidisClientFactory, + { + provide: ConfigService, + useValue: createMock>({ + getOrThrow: (key: string) => { + switch (key) { + case 'VIDIS_API_CLIENT_BASE_URL': + return faker.internet.url(); + default: + throw new Error(`Unknown key: ${key}`); + } + }, + }), + }, + ], + }).compile(); + + sut = module.get(VidisClientFactory); + configServiceMock = module.get(ConfigService); + }); + + afterAll(async () => { + await module.close(); + }); + + beforeEach(() => { + jest.resetAllMocks(); + jest.restoreAllMocks(); + jest.clearAllMocks(); + }); + + it('should be defined', () => { + expect(sut).toBeDefined(); + }); + + describe('createExportClient', () => { + describe('when createExportClient is called', () => { + it('should return ExportApiInterface', () => { + const result = sut.createExportClient({ + password: faker.string.alpha(), + username: faker.string.alpha(), + }); + + expect(result).toBeDefined(); + expect(configServiceMock.getOrThrow).toHaveBeenCalledTimes(0); + }); + }); + }); +}); diff --git a/apps/server/src/infra/vidis-client/vidis-client-factory.ts b/apps/server/src/infra/vidis-client/vidis-client-factory.ts new file mode 100644 index 0000000000..d3dd07db29 --- /dev/null +++ b/apps/server/src/infra/vidis-client/vidis-client-factory.ts @@ -0,0 +1,30 @@ +import { Injectable } from '@nestjs/common'; +import { ConfigService } from '@nestjs/config'; +import { Configuration, IDMBetreiberApiFactory, IDMBetreiberApiInterface } from './generated'; +import { VidisClientConfig } from './vidis-client-config'; + +type FactoryParams = { + username: string; + password: string; +}; + +@Injectable() +export class VidisClientFactory { + private readonly baseUrl: string; + + constructor(private readonly configService: ConfigService) { + this.baseUrl = configService.getOrThrow('VIDIS_API_CLIENT_BASE_URL'); + } + + public createExportClient(params: FactoryParams): IDMBetreiberApiInterface { + const factory = IDMBetreiberApiFactory( + new Configuration({ + username: params.username, + password: params.password, + basePath: this.baseUrl, + }) + ); + + return factory; + } +} diff --git a/apps/server/src/infra/vidis-client/vidis-client.module.ts b/apps/server/src/infra/vidis-client/vidis-client.module.ts new file mode 100644 index 0000000000..2b65500636 --- /dev/null +++ b/apps/server/src/infra/vidis-client/vidis-client.module.ts @@ -0,0 +1,9 @@ +import { Module } from '@nestjs/common'; +import { VidisClientFactory } from './vidis-client-factory'; + +@Module({ + imports: [], + providers: [VidisClientFactory], + exports: [VidisClientFactory], +}) +export class VidisClientModule {}