Migrate wrong rounded consumptions
MODIFY
src/migration/MigrationHandler.ts
src/migration/MigrationHandler.ts
@@ -18,6 +18,7 @@ import CleanupOrphanBadgeTask from './tasks/CleanupOrphanBadgeTask';
import CleanupSiteAreasTask from './tasks/CleanupSiteAreasTask';
import Constants from '../utils/Constants';
import DeleteChargingStationPropertiesTask from './tasks/DeleteChargingStationPropertiesTask';
+import FixedConsumptionRoundedPriceTask from './tasks/FixedConsumptionRoundedPriceTask';
import InitialCarImportTask from './tasks/InitialCarImportTask';
import { LockEntity } from '../types/Locking';
import LockingManager from '../locking/LockingManager';
@@ -97,6 +98,7 @@ export default class MigrationHandler {
currentMigrationTasks.push(new AddDescriptionToTagsTask());
currentMigrationTasks.push(new AddDefaultPropertyToTagsTask());
currentMigrationTasks.push(new DeleteChargingStationPropertiesTask());
+ currentMigrationTasks.push(new FixedConsumptionRoundedPriceTask());
// Get the already done migrations from the DB
const migrationTasksDone = await MigrationStorage.getMigrations();
// Check

ADD
src/migration/tasks/FixedConsumptionRoundedPriceTask.ts
+import Constants from '../../utils/Constants';
+import Logging from '../../utils/Logging';
+import MigrationTask from '../MigrationTask';
+import { ServerAction } from '../../types/Server';
+import Tenant from '../../types/Tenant';
+import TenantStorage from '../../storage/mongodb/TenantStorage';
+import global from '../../types/GlobalType';
+
+const MODULE_NAME = 'FixedConsumptionRoundedPriceTask';
+
+export default class FixedConsumptionRoundedPriceTask extends MigrationTask {
+ async migrate(): Promise<void> {
+ const tenants = await TenantStorage.getTenants({}, Constants.DB_PARAMS_MAX_LIMIT);
+ for (const tenant of tenants.result) {
+ await this.migrateTenant(tenant);
+ }
+ }
+
+ async migrateTenant(tenant: Tenant): Promise<void> {
+ let modifiedCount = 0;
+ const result = await global.database.getCollection(tenant.id, 'consumptions').updateMany(
+ { },
+ [
+ {
+ $set: {
+ roundedAmount: {
+ $round: [ '$amount', 2 ]
+ }
+ }
+ }
+ ]
+ );
+ modifiedCount += result.modifiedCount;
+ // Log in the default tenant
+ if (modifiedCount > 0) {
+ Logging.logDebug({
+ tenantID: Constants.DEFAULT_TENANT,
+ action: ServerAction.MIGRATION,
+ module: MODULE_NAME, method: 'migrateTenant',
+ message: `${modifiedCount} Consumptions have been updated in Tenant '${tenant.name}'`
+ });
+ }
+ }
+
+ getVersion(): string {
+ return '1.0';
+ }
+
+ getName(): string {
+ return 'FixedConsumptionRoundedPriceTask';
+ }
+
+ isAsynchronous(): boolean {
+ return true;
+ }
+}

Migrate wrong rounded consumptions