Skip to content

Commit

Permalink
EW-1011 Add migration to TspSyncStrategy (#5280)
Browse files Browse the repository at this point in the history
Co-authored-by: Maximilian Kreuzkam <[email protected]>
  • Loading branch information
2 people authored and hoeppner-dataport committed Nov 18, 2024
1 parent 90b7ca3 commit bf5fec9
Show file tree
Hide file tree
Showing 34 changed files with 890 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ data:
TSP_SYNC_SCHOOL_LIMIT: "{{ TSP_SYNC_SCHOOL_LIMIT }}"
TSP_SYNC_DATA_LIMIT: "{{ TSP_SYNC_DATA_LIMIT }}"
TSP_SYNC_SCHOOL_DAYS_TO_FETCH: "{{ TSP_SYNC_SCHOOL_DAYS_TO_FETCH }}"
TSP_SYNC_DATA_DAYS_TO_FETCH: "{{ TSP_SYNC_DATA_DAYS_TO_FETCH }}"
TSP_SYNC_DATA_DAYS_TO_FETCH: "{{ TSP_SYNC_DATA_DAYS_TO_FETCH }}"
FEATURE_TSP_MIGRATION_ENABLED: "{{ FEATURE_TSP_MIGRATION_ENABLED }}"
TSP_SYNC_MIGRATION_LIMIT: "{{ TSP_SYNC_MIGRATION_LIMIT }}"
Original file line number Diff line number Diff line change
Expand Up @@ -22,4 +22,6 @@ data:
TSP_SYNC_SCHOOL_LIMIT: "{{ TSP_SYNC_SCHOOL_LIMIT }}"
TSP_SYNC_DATA_LIMIT: "{{ TSP_SYNC_DATA_LIMIT }}"
TSP_SYNC_SCHOOL_DAYS_TO_FETCH: "{{ ((ansible_date_time.date | to_datetime('%Y-%m-%d')) - ("1970-01-01" | to_datetime('%Y-%m-%d'))).days }}"
TSP_SYNC_DATA_DAYS_TO_FETCH: "{{ ((ansible_date_time.date | to_datetime('%Y-%m-%d')) - ("1970-01-01" | to_datetime('%Y-%m-%d'))).days }}"
TSP_SYNC_DATA_DAYS_TO_FETCH: "{{ ((ansible_date_time.date | to_datetime('%Y-%m-%d')) - ("1970-01-01" | to_datetime('%Y-%m-%d'))).days }}"
FEATURE_TSP_MIGRATION_ENABLED: "{{ FEATURE_TSP_MIGRATION_ENABLED }}"
TSP_SYNC_MIGRATION_LIMIT: "{{ TSP_SYNC_MIGRATION_LIMIT }}"
13 changes: 12 additions & 1 deletion apps/server/src/infra/sync/sync.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,11 @@ import { Configuration } from '@hpi-schul-cloud/commons/lib';
import { ConsoleWriterModule } from '@infra/console';
import { RabbitMQWrapperModule } from '@infra/rabbitmq';
import { TspClientModule } from '@infra/tsp-client/tsp-client.module';
import { AccountModule } from '@modules/account';
import { LegacySchoolModule } from '@modules/legacy-school';
import { SchoolModule } from '@modules/school';
import { SystemModule } from '@modules/system';
import { UserModule } from '@modules/user';
import { Module } from '@nestjs/common';
import { LoggerModule } from '@src/core/logger';
import { ProvisioningModule } from '@src/modules/provisioning';
Expand All @@ -20,7 +22,16 @@ import { SyncUc } from './uc/sync.uc';
LoggerModule,
ConsoleWriterModule,
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean)
? [TspClientModule, SystemModule, SchoolModule, LegacySchoolModule, RabbitMQWrapperModule, ProvisioningModule]
? [
TspClientModule,
SystemModule,
SchoolModule,
LegacySchoolModule,
RabbitMQWrapperModule,
ProvisioningModule,
UserModule,
AccountModule,
]
: []),
],
providers: [
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class TspDataFetchedLoggable implements Loggable {
private readonly daysFetched: number
) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Fetched ${this.tspTeacherCount} teachers, ${this.tspStudentCount} students and ${this.tspClassesCount} classes for the last ${this.daysFetched} days from TSP`,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Loggable, LogMessage } from '@src/core/logger';
export class TspMissingExternalIdLoggable implements Loggable {
constructor(private readonly objectType: string) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `A ${this.objectType} is missing an id. It is skipped.`,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Loggable, LogMessage } from '@src/core/logger';
export class TspSchoolsFetchedLoggable implements Loggable {
constructor(private readonly tspSchoolCount: number, private readonly daysFetched: number) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Fetched ${this.tspSchoolCount} schools for the last ${this.daysFetched} days from TSP`,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ export class TspSchoolsSyncedLoggable implements Loggable {
private readonly updatedSchools: number
) {}

getLogMessage(): LogMessage {
public 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: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Loggable, LogMessage } from '@src/core/logger';
export class TspSchulnummerMissingLoggable implements Loggable {
constructor(private readonly schulName?: string) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `The TSP school '${this.schulName ?? ''}' is missing a Schulnummer. This school is skipped.`,
data: {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TspStudentsFetchedLoggable } from './tsp-students-fetched.loggable';

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

beforeAll(() => {
loggable = new TspStudentsFetchedLoggable(10);
});

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 students for migration from TSP`,
data: {
tspStudentCount: 10,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspStudentsFetchedLoggable implements Loggable {
constructor(private readonly tspStudentCount: number) {}

public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Fetched ${this.tspStudentCount} students for migration from TSP`,
data: {
tspStudentCount: this.tspStudentCount,
},
};

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

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

beforeAll(() => {
loggable = new TspStudentsMigratedLoggable(10);
});

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: `Migrated students: 10 students migrated`,
data: {
migratedStudents: 10,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspStudentsMigratedLoggable implements Loggable {
constructor(private readonly migratedStudents: number) {}

public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Migrated students: ${this.migratedStudents} students migrated`,
data: {
migratedStudents: this.migratedStudents,
},
};

return message;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Loggable, LogMessage } from '@src/core/logger';
export class TspSyncedUsersLoggable implements Loggable {
constructor(private readonly syncedUsers: number) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Synced ${this.syncedUsers} users from TSP.`,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ import { Loggable, LogMessage } from '@src/core/logger';
export class TspSyncingUsersLoggable implements Loggable {
constructor(private readonly syncingUsers: number) {}

getLogMessage(): LogMessage {
public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Syncing ${this.syncingUsers} users from TSP.`,
data: {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ export class TspSystemNotFoundLoggableException extends BusinessError implements
);
}

getLogMessage(): LogMessage | ErrorLogMessage {
public getLogMessage(): LogMessage | ErrorLogMessage {
const message: LogMessage | ErrorLogMessage = {
message: this.message,
type: this.type,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
import { TspTeachersFetchedLoggable } from './tsp-teachers-fetched.loggable';

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

beforeAll(() => {
loggable = new TspTeachersFetchedLoggable(10);
});

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 teachers for migration from TSP`,
data: {
tspTeacherCount: 10,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspTeachersFetchedLoggable implements Loggable {
constructor(private readonly tspTeacherCount: number) {}

public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Fetched ${this.tspTeacherCount} teachers for migration from TSP`,
data: {
tspTeacherCount: this.tspTeacherCount,
},
};

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

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

beforeAll(() => {
loggable = new TspTeachersMigratedLoggable(10);
});

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: `Migrated teachers: 10 teachers migrated`,
data: {
migratedTeachers: 10,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspTeachersMigratedLoggable implements Loggable {
constructor(private readonly migratedTeachers: number) {}

public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Migrated teachers: ${this.migratedTeachers} teachers migrated`,
data: {
migratedTeachers: this.migratedTeachers,
},
};

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

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

beforeAll(() => {
loggable = new TspUsersMigratedLoggable(10);
});

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: `Migrated users: 10 users migrated`,
data: {
migratedUsers: 10,
},
});
});
});
});
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
import { Loggable, LogMessage } from '@src/core/logger';

export class TspUsersMigratedLoggable implements Loggable {
constructor(private readonly migratedUsers: number) {}

public getLogMessage(): LogMessage {
const message: LogMessage = {
message: `Migrated users: ${this.migratedUsers} users migrated`,
data: {
migratedUsers: this.migratedUsers,
},
};

return message;
}
}
4 changes: 2 additions & 2 deletions apps/server/src/infra/sync/tsp/tsp-oauth-data.mapper.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,7 @@ describe(TspOauthDataMapper.name, () => {
system: provisioningSystemDto,
externalUser: new ExternalUserDto({
externalId: tspTeachers[0].lehrerUid ?? '',
firstName: tspTeachers[0].lehrerNachname,
firstName: tspTeachers[0].lehrerVorname,
lastName: tspTeachers[0].lehrerNachname,
roles: [RoleName.TEACHER],
}),
Expand All @@ -123,7 +123,7 @@ describe(TspOauthDataMapper.name, () => {
system: provisioningSystemDto,
externalUser: new ExternalUserDto({
externalId: tspStudents[0].schuelerUid ?? '',
firstName: tspStudents[0].schuelerNachname,
firstName: tspStudents[0].schuelerVorname,
lastName: tspStudents[0].schuelerNachname,
roles: [RoleName.STUDENT],
}),
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/infra/sync/tsp/tsp-oauth-data.mapper.ts
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ export class TspOauthDataMapper {

const externalUser = new ExternalUserDto({
externalId: tspTeacher.lehrerUid,
firstName: tspTeacher.lehrerNachname,
firstName: tspTeacher.lehrerVorname,
lastName: tspTeacher.lehrerNachname,
roles: [RoleName.TEACHER],
});
Expand Down Expand Up @@ -109,7 +109,7 @@ export class TspOauthDataMapper {

const externalUser = new ExternalUserDto({
externalId: tspStudent.schuelerUid,
firstName: tspStudent.schuelerNachname,
firstName: tspStudent.schuelerVorname,
lastName: tspStudent.schuelerNachname,
roles: [RoleName.STUDENT],
});
Expand Down
2 changes: 2 additions & 0 deletions apps/server/src/infra/sync/tsp/tsp-sync.config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,6 @@ export interface TspSyncConfig {
TSP_SYNC_SCHOOL_DAYS_TO_FETCH: number;
TSP_SYNC_DATA_LIMIT: number;
TSP_SYNC_DATA_DAYS_TO_FETCH: number;
TSP_SYNC_MIGRATION_LIMIT: number;
FEATURE_TSP_MIGRATION_ENABLED: boolean;
}
Loading

0 comments on commit bf5fec9

Please sign in to comment.