Update also Transactions and Assets
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
import AddCompanyIDToChargingStationsTask from './tasks/AddCompanyIDToChargingStationsTask';
import AddCompanyIDToTransactionsTask from './tasks/AddCompanyIDToTransactionsTask';
import AddUserIDToCarsTask from './tasks/AddUserIDToCarsTask';
+import AlignEntitiesWithOrganizationIDsTask from './tasks/AlignEntitiesWithOrganizationIDsTask';
import Constants from '../utils/Constants';
-import FixChargingStationCompanyAndSiteIDsTask from './tasks/FixChargingStationCompanyAndSiteIDsTask';
import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
import Logging from '../utils/Logging';
@@ -149,7 +149,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new RepairTransactionPricedAtZero());
currentMigrationTasks.push(new UpdateEmailsToLowercaseTask());
currentMigrationTasks.push(new UserCleanUpTask());
- currentMigrationTasks.push(new FixChargingStationCompanyAndSiteIDsTask());
+ currentMigrationTasks.push(new AlignEntitiesWithOrganizationIDsTask());
return currentMigrationTasks;
}
}

ADD
src/migration/tasks/AlignEntitiesWithOrganizationIDsTask.ts
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantMigrationTask from '../TenantMigrationTask';
+import Utils from '../../utils/Utils';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'AlignEntitiesWithOrganizationIDsTask';
+
+export default class AlignEntitiesWithOrganizationIDsTask extends TenantMigrationTask {
+ public async migrateTenant(tenant: Tenant): Promise<void> {
+ let updated = 0;
+ // Get all the Sites
+ const sites = await global.database.getCollection<any>(tenant.id, 'sites').find({ issuer: true })
+ .project({ _id: 1, companyID: 1 }).toArray();
+ // Get all the Site Areas
+ const siteAreas = await global.database.getCollection<any>(tenant.id, 'siteareas').find({ issuer: true })
+ .project({ _id: 1, siteID: 1 }).toArray();
+ if (!Utils.isEmptyArray(siteAreas)) {
+ for (const siteArea of siteAreas) {
+ // Ignore
+ if (!siteArea.siteID) {
+ continue;
+ }
+ const foundSite = sites.find((site) => site._id.toString() === siteArea.siteID.toString());
+ if (foundSite) {
+ // Update Charging Stations
+ let result = await global.database.getCollection<any>(tenant.id, 'chargingstations').updateMany(
+ {
+ siteAreaID: siteArea._id
+ },
+ {
+ $set: {
+ siteID: siteArea.siteID,
+ companyID: foundSite.companyID
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ // Update Transactions
+ result = await global.database.getCollection<any>(tenant.id, 'transactions').updateMany(
+ {
+ siteAreaID: siteArea._id
+ },
+ {
+ $set: {
+ siteID: siteArea.siteID,
+ companyID: foundSite.companyID
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ // Update Assets
+ result = await global.database.getCollection<any>(tenant.id, 'assets').updateMany(
+ {
+ siteAreaID: siteArea._id
+ },
+ {
+ $set: {
+ siteID: siteArea.siteID,
+ companyID: foundSite.companyID
+ }
+ }
+ );
+ updated += result.modifiedCount;
+ } else {
+ await Logging.logError({
+ tenantID: Constants.DEFAULT_TENANT,
+ module: MODULE_NAME, method: 'migrateTenant',
+ action: ServerAction.MIGRATION,
+ message: `Site ID '${siteArea.siteID.toString()}' does not exist.`,
+ });
+ }
+ }
+ }
+ // Log in the default tenant
+ if (updated > 0) {
+ await Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ module: MODULE_NAME, method: 'migrateTenant',
+ action: ServerAction.MIGRATION,
+ message: `${updated} record(s) have been updated with Organization IDs in Tenant ${Utils.buildTenantName(tenant)}`
+ });
+ }
+ }
+
+ public getVersion(): string {
+ return '1.05';
+ }
+
+ public getName(): string {
+ return 'FixChargingStationCompanyAndSiteIDsTask';
+ }
+
+ public isAsynchronous(): boolean {
+ return true;
+ }
+}

DELETE
src/migration/tasks/FixChargingStationCompanyAndSiteIDsTask.ts
-import Constants from '../../utils/Constants';
-import Logging from '../../utils/Logging';
-import { ServerAction } from '../../types/Server';
-import Tenant from '../../types/Tenant';
-import TenantMigrationTask from '../TenantMigrationTask';
-import Utils from '../../utils/Utils';
-import global from '../../types/GlobalType';
-
-const MODULE_NAME = 'FixChargingStationCompanyAndSiteIDsTask';
-
-export default class FixChargingStationCompanyAndSiteIDsTask extends TenantMigrationTask {
- public async migrateTenant(tenant: Tenant): Promise<void> {
- let updated = 0, siteArea, site;
- // Get all the charging stations
- const chargingStations = await global.database.getCollection<any>(tenant.id, 'chargingstations').find({})
- .project({ siteAreaID: 1, _id: 1 })
- .toArray();
- if (!Utils.isEmptyArray(chargingStations)) {
- for (const chargingStation of chargingStations) {
- if (chargingStation.siteAreaID) {
- siteArea = await global.database.getCollection<any>(tenant.id, 'siteareas').findOne({ _id: chargingStation.siteAreaID });
- if (siteArea) {
- site = await global.database.getCollection<any>(tenant.id, 'sites').findOne({ _id: siteArea.siteID });
- if (site) {
- await global.database.getCollection<any>(tenant.id, 'chargingstations').updateOne(
- { _id: chargingStation._id },
- {
- $set: {
- siteID: siteArea.siteID,
- companyID: site.companyID
- }
- }
- );
- updated++;
- } else {
- await Logging.logError({
- tenantID: Constants.DEFAULT_TENANT,
- module: MODULE_NAME, method: 'migrateTenant',
- action: ServerAction.MIGRATION,
- message: `Site ID '${siteArea.siteID}' does not exist.`,
- });
- }
- } else {
- await Logging.logError({
- tenantID: Constants.DEFAULT_TENANT,
- module: MODULE_NAME, method: 'migrateTenant',
- action: ServerAction.MIGRATION,
- message: `Site Area ID '${chargingStation.siteAreaID}' does not exist.`,
- });
- }
- } else {
- await Logging.logError({
- tenantID: Constants.DEFAULT_TENANT,
- module: MODULE_NAME, method: 'migrateTenant',
- action: ServerAction.MIGRATION,
- message: `Charging station ID '${chargingStation._id}' is not assigned to a Site Area.`,
- });
- }
- }
- }
- // Log in the default tenant
- if (updated > 0) {
- await Logging.logDebug({
- tenantID: Constants.DEFAULT_TENANT,
- module: MODULE_NAME, method: 'migrateTenant',
- action: ServerAction.MIGRATION,
- message: `${updated} ChargingStation(s) have been updated with company ID in Tenant ${Utils.buildTenantName(tenant)}`
- });
- }
- }
-
- public getVersion(): string {
- return '1.0';
- }
-
- public getName(): string {
- return 'FixChargingStationCompanyAndSiteIDsTask';
- }
-
- public isAsynchronous(): boolean {
- return true;
- }
-}

Update also Transactions and Assets