Skip to content

Commit

Permalink
EW-1006: Add school part to TSP sync. (#5279)
Browse files Browse the repository at this point in the history
* Add school part to sync.
---------

Co-authored-by: Alexander Weber <[email protected]>
  • Loading branch information
mkreuzkam-cap and alweber-cap authored Oct 14, 2024
1 parent 37b842a commit f11a3fa
Show file tree
Hide file tree
Showing 20 changed files with 875 additions and 18 deletions.
1 change: 1 addition & 0 deletions apps/server/src/infra/sync/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
export * from './tsp';
22 changes: 17 additions & 5 deletions apps/server/src/infra/sync/sync.module.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,31 @@
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { ConsoleWriterModule } from '@infra/console';
import { TspClientModule } from '@infra/tsp-client/tsp-client.module';
import { LegacySchoolModule } from '@modules/legacy-school';
import { SchoolModule } from '@modules/school';
import { SystemModule } from '@modules/system';
import { Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { ConsoleWriterModule } from '@infra/console';
import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { RabbitMQWrapperModule } from '@infra/rabbitmq';
import { SyncConsole } from './console/sync.console';
import { SyncUc } from './uc/sync.uc';
import { SyncService } from './service/sync.service';
import { TspSyncService } from './tsp/tsp-sync.service';
import { TspSyncStrategy } from './tsp/tsp-sync.strategy';
import { SyncUc } from './uc/sync.uc';

@Module({
imports: [LoggerModule, ConsoleWriterModule],
imports: [
LoggerModule,
ConsoleWriterModule,
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean)
? [TspClientModule, SystemModule, SchoolModule, LegacySchoolModule, RabbitMQWrapperModule]
: []),
],
providers: [
SyncConsole,
SyncUc,
SyncService,
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean) ? [TspSyncStrategy] : []),
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean) ? [TspSyncStrategy, TspSyncService] : []),
],
exports: [SyncConsole],
})
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/infra/sync/tsp/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
export { TspSyncConfig } from './tsp-sync.config';
export { TspSyncStrategy } from './tsp-sync.strategy';
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
import { TspSchoolsFetchedLoggable } from './tsp-schools-fetched.loggable';

describe(TspSchoolsFetchedLoggable.name, () => {
let loggable: TspSchoolsFetchedLoggable;

beforeAll(() => {
loggable = new TspSchoolsFetchedLoggable(10, 5);
});

describe('when loggable is initialized', () => {
it('should be defined', () => {
expect(loggable).toBeDefined();
});
});

describe('getLogMessage', () => {
it('should return a log message', () => {
expect(loggable.getLogMessage()).toEqual({
message: `Fetched 10 schools for the last 5 days from TSP`,
data: {
tspSchoolCount: 10,
daysFetched: 5,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspSchoolsFetchedLoggable implements Loggable {
constructor(private readonly tspSchoolCount: number, private readonly daysFetched: number) {}

getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Fetched ${this.tspSchoolCount} schools for the last ${this.daysFetched} days from TSP`,
data: {
tspSchoolCount: this.tspSchoolCount,
daysFetched: this.daysFetched,
},
};

return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
import { TspSchoolsSyncedLoggable } from './tsp-schools-synced.loggable';

describe(TspSchoolsSyncedLoggable.name, () => {
let loggable: TspSchoolsSyncedLoggable;

beforeAll(() => {
loggable = new TspSchoolsSyncedLoggable(10, 10, 5, 5);
});

describe('when loggable is initialized', () => {
it('should be defined', () => {
expect(loggable).toBeDefined();
});
});

describe('getLogMessage', () => {
it('should return a log message', () => {
expect(loggable.getLogMessage()).toEqual({
message: `Synced schools: Of 10 schools 10 were processed. 5 were created and 5 were updated`,
data: {
tspSchoolCount: 10,
processedSchools: 10,
createdSchools: 5,
updatedSchools: 5,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspSchoolsSyncedLoggable implements Loggable {
constructor(
private readonly tspSchoolCount: number,
private readonly processedSchools: number,
private readonly createdSchools: number,
private readonly updatedSchools: number
) {}

getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Synced schools: Of ${this.tspSchoolCount} schools ${this.processedSchools} were processed. ${this.createdSchools} were created and ${this.updatedSchools} were updated`,
data: {
tspSchoolCount: this.tspSchoolCount,
processedSchools: this.processedSchools,
createdSchools: this.createdSchools,
updatedSchools: this.updatedSchools,
},
};

return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TspSchulnummerMissingLoggable } from './tsp-schulnummer-missing.loggable';

describe(TspSchulnummerMissingLoggable.name, () => {
let loggable: TspSchulnummerMissingLoggable;

beforeAll(() => {
loggable = new TspSchulnummerMissingLoggable('Schule');
});

describe('when loggable is initialized', () => {
it('should be defined', () => {
expect(loggable).toBeDefined();
});
});

describe('getLogMessage', () => {
it('should return a log message', () => {
expect(loggable.getLogMessage()).toEqual({
message: `The TSP school 'Schule' is missing a Schulnummer. This school is skipped.`,
data: {
schulName: 'Schule',
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspSchulnummerMissingLoggable implements Loggable {
constructor(private readonly schulName?: string) {}

getLogMessage(): LogMessage {
const message: LogMessage = {
message: `The TSP school '${this.schulName ?? ''}' is missing a Schulnummer. This school is skipped.`,
data: {
schulName: this.schulName,
},
};

return message;
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
import { TspSystemNotFoundLoggableException } from './tsp-system-not-found.loggable-exception';

describe(TspSystemNotFoundLoggableException.name, () => {
let loggable: TspSystemNotFoundLoggableException;

beforeAll(() => {
loggable = new TspSystemNotFoundLoggableException();
});

describe('when loggable is initialized', () => {
it('should be defined', () => {
expect(loggable).toBeDefined();
});
});

describe('getLogMessage', () => {
it('should return a log message', () => {
expect(loggable.getLogMessage()).toEqual({
message: 'The TSP system could not be found during the sync',
type: 'TSP_SYSTEM_NOT_FOUND',
stack: expect.any(String),
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { HttpStatus } from '@nestjs/common';
import { BusinessError, ErrorLogMessage } from '@shared/common';
import { Loggable, LogMessage } from '@src/core/logger';

export class TspSystemNotFoundLoggableException extends BusinessError implements Loggable {
constructor() {
super(
{
type: 'TSP_SYSTEM_NOT_FOUND',
title: 'The TSP system could not be found',
defaultMessage: 'The TSP system could not be found during the sync',
},
HttpStatus.BAD_REQUEST
);
}

getLogMessage(): LogMessage | ErrorLogMessage {
const message: LogMessage | ErrorLogMessage = {
message: this.message,
type: this.type,
stack: this.stack,
};

return message;
}
}
4 changes: 4 additions & 0 deletions apps/server/src/infra/sync/tsp/tsp-sync.config.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
export interface TspSyncConfig {
TSP_SYNC_SCHOOL_LIMIT: number;
TSP_SYNC_SCHOOL_DAYS_TO_FETCH: number;
}
Loading

0 comments on commit f11a3fa

Please sign in to comment.