Skip to content

Commit

Permalink
Use configService directly and don't read values in constructor.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Dec 16, 2024
1 parent 0a38d25 commit a04d22d
Showing 1 changed file with 13 additions and 30 deletions.
43 changes: 13 additions & 30 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,40 +25,19 @@ import { TspSyncService } from './tsp-sync.service';

@Injectable()
export class TspSyncStrategy extends SyncStrategy {
private readonly schoolLimit: number;

private readonly dataLimit: number;

private readonly migrationLimit: number;

private readonly schoolDaysToFetch: number;

private readonly schoolDataDaysToFetch: number;

private readonly migrationEnabled: boolean;

constructor(
private readonly logger: Logger,
private readonly tspSyncService: TspSyncService,
private readonly tspFetchService: TspFetchService,
private readonly tspOauthDataMapper: TspOauthDataMapper,
private readonly tspLegacyMigrationService: TspLegacyMigrationService,
configService: ConfigService<TspSyncConfig, true>,
private readonly configService: ConfigService<TspSyncConfig, true>,
private readonly provisioningService: ProvisioningService,
private readonly userService: UserService,
private readonly accountService: AccountService
) {
super();
this.logger.setContext(TspSyncStrategy.name);

this.schoolLimit = configService.getOrThrow<number>('TSP_SYNC_SCHOOL_LIMIT');
this.schoolDaysToFetch = configService.get<number>('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1);

this.dataLimit = configService.getOrThrow<number>('TSP_SYNC_DATA_LIMIT');
this.schoolDataDaysToFetch = configService.get<number>('TSP_SYNC_DATA_DAYS_TO_FETCH', 1);

this.migrationLimit = configService.getOrThrow<number>('TSP_SYNC_MIGRATION_LIMIT');
this.migrationEnabled = configService.get<boolean>('FEATURE_TSP_MIGRATION_ENABLED', false);
}

public override getType(): SyncStrategyTarget {
Expand All @@ -74,16 +53,17 @@ export class TspSyncStrategy extends SyncStrategy {

const schools = await this.tspSyncService.findSchoolsForSystem(system);

if (this.migrationEnabled) {
if (this.configService.get<boolean>('FEATURE_TSP_MIGRATION_ENABLED', false)) {
await this.runMigration(system);
}

await this.syncData(system, schools);
}

private async syncSchools(system: System): Promise<School[]> {
const tspSchools = await this.tspFetchService.fetchTspSchools(system, this.schoolDaysToFetch);
this.logger.info(new TspSchoolsFetchedLoggable(tspSchools.length, this.schoolDaysToFetch));
const schoolDaysToFetch = this.configService.get<number>('TSP_SYNC_SCHOOL_LIMIT', 1);
const tspSchools = await this.tspFetchService.fetchTspSchools(system, schoolDaysToFetch);
this.logger.info(new TspSchoolsFetchedLoggable(tspSchools.length, schoolDaysToFetch));

const schoolPromises = tspSchools.map(async (tspSchool) => {
if (!tspSchool.schuleNummer) {
Expand Down Expand Up @@ -118,11 +98,12 @@ export class TspSyncStrategy extends SyncStrategy {
}

private async syncData(system: System, schools: School[]): Promise<void> {
const tspTeachers = await this.tspFetchService.fetchTspTeachers(system, this.schoolDataDaysToFetch);
const tspStudents = await this.tspFetchService.fetchTspStudents(system, this.schoolDataDaysToFetch);
const tspClasses = await this.tspFetchService.fetchTspClasses(system, this.schoolDataDaysToFetch);
const schoolDataDaysToFetch = this.configService.get<number>('TSP_SYNC_SCHOOL_DAYS_TO_FETCH', 1);
const tspTeachers = await this.tspFetchService.fetchTspTeachers(system, schoolDataDaysToFetch);
const tspStudents = await this.tspFetchService.fetchTspStudents(system, schoolDataDaysToFetch);
const tspClasses = await this.tspFetchService.fetchTspClasses(system, schoolDataDaysToFetch);
this.logger.info(
new TspDataFetchedLoggable(tspTeachers.length, tspStudents.length, tspClasses.length, this.schoolDataDaysToFetch)
new TspDataFetchedLoggable(tspTeachers.length, tspStudents.length, tspClasses.length, schoolDataDaysToFetch)
);

const oauthDataDtos = this.tspOauthDataMapper.mapTspDataToOauthData(
Expand All @@ -145,7 +126,9 @@ export class TspSyncStrategy extends SyncStrategy {
private async migrateTspTeachersBatch(system: System, oldToNewMappings: Map<string, string>): Promise<number> {
this.logger.info(new TspTeachersFetchedLoggable(oldToNewMappings.size));

const users = await this.userService.findByTspUids(Array.from(oldToNewMappings.keys()));
const migrationIds = Array.from(oldToNewMappings.keys());

const users = await this.userService.findByTspUids(migrationIds);
this.logger.info(this.logForMsg(`Users fetched: ${users.length}`));

const userIds = users.map((user) => user.id ?? '');
Expand Down

0 comments on commit a04d22d

Please sign in to comment.