-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Refactor import process and testing framework
- Loading branch information
Showing
15 changed files
with
523 additions
and
229 deletions.
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
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
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
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
40 changes: 40 additions & 0 deletions
40
api/src/modules/import-data/sourcing-data/sourcing-data.db-cleaner.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,40 @@ | ||
import { GeoRegion } from 'modules/geo-regions/geo-region.entity'; | ||
import { Scenario } from 'modules/scenarios/scenario.entity'; | ||
import { DataSource, EntityManager } from 'typeorm'; | ||
import { BusinessUnit } from 'modules/business-units/business-unit.entity'; | ||
import { SourcingLocation } from 'modules/sourcing-locations/sourcing-location.entity'; | ||
import { Supplier } from 'modules/suppliers/supplier.entity'; | ||
|
||
export class SourcingDataDbCleaner { | ||
constructor(private readonly dataSource: DataSource) {} | ||
|
||
async cleanDataBeforeImport(): Promise<void> { | ||
await this.dataSource.transaction(async (manager: EntityManager) => { | ||
await this.deleteExistingUserData(manager); | ||
await this.deactivateAllIndicators(manager); | ||
await this.deactivateAllMaterials(manager); | ||
}); | ||
} | ||
|
||
private async deactivateAllIndicators(manager: EntityManager): Promise<void> { | ||
await manager.query('UPDATE indicators SET active = false'); | ||
} | ||
|
||
private async deactivateAllMaterials(manager: EntityManager): Promise<void> { | ||
await manager.query('UPDATE materials SET active = false'); | ||
} | ||
|
||
private async deleteExistingUserData(manager: EntityManager): Promise<void> { | ||
const entities: any = [Scenario, SourcingLocation, BusinessUnit, Supplier]; | ||
for (const entity of entities) { | ||
await manager.getRepository(entity).delete({}); | ||
} | ||
await this.deleteGeoRegionsCreatedByUser(manager); | ||
} | ||
|
||
private async deleteGeoRegionsCreatedByUser( | ||
manager: EntityManager, | ||
): Promise<void> { | ||
await manager.getRepository(GeoRegion).delete({ isCreatedByUser: true }); | ||
} | ||
} |
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
Oops, something went wrong.