-
Notifications
You must be signed in to change notification settings - Fork 17
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
10bebc1
commit faef50e
Showing
3 changed files
with
70 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
64 changes: 64 additions & 0 deletions
64
apps/server/src/infra/sync/tsp/tsp-legacy-migration.service.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters