Skip to content

Commit

Permalink
Add legacy migration.
Browse files Browse the repository at this point in the history
  • Loading branch information
mkreuzkam-cap committed Nov 11, 2024
1 parent 10bebc1 commit faef50e
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 1 deletion.
3 changes: 2 additions & 1 deletion apps/server/src/infra/sync/sync.module.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import { LoggerModule } from '@src/core/logger';
import { ProvisioningModule } from '@src/modules/provisioning';
import { SyncConsole } from './console/sync.console';
import { SyncService } from './service/sync.service';
import { TspLegacyMigrationService } from './tsp/tsp-legacy-migration.service';
import { TspOauthDataMapper } from './tsp/tsp-oauth-data.mapper';
import { TspSyncService } from './tsp/tsp-sync.service';
import { TspSyncStrategy } from './tsp/tsp-sync.strategy';
Expand All @@ -28,7 +29,7 @@ import { SyncUc } from './uc/sync.uc';
SyncUc,
SyncService,
...((Configuration.get('FEATURE_TSP_SYNC_ENABLED') as boolean)
? [TspSyncStrategy, TspSyncService, TspOauthDataMapper]
? [TspSyncStrategy, TspSyncService, TspOauthDataMapper, TspLegacyMigrationService]
: []),
],
exports: [SyncConsole],
Expand Down
64 changes: 64 additions & 0 deletions apps/server/src/infra/sync/tsp/tsp-legacy-migration.service.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
import { EntityManager, ObjectId } from '@mikro-orm/mongodb';
import { Injectable } from '@nestjs/common';
import { SchoolProperties } from '@shared/domain/entity';
import { EntityId } from '@shared/domain/types';
import { Logger } from '@src/core/logger';

@Injectable()
export class TspLegacyMigrationService {
constructor(private readonly em: EntityManager, private readonly logger: Logger) {
logger.setContext(TspLegacyMigrationService.name);
}

public async migrateLegacyData(newSystemId: EntityId): Promise<void> {
console.log('starting legacy migration');
const legacySystemId = await this.findLegacySystemId();

if (!legacySystemId) {
console.log('No legacy system found');
return;
}

const schools = await this.em.find<
SchoolProperties & {
sourceOptions: {
schoolIdentifier: number;
};
}
>('schools', {
systems: [legacySystemId],
source: 'tsp',
});

const schoolIds = schools.map((school) => school.sourceOptions.schoolIdentifier);

console.log('Number of schools', schoolIds);

const promises = schoolIds.map((oldId) =>
this.em.nativeUpdate(
'schools',
{
systems: [legacySystemId],
source: 'tsp',
},
{
$unset: { sourceOptions: '' },
ldapSchoolIdentifier: oldId,
systems: [new ObjectId(newSystemId)],
}
)
);

const res = await Promise.allSettled(promises);
const success = res.map((r) => r.status === 'fulfilled').length;
console.log(`Migrated ${schoolIds.length} legacy schools to new system. ${success} succeeded.`);
}

private async findLegacySystemId() {
const tspLegacySystem = await this.em.getCollection('systems').findOne({
type: 'tsp-school',
});

return tspLegacySystem?._id;
}
}
4 changes: 4 additions & 0 deletions apps/server/src/infra/sync/tsp/tsp-sync.strategy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ import { TspSyncingUsersLoggable } from './loggable/tsp-syncing-users.loggable';
import { TspOauthDataMapper } from './tsp-oauth-data.mapper';
import { TspSyncConfig } from './tsp-sync.config';
import { TspSyncService } from './tsp-sync.service';
import { TspLegacyMigrationService } from './tsp-legacy-migration.service';

@Injectable()
export class TspSyncStrategy extends SyncStrategy {
Expand All @@ -31,6 +32,7 @@ export class TspSyncStrategy extends SyncStrategy {
private readonly logger: Logger,
private readonly tspSyncService: TspSyncService,
private readonly tspOauthDataMapper: TspOauthDataMapper,
private readonly tspLegacyMigrationService: TspLegacyMigrationService,
configService: ConfigService<TspSyncConfig, true>,
private readonly provisioningService: ProvisioningService
) {
Expand All @@ -51,6 +53,8 @@ export class TspSyncStrategy extends SyncStrategy {
public async sync(): Promise<void> {
const system = await this.tspSyncService.findTspSystemOrFail();

await this.tspLegacyMigrationService.migrateLegacyData(system.id);

await this.syncSchools(system);

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

0 comments on commit faef50e

Please sign in to comment.