update siteID and company Id in charging station
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -2,8 +2,7 @@ import AddCompanyIDToChargingStationsTask from './tasks/AddCompanyIDToChargingSt
import AddCompanyIDToTransactionsTask from './tasks/AddCompanyIDToTransactionsTask';
import AddUserIDToCarsTask from './tasks/AddUserIDToCarsTask';
import Constants from '../utils/Constants';
-import FixChargingStationCompanyIDsTask from './tasks/FixChargingStationCompanyIDsTask';
-import FixChargingStationSiteIDsTask from './tasks/FixChargingStationSiteIDsTask';
+import FixChargingStationCompanyAndSiteIDsTask from './tasks/FixChargingStationCompanyAndSiteIDsTask';
import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
import Logging from '../utils/Logging';
@@ -150,8 +149,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new RepairTransactionPricedAtZero());
currentMigrationTasks.push(new UpdateEmailsToLowercaseTask());
currentMigrationTasks.push(new UserCleanUpTask());
- currentMigrationTasks.push(new FixChargingStationSiteIDsTask());
- currentMigrationTasks.push(new FixChargingStationCompanyIDsTask());
+ currentMigrationTasks.push(new FixChargingStationCompanyAndSiteIDsTask());
return currentMigrationTasks;
}
}

ADD
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;
+ }
+}

DELETE
src/migration/tasks/FixChargingStationCompanyIDsTask.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 = 'FixChargingStationCompanyIDsTask';
-
-export default class FixChargingStationCompanyIDsTask 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) {
- siteArea = await global.database.getCollection<any>(tenant.id, 'siteareas').findOne({ _id: chargingStation.siteAreaID });
- site = await global.database.getCollection<any>(tenant.id, 'sites').findOne({ _id: siteArea.siteID });
- await global.database.getCollection<any>(tenant.id, 'chargingstations').updateOne(
- { _id: chargingStation._id },
- {
- $set: {
- companyID: site.companyID
- }
- }
- );
- updated++;
- }
- }
- // 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 'FixChargingStationCompanyIDsTask';
- }
-
- public isAsynchronous(): boolean {
- return true;
- }
-}

DELETE
src/migration/tasks/FixChargingStationSiteIDsTask.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 = 'FixChargingStationSiteIDsTask';
-
-export default class FixChargingStationSiteIDsTask extends TenantMigrationTask {
- public async migrateTenant(tenant: Tenant): Promise<void> {
- let updated = 0; let siteArea;
- // 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) {
- siteArea = await global.database.getCollection<any>(tenant.id, 'siteareas').findOne({ _id: chargingStation.siteAreaID });
- await global.database.getCollection<any>(tenant.id, 'chargingstations').updateOne(
- { _id: chargingStation._id },
- {
- $set: {
- siteID: siteArea.siteID
- }
- }
- );
- updated++;
- }
- }
- // 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 site ID in Tenant ${Utils.buildTenantName(tenant)}`
- });
- }
- }
-
- public getVersion(): string {
- return '1.0';
- }
-
- public getName(): string {
- return 'FixChargingStationSiteIDsTask';
- }
-
- public isAsynchronous(): boolean {
- return true;
- }
-}

MODIFY
src/server/rest/v1/service/SiteAreaService.ts
src/server/rest/v1/service/SiteAreaService.ts
@@ -365,14 +365,13 @@ export default class SiteAreaService {
}
siteArea.smartCharging = filteredRequest.smartCharging;
siteArea.accessControl = filteredRequest.accessControl;
- if (siteArea.siteID !== filteredRequest.siteID) {
- await ChargingStationStorage.updateChargingStationsSite(req.tenant, filteredRequest.id, filteredRequest.siteID);
siteArea.siteID = filteredRequest.siteID;
- }
siteArea.lastChangedBy = { 'id': req.user.id };
siteArea.lastChangedOn = new Date();
// Save
await SiteAreaStorage.saveSiteArea(req.tenant, siteArea, Utils.objectHasProperty(filteredRequest, 'image'));
+ // Update charging station
+ await ChargingStationStorage.updateChargingStationsSite(req.tenant, filteredRequest.id, filteredRequest.siteID);
// Retrigger Smart Charging
if (filteredRequest.smartCharging) {
// FIXME: the lock acquisition can wait for 30s before timeout and the whole code execution timeout at 3s

MODIFY
src/server/rest/v1/service/SiteService.ts
src/server/rest/v1/service/SiteService.ts
@@ -400,10 +400,7 @@ export default class SiteService {
req.tenant, req.user, filteredRequest.companyID, Action.READ, action, filteredRequest);
// Update
site.name = filteredRequest.name;
- if (site.companyID !== filteredRequest.companyID) {
- await ChargingStationStorage.updateChargingStationsCompany(req.tenant, filteredRequest.id, filteredRequest.companyID);
site.companyID = filteredRequest.companyID;
- }
if (Utils.objectHasProperty(filteredRequest, 'public')) {
if (!filteredRequest.public) {
// Check that there is no public charging stations
@@ -443,6 +440,8 @@ export default class SiteService {
site.lastChangedOn = new Date();
// Save
await SiteStorage.saveSite(req.tenant, site, Utils.objectHasProperty(filteredRequest, 'image') ? true : false);
+ // Update charging station
+ await ChargingStationStorage.updateChargingStationsCompany(req.tenant, filteredRequest.id, filteredRequest.companyID);
await Logging.logInfo({
tenantID: req.user.tenantID,
user: req.user, module: MODULE_NAME, method: 'handleUpdateSite',

MODIFY
src/storage/mongodb/ChargingStationStorage.ts
src/storage/mongodb/ChargingStationStorage.ts
@@ -976,7 +976,7 @@ export default class ChargingStationStorage {
siteAreaID: DatabaseUtils.convertToObjectID(siteAreaID),
},
{
- $set: { siteID: DatabaseUtils.convertToObjectID(siteAreaID) }
+ $set: { siteID: DatabaseUtils.convertToObjectID(siteID) }
});
await Logging.traceDatabaseRequestEnd(tenant, MODULE_NAME, 'updateChargingStationsSite', startTime, { siteID });
}

update siteID and company Id in charging station